@Entity
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer id;
private Integer code;
private Integer item;
}
@Entity
public class Child implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private Integer id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="prop_code", referencedColumnName="code"),
@JoinColumn(name="prop_item", referencedColumnName="item")
})
private Parent property;
}
When i load two different childs in the same session, and both reference the same parent, 4 queries are fired, 2 for the childs and 2 for the (same) parent. In that case there should be only 3 queries, two for the childs, and one for the parent, as i believe the parent should be added to the persistenceContext entitiesByUniqueKey map after it is retrieved from the database for the first time. If my assumptions are correct, a NAIVE fix would be to call persister.addEntity(euk, result) somewhere inside EntityType.loadByUniqueKey. I've tried this already in a fork, and although it fixed the issue, when running the full suite of tests with H2, a (seemingly) unrelated test (org.hibernate.jpa.test.lock.LockTest.testContendedPessimisticLock) broke on cleanup, so since im extremelly new to Hibernate i have no idea of the side effects of my "fix". |