I have an entity A which has an arbitrary join (for read-only report HQL purposes only) to an entity B, based on the condition that the B (if it exists) has 3 properties in common.
Note that it's not possible to extract those 3 properties as a separate entity in my domain model because in A they are nullable and in B they are definitely not nullable.
Code:
@ManyToOne(targetEntity = BImpl.class, fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "prop1_id", referencedColumnName = "prop1_id",
insertable = false, updatable = false),
@JoinColumn(name = "prop2_id", referencedColumnName = "prop2_id",
insertable = false, updatable = false),
@JoinColumn(name = "prop3_id", referencedColumnName = "prop3_id",
insertable = false, updatable = false)
})
public B getB()
{
return b;
}
Now when I run my old query's on this entity (which don't care about B), it decided to eager fetch the B's anyway? Which of course yields an n+Q query problem for all those query's.
Code:
2008-06-25 11:49:47 DEBUG: select this_.id as id28_8_, this_.version as version28_8_, ... from a this_
2008-06-25 11:49:47 DEBUG: select b0_.id as id29_0_, b0_.version as version29_0_, b0_.prop1_id as prop13_29_0_, b0_.otherProp_id as otherProp_29_0_, ... from b b0_ where b0_.prop1_id=? and ...
2008-06-25 11:49:47 DEBUG: select b0_.id as id29_0_, b0_.version as version29_0_, b0_.prop1_id as prop13_29_0_, b0_.otherProp_id as otherProp_29_0_, ... from b b0_ where b0_.prop1_id=? and ...
...
Is this a known bug or I am missing an magic hibernate specific annotation to fix this surprising behavior?