当我使用JPA查询的getResultList()时,结果是分离的还是管理的?

时间:2022-08-15 04:48:30

When I have a JPA Query that I call .getResultList(), It gives me back a List of objects. Are the objects in that list managed or detached? That is, do I have to worry about merging or persisting them later if I make changes to them, or will those changes be picked up automatically?

当我有一个名为. getresultlist()的JPA查询时,它会返回一个对象列表。列表中的对象是托管的还是分离的?也就是说,如果我对它们做了更改,我是否需要担心合并或持久化它们,或者这些更改是否会被自动拾取?

3 个解决方案

#1


13  

Yes, the objects returned from .getResultList() are managed.

是的,从. getresultlist()返回的对象是受管理的。

When you made changes on the managed objects, you do not worry about merging as those changes will be picked up by the EntityManager automatically.

当您对托管对象进行更改时,您不必担心合并,因为这些更改将被EntityManager自动拾取。

The managed objects will become detached when the EntityManager that is used to load that object is close(), clear() or detach(). Detached objects are not manged anymore and you should do merging to let the EntityManager pick up the changes.

当用于加载该对象的EntityManager是close()、clear()或detach()时,托管对象将被分离。分离对象不再被管理,您应该进行合并,让EntityManager接收更改。

#2


2  

From my experience the getResultList() return values are Attached. That is, you do not have to manually persist them if you make modifications to them within the same transaction.

根据我的经验,getResultList()返回值是附加的。也就是说,如果您在同一事务中对它们进行修改,则不必手工持久化它们。

#3


2  

They will be managed if you are currently within a transaction, but if you are not (e.g. if you have annotated your trnasction with TransactionAttributeType.NOT_SUPPORTED or TransactionAttributeType.NEVER) your entities will not be managed.

如果您当前在事务中,它们将被管理,但是如果您没有管理(例如,如果您用TransactionAttributeType注释了您的trnasction)。NOT_SUPPORTED或TransactionAttributeType.NEVER)您的实体将不会被管理。

#1


13  

Yes, the objects returned from .getResultList() are managed.

是的,从. getresultlist()返回的对象是受管理的。

When you made changes on the managed objects, you do not worry about merging as those changes will be picked up by the EntityManager automatically.

当您对托管对象进行更改时,您不必担心合并,因为这些更改将被EntityManager自动拾取。

The managed objects will become detached when the EntityManager that is used to load that object is close(), clear() or detach(). Detached objects are not manged anymore and you should do merging to let the EntityManager pick up the changes.

当用于加载该对象的EntityManager是close()、clear()或detach()时,托管对象将被分离。分离对象不再被管理,您应该进行合并,让EntityManager接收更改。

#2


2  

From my experience the getResultList() return values are Attached. That is, you do not have to manually persist them if you make modifications to them within the same transaction.

根据我的经验,getResultList()返回值是附加的。也就是说,如果您在同一事务中对它们进行修改,则不必手工持久化它们。

#3


2  

They will be managed if you are currently within a transaction, but if you are not (e.g. if you have annotated your trnasction with TransactionAttributeType.NOT_SUPPORTED or TransactionAttributeType.NEVER) your entities will not be managed.

如果您当前在事务中,它们将被管理,但是如果您没有管理(例如,如果您用TransactionAttributeType注释了您的trnasction)。NOT_SUPPORTED或TransactionAttributeType.NEVER)您的实体将不会被管理。