-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Unable to proxy a reference if not-found is set to ignore
PostPosted: Fri Jul 22, 2005 6:50 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
Code:
  <class name="Person" table="TPerson">
    <id name="persistentIdentifier" type="java.lang.Long" column="PK_persistentIdentifier" access="property"/>
    <property name="Age" column="CAge0" type="java.lang.Integer" not-null="true" unique="false" access="property"/>
    <property name="Married" column="CMarried1" type="java.lang.Boolean" not-null="true" unique="false" access="property"/>
    <property name="Sex" column="CSex2" type="java.lang.Boolean" not-null="true" unique="false" access="property"/>
    <many-to-one name="Spouse" class="Person" column="FK_Spouse3_Person0" not-null="false" access="property" not-found="ignore"/>
    <property name="Name" column="CName4" type="java.lang.String" not-null="true" unique="false" access="property"/>
  </class>


I save two Person objects A and B, with one being the spouse of other and vice versa.

When I load the object A, the spouse reference is not being proxied but loaded when not-foun set to ignore for the many-to-one association. But if I remove it then it is proxied as expected.
Is this related to foreign key constraint or something like that? like one-to-one needs to have constraint=true otherwise the reference is initialized instead of proxied.

Can any one help?

Thanks,
Prasad
Code:


Top
 Profile  
 
 Post subject: Is lazy fetching disallowed when not-found is set to true?
PostPosted: Fri Jul 22, 2005 7:15 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
Upon further inspection, when not-found is set true, ManyToOneType.isNullable() is returning true thus selecting LoadEvenListener.INTERNAL_LOAD_NULLABLE. But this type do not allow creation of proxies. Any idea why this restriction exists? Basically this means that not-found=ignore can't be used with lazy fetching strategy. Is that right?

