[hibernate-dev] Queries executed with bidirectional @OneToOnes

Guillaume Smet guillaume.smet at gmail.com
Wed Feb 29 12:21:36 EST 2012


It's not something new as we checked that it was already the case with
Hibernate 3.6 but we just hit an annoying problem with @OneToOne
annotation if the relation is bidirectional.

Simplified example:
class A {
        @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
	private B b;

class B {
        @OneToOne(mappedBy = "b")
	private A a;

If we insert 1 object in each table and we try to list the objects of
A, the following queries are executed:
1. select a0_.id as id55_, a0_.b_id as b2_55_ from A a0_
2. select b0_.id as id54_1_, a1_.id as id55_0_, a1_.b_id as b2_55_0_
from B b0_ left outer join A a1_ on b0_.id=a1_.b_id where b0_.id=1
3. select a0_.id as id55_1_, a0_.b_id as b2_55_1_, b1_.id as id54_0_
from A a0_ left outer join B b1_ on a0_.b_id=b1_.id where a0_.b_id=1

I don't understand the need for the third query. And this query
quickly becomes a problem as it's executed for each EAGER @OneToOne
relationship of A.

In our application, we have a lot of @OneToOne relationships on an
entity and a variant of the third query (which is big as the entity is
big and the third query tries to populate all the EAGER relations) is
executed for each @OneToOne of the base entity.

I can provide more information if needed. I don't know if it's a
limitation or a bug, that's why I haven't created a JIRA issue yet but
I find this behaviour really suboptimal.

As a temporary workaround, we made the @OneToOne relations
unidirectional which makes our application usable again.

Any pointers on where the problem could be or a confirmation that it's
something expected?



More information about the hibernate-dev mailing list