在休眠JPA的getReference

在休眠JPA的getReference

本文介绍了在休眠JPA的getReference()之后不使用setter发出select的情况下无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法-

 @Transactional
 public void savethis(){
    EntityObject t = entityManagerTreasury.getReference(EntityObject.class, 1);
    t.setAction("abc");
 }

现在,符合以下答案- https://stackoverflow.com/a/1608621/4881766

Now, going in line with the following answer - https://stackoverflow.com/a/1608621/4881766

我应该只会在我的sql日志中看到一个更新查询.

I should be only seeing an update query in my sql logs.

但是我观察到的行为如下-

However the behaviour i've observed is as follows -

  1. 提供代码-选择然后更新
  2. 评论t.setAction("abc");行-没有选择也没有更新
  3. 用find()替换getReference()-选择然后更新

我期望的行为是,如果我在代理上使用任何getter,则应该发出选择,但是当仅使用setter时,我希望在方法末尾通过更新和更新来提交更改.没有选择发布.

The behaviour i was expecting was that if i use any getter on the proxy, then a select should be issued, but when only using a setter, i wanted the changes to be committed at the end of the method with an update and no select being issued.

结果是,无论我对代理对象,getter或setter做什么,它都会发出选择.

Turns out, no matter what i do with the proxy object, getter or setter, it issues a select.

我想更新给定ID的实体的选定字段.如果有任何方法可以在不编写jpql或本机查询的情况下更新我想要的任何字段,我将不胜感激.

I want to update selected fields of an entity for a given id.If there is any way to update any fields that i want without writing jpql or native query, I'd really appreciate it.

提前谢谢!

推荐答案

来自 EntityManager.getReference()文档:

因此,在entityManagerTreasury.getReference之后不会发出选择.

Therefore, after entityManagerTreasury.getReference no select is issued.

仅在t.setAction("abc")之后,如果尚未获取实体状态,则发出选择以获取状态.

Only after t.setAction("abc"), if the entity state is not already fetched, a select is issued to fetch the state.

重点是:除非获取实体状态,否则实体管理器无法保存实体的状态.因此,除非您使用JPQL,否则您不能跳过先前的选择.

The point is: the entity manager cannot save the state of an entity unless the entity state is fetched. Therefore you cannot skip the prior select, unless you use JPQL.

这篇关于在休眠JPA的getReference()之后不使用setter发出select的情况下无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:05