| EntityType#loadByUniqueKey looks up the entity by EntityUniqueKey via PersistenceContext#getEntity(EntityUniqueKey). If it is not present, then the entity gets loaded by its unique ID from the database. If the result is non-null, it should be mapped by its unique ID via PersistenceContext#addEntity( EntityUniqueKey, Object). For example: {{@Entity public class Lens { ... @ManyToOne() @JoinColumn(name="`frame_fk`", referencedColumnName = "name") private Frame frame; } @Entity public class Frame { ... @OneToMany( mappedBy = "frame" ) private Set<Lens> lenses; @Column(unique=true) private String name; } }} Without the proposed fix, when Frame#lenses is initialized, the same Frame will be loaded by name for each Lens in the Frame#lenses. With the fix, Frame will be loaded by name only once. |