Hi!
I've opened the following issue on JIRA:
https://hibernate.atlassian.net/browse/HHH-10936and i wanted a clarification, but since the issue was closed i can't comment there anymore.
The issue said:
Quote:
@ManyToOne mapped by a composite natural key should be added to persistenceContext's entitiesByUniqueKey map
Code:
@Entity
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer id;
private Integer code;
private Integer item;
}
Code:
@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".
Gail's answer says:
Quote:
Parent does not have a unique ID, so it cannot be added to persistenceContext's entitiesByUniqueKey.
Child has a non-unique many-to-one, as you mention that more than one Child can have the same parent. Since Child#parent is non-unique, it cannot be added to persistenceContext's entitiesByUniqueKey either.
Although I understand the answer, i believe that if i map a relation with manytoone, its implicit that the joincolumns must form a unique key in the referenced entity, am i missing something?
Anyway, i would like to know how can i tell hibernate that the Parent's code/item properties form a composite unique key, so they are hopefully added to persistenceContext's entitiesByUniqueKey.
Thanks!