Arquebus
Arquebus

Reputation: 87

JPA 2 persistence context specification

I'm currently studying the official JPA 2 final specification. Is the following statement contained anywhere in the Spec?

The Entity Manager guarantees that within a single Persistence Context, for any particular database row, there will be only one object instance.

Either I don't clearly understand the spec or I just can't find the part that proves that the quoted statement is part of the specification.

Upvotes: 2

Views: 273

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42074

No, specification does not give such a guarantee. But in my opinion it is implicitly assumed.

In practice sometimes same table is mapped to the two different entities. One of them being treated as read only entity. Read only entity can for example be used for reporting purposes and as an optimization contains only subset of fields in other entity. This can be done for example as follows:

@Entity
public class EntityA {    
    @Id private Integer id;
    @Lob
    byte[] tooHeavyToLoadAlways;
}

@Entity
@Table(name="EntityA")
public class EntityALightWeight {
    @Id private Integer id;
}

For JPA there is no connection between these two entities, so keeping care that only first one of them is modified and that second one is refreshed is responsibility of application. Because of that should be used only with caution, because EntityALightWeight can be refreshed from database but will never contain changes made to the EntityA in same transaction.

Upvotes: 1

Related Questions