| For a OneToOne relationship, if the table associated with the owning side entity has 2 records related to the non-owning side entity, this should raise a PersistenceException, claiming that "More than one row with the given identifier was found", e.g.:
public class EntityA {
@Id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "entityA")
private EntityB entityB;
}
public class EntityB {
@Id
@GeneratedValue
private Long id;
@OneToOne
@JoinColumn(name = "ENTITY_A_ID", referencedColumnName = "ID")
private EntityA entityA;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "entityB", fetch = FetchType.EAGER)
private List<EntityC> entityCS;
}
If EntityB has an association of type ToMany with eager fetching, this will bypass the duplicated foreign key references check for the OneToOne association with entity A, and the Exception will not be thrown From The AbstractEntityOwner load method code, the exception will not be thrown if the Entity is a collection owner
if (this.getCollectionOwners() != null) {
return list.get(0);
} else {
throw new HibernateException("More than one row with the given identifier was found: " + id + ", for class: " + this.persister.getEntityName());
}
There is no check if different records from owning side entity references the same key of the non-owning side entity |