-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Lazy on an arbitrary join @ManyToOne isn't lazy?
PostPosted: Wed Jun 25, 2008 5:55 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
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?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 27, 2008 5:58 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
It looks to me as it can't create a B proxy without initializing the id of its B, so it looks up the B id based on the 3 properties.
But in fact, I don't care about that B proxy (the reference can be null for all I care): however the current implementation creates an n + 1 query problem that kills my performance.

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 7:18 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Is there any way to avoid the n+1 query problem?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 4:44 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Would it be possible to avoid this problem by introducing an entity C between A and B which has a lazy by default @OneToOne from A to C, before it references to B from C?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.