Help understanding Entity behavior with JPA

#1

Hi Support,

 

We have a entity with an @Id entity which we then put into a list and store in an different entity inside a field defined as:

 

@OneToOne(orphanRemoval=true, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private Object payload;

the list that we put in the 'payload' is an java.util.ArrayList and it contains multitple entities with an the same id.  the behavior we are seeing is that when we reload the entity that contains the 'payload' field is that every entity in the list is the same. I would understand if we ended up with a list with one item in item but we are ending up with a list with 45 items the same this is a little confusing.

can you help?

Dan

#2

The combination of (type, ID) should identify entity objects uniquely. Therefore, only one entity object per (type, ID) is managed per EntityManager. Consequently, it is unclear what exactly you put in the payload list. How can you have multiple entities with the same ID?

Notice that if you just create objects with the IDs they are not managed. You can get the single managed entity of that ID by using find or merge. Non managed objects with the same ID may be replaced by the single managed entity object.

If you just want to eliminate duplicates consider using a Set instead of a List.

ObjectDB Support

Reply