Hi all,
I am using hibernate with JPA and have come across something that looks a bit strange to me. I have two entities that have a OneToOne bi directional mapping between them. When I do a query on the owning entity, it gets loaded twice.
Short version of my entities are;
Code:
@Entity
public class One {
@Id
@GeneratedValue
private long id;
@OneToOne
private Two two;
private int counting;
}
@Entity
public class Two {
@Id
@GeneratedValue
private long id;
@OneToOne(mappedBy="two")
private One one;
}
When I perform the query
Code:
select o from One as o left join fetch o.two where o.counting > 50
hibernate executes the following sql
Code:
Hibernate: select one0_.id as id0_0_, two1_.id as id1_1_, one0_.counting as counting0_0_, one0_.two_id as two3_0_0_ from One one0_ left outer join Two two1_ on one0_.two_id=two1_.id where one0_.counting>50
and then the following for every entity returned in the first query.
Code:
Hibernate: select one0_.id as id0_1_, one0_.counting as counting0_1_, one0_.two_id as two3_0_1_, two1_.id as id1_0_ from One one0_ left outer join Two two1_ on one0_.two_id=two1_.id where one0_.two_id=?
What I don't understand is why that is the case, as the One entity has already been loaded by the first query and Two.one is mearly just pointing back at it.
Am I missing something?
Thanks
Mark