JpaRepository getOne() 获取并更新整个对象
JpaRepository getOne() fetches and updates whole object
我正在尝试模仿这个公认的 SO 答案
在仅更新某些对象的字段时使用 Hibernate 代理。
我的代码如下所示:
@Transactional
public void updateStudentYear(int uniqueNumber, int newYear) {
Student studentProxy = studentRepository.getOne(uniqueNumber);
studentProxy.setYear(newYear);
studentRepository.save(studentProxy);
}
问题在于 Hibernate 会执行 完整 select 和 完整更新 。这是日志:
2021-03-10 20:24:23.240 DEBUG 118757 --- [ main] org.hibernate.SQL : select student0_.unique_number as unique_n1_1_0_, student0_.name as name2_1_0_, student0_.team as team3_1_0_, student0_.year as year4_1_0_ from students student0_ where student0_.unique_number=?
2021-03-10 20:24:23.268 DEBUG 118757 --- [ main] org.hibernate.SQL : update students set name=?, team=?, year=? where unique_number=?
我原以为只会发布一个“更新年份”语句,但令我惊讶的是,看起来一切都像经典的 findOne()/save( ) 对运算。我错了吗?
不,很遗憾,链接问题中的答案是错误的。 getOne()/getReference()
方法不是关于更新单个字段,而是关于避免在必要时访问数据库。
该方法的用处在于
Foo foo = fooRepo.findOne(id);
Bar barProxy = barRepo.getOne(id);
foo.setBar(barProxy);
fooRepo.save(foo);
在这种情况下,Foo
将从数据库中获取,但 Bar
不会。
JPA 将始终更新更新语句中的所有字段,尽管我不确定它是否会受到实现自身限制列的 dirty checking. Hibernate has 的影响,但 JPA 没有指定通用机制它。
我正在尝试模仿这个公认的 SO 答案
@Transactional
public void updateStudentYear(int uniqueNumber, int newYear) {
Student studentProxy = studentRepository.getOne(uniqueNumber);
studentProxy.setYear(newYear);
studentRepository.save(studentProxy);
}
问题在于 Hibernate 会执行 完整 select 和 完整更新 。这是日志:
2021-03-10 20:24:23.240 DEBUG 118757 --- [ main] org.hibernate.SQL : select student0_.unique_number as unique_n1_1_0_, student0_.name as name2_1_0_, student0_.team as team3_1_0_, student0_.year as year4_1_0_ from students student0_ where student0_.unique_number=?
2021-03-10 20:24:23.268 DEBUG 118757 --- [ main] org.hibernate.SQL : update students set name=?, team=?, year=? where unique_number=?
我原以为只会发布一个“更新年份”语句,但令我惊讶的是,看起来一切都像经典的 findOne()/save( ) 对运算。我错了吗?
不,很遗憾,链接问题中的答案是错误的。 getOne()/getReference()
方法不是关于更新单个字段,而是关于避免在必要时访问数据库。
该方法的用处在于
Foo foo = fooRepo.findOne(id);
Bar barProxy = barRepo.getOne(id);
foo.setBar(barProxy);
fooRepo.save(foo);
在这种情况下,Foo
将从数据库中获取,但 Bar
不会。
JPA 将始终更新更新语句中的所有字段,尽管我不确定它是否会受到实现自身限制列的 dirty checking. Hibernate has