The code is from 3.0.5 version.
EntityType.java:261
Code:
   protected final Object resolveIdentifier(Serializable id, SessionImplementor session)
   throws HibernateException {
      return session.internalLoad( getAssociatedEntityName(), id, eager, isNullable() );


SessionImpl.java:651
Code:
   
public Object internalLoad(String entityName, Serializable id, boolean eager, boolean nullable) throws HibernateException {
      // todo : remove
      LoadEventListener.LoadType type = nullable ?
         LoadEventListener.INTERNAL_LOAD_NULLABLE :
         eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 7:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, that is correct.

(Well, lazy="proxy" can't be used. lazy="true" can be.)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 7:53 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
Thanks Gavin for the quick response. I have tried with the following combinations to understand completely when proxying is enabled and when not.

1. <many-to-one name="Spouse" class="Person" not-null="true" not-found="ignore" lazy="true" access="property"/> -->proxying is disabled


2. <many-to-one name="Spouse" class="Person" not-null="true" access="property"/> -->proxying is enabled


3. <many-to-one name="Spouse" class="Person" not-null="true" lazy="true" access="property"/> -->proxying is disabled


4. <many-to-one name="Spouse" class="Person" not-null="false" access="property"/> -->proxying is enabled


5. <many-to-one name="Spouse" class="Person" not-null="false" lazy="true" access="property"/> -->proxying is disabled


What is the difference between laze="true" or lazy="proxy"? Even with lazy=true and not-found=ignore, proxying is disabled. Can you please clarify?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 7:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
How about reading the documentation about lazy="true"? Among other things, it tells you you need the bytecode enhancer.

By the way, it should be very obvious why lazy="proxy" does not work with not-found="ignore". Think about it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 8:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually, I apologise, I gave you bad advice. We have removed lazy="true" from the <many-to-one> mapping in 3.1 'cos it had some problems.

There is no way to use lazy fetching with not-found="ignore".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 2:04 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
gavin, this post helped me solve an almost identical problem I've been having. Let me ask you, though, you mentioned above that

>>it should be very obvious why lazy="proxy" does not work with not-found="ignore"

Is that because the proxy object that serves as a place holder can't be null?
Can you elaborate on the rationale for that, please? To the uninitiated like myself, it would seem that you could leave those as null, but I am sure there's a good reason why not. (and, yes, I read the documentation about proxying, unless there's some place you can point to that I should have read more carefully).
Thanks so much,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 2:09 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
and being that there isn't a way to have lazy fetching with not-found="ignore", is the only way to avoid multiple selects in that case is to do a fetch outer join?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
aturetsky wrote:
gavin, this post helped me solve an almost identical problem I've been having. Let me ask you, though, you mentioned above that

>>it should be very obvious why lazy="proxy" does not work with not-found="ignore"

Is that because the proxy object that serves as a place holder can't be null?
Can you elaborate on the rationale for that, please? To the uninitiated like myself, it would seem that you could leave those as null, but I am sure there's a good reason why not. (and, yes, I read the documentation about proxying, unless there's some place you can point to that I should have read more carefully).
Thanks so much,
Alex



To determine if the object should be built with a proxy instance or with a null, you have to hit the other database table. In which case you may as well just load the object on the other side.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 2:33 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
aha, ok, now it makes sense - thanks for your lightning-fast response. so my only option to avoid n+1 selects, then, is fetch outer join, ha?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 11:04 am 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
shucks, left outer join doesn't seem to help here either. I searched high and low through the documentation, but I couldn't see anything that said that left outer join solution to the n+1 problem would not work where not-found="ignore". Please help. Thanks so much.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 11:06 am 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
btw, when I say it doesn't work I mean I still get multiple selects. Let me know if it's not supposed to work in this case, or if it's meant to work, in which case I will post my code.


Top
 Profile  
 
 Post subject: no way to prevent n+1 with not-found="ignore" ?
PostPosted: Mon Sep 19, 2005 6:08 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
I haven't received a response. Is that because I should post this as a separate topic? Yet, this strongly appears to be an issue with not-found="ignore". I tried everything and yet I still get the multiple selects issue, with join="fetch" being ignored. Now, in the past what appeared to me as a bug turned out to be a hibernate feature or an oversight on my part, but this one certainly looks a lot like a bug. If I am wrong, I apologize.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 8:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
What is the code and mapping that causes the multiple selects? You have not given enough information.


Top
 Profile  
 
 Post subject: code posted for the not-found="ignore" causing n+1
PostPosted: Tue Sep 20, 2005 11:08 am 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
Thanks for your willingness to help, gavin

Here are the mappings (just to eliminate all factors, I resorted to specifying left outer join in three different ways: outer-join="true" fetch="join" and in hql)

Code:
<hibernate-mapping package="smefs.pojo">
    <class name="BuyerMgr" table="V_BUYER_MGR">
        <composite-id name="id" class="BuyerMgrKey">
            <key-property name="buyerCode" column="BUYER_CODE" type="string"/>
            <key-property name="secGroupCd" column="SEC_GROUP_CD" type="string"/>
            <key-property name="sourceSystem" column="SOURCE_SYSTEM" type="string"/>
        </composite-id>
        <property name="buyerName" column="BUYER_NAME" type="string" />
        <many-to-one name="userIdentity" formula="BUYER_USER_ID" class="UserIdentity" not-found="ignore" outer-join="true" fetch="join"/>
        <property name="buyerUserId" column="BUYER_USER_ID" type="string" insert="false" update="false"/>
    </class>
</hibernate-mapping>


Code:
<hibernate-mapping package="smefs.pojo">
    <class name="UserIdentity" table="T32_USER_IDENTITY_REF">
        <id name="userId" column="USER_ID" type="string"/>
        <property name="firstName" column="FIRST_NAME" type="string"  not-null="true" />
        <property name="lastName" column="LAST_NAME" type="string"  not-null="true" />
        <property name="timestamp" column="TIMESTAMP" type="timestamp"  not-null="true" />
    </class>
</hibernate-mapping>


Here's the code I'm running:
Code:
return HibernateUtil.getCurrentSession().createQuery("from BuyerMgr bm left join fetch bm.userIdentity").list();



And here's what I get:


Hibernate: select buyermgr0_.BUYER_CODE as BUYER1_1_0_, buyermgr0_.SEC_GROUP_CD as SEC2_1_0_, buyermgr0_.SOURCE_SYSTEM as SOURCE3_1_0_, useridenti1_.USER_ID as USER1_8_1_, buyermgr0_.BUYER_NAME as BUYER4_1_0_, buyermgr0_.BUYER_USER_ID as BUYER5_1_0_, buyermgr0_.BUYER_USER_ID as formula0_0_, useridenti1_.FIRST_NAME as FIRST2_8_1_, useridenti1_.LAST_NAME as LAST3_8_1_, useridenti1_.TIMESTAMP as TIMESTAMP8_1_ from V_BUYER_MGR buyermgr0_ left outer join T32_USER_IDENTITY_REF useridenti1_ on buyermgr0_.BUYER_USER_ID=useridenti1_.USER_ID
Hibernate: select useridenti0_.USER_ID as USER1_8_0_, useridenti0_.FIRST_NAME as FIRST2_8_0_, useridenti0_.LAST_NAME as LAST3_8_0_, useridenti0_.TIMESTAMP as TIMESTAMP8_0_ from T32_USER_IDENTITY_REF useridenti0_ where useridenti0_.USER_ID=?
Hibernate: select useridenti0_.USER_ID as USER1_8_0_, useridenti0_.FIRST_NAME as FIRST2_8_0_, useridenti0_.LAST_NAME as LAST3_8_0_, useridenti0_.TIMESTAMP as TIMESTAMP8_0_ from T32_USER_IDENTITY_REF useridenti0_ where useridenti0_.USER_ID=?
Hibernate: select useridenti0_.USER_ID as USER1_8_0_, useridenti0_.FIRST_NAME as FIRST2_8_0_, useridenti0_.LAST_NAME as LAST3_8_0_, useridenti0_.TIMESTAMP as TIMESTAMP8_0_ from T32_USER_IDENTITY_REF useridenti0_ where useridenti0_.USER_ID=?
and then a whole bunch of these identical selects


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.