-->
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.  [ 5 posts ] 
Author Message
 Post subject: one or zero to one or zero lazy loading
PostPosted: Wed Jul 30, 2008 5:45 am 
Newbie

Joined: Mon Feb 05, 2007 11:49 am
Posts: 13
Hello.

I'm trying to map a one or zero to one or zero relationship.
Right now I have it working without lazy loading, but this causes the application to slow down considerably.
I've read a lot os posts regarding the solution to the one to one or zero lazy loading issues, but none of that applie here, since the fact that both sides can be null causes constraint violations when it happens.
Right now I'm using the following mappings for this association:

<one-to-one name="normalTp"
class="com.nsn.tm.server.eml.dom.TPImpl"
property-ref="fragmentTp"
outer-join="false"
cascade="none"/>

<many-to-one name="fragmentTp"
class="com.nsn.tm.server.eml.dom.TPImpl"
cascade="none"
unique="true">

<column name="FRAGMENT_TP_PARENT_PTP_ID"
not-null="false"
unique-key="TM_T_PFRAGMENT_TP_IDX"/>

<column name="FRAGMEN_TP_PARENT_PARENT_NE_ID"
not-null="false"
unique-key="TM_T_PFRAGMENT_TP_IDX"/>

<column name="FRAGMENT_TP_TP_ID"
not-null="false"
unique-key="TM_T_PFRAGMENT_TP_IDX"/>

</many-to-one>

Can some tell how I can modify this in order to get lazy loading to work?

Right now I get one of these

select ... from TM_ROUTE_POINT tpimpl0_ where tpimpl0_.FRAGMENT_TP_PARENT_PTP_ID=? and tpimpl0_.FRAGMEN_TP_PARENT_PARENT_NE_ID=? and tpimpl0_.FRAGMENT_TP_TP_ID=? and tpimpl0_.CLASS='ITP'

of each element returned by this

select ... from TM_ROUTE_POINT tpimpl0_ where tpimpl0_.CLASS='ITP' and tpimpl0_.TP_ID_PARENT_PARENT_NE_ID=?

Thanks in advance,

Hugo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 12:39 pm 
Newbie

Joined: Wed Jul 30, 2008 12:19 pm
Posts: 1
Location: Portugal
I'm also facing this issue.

First, it's a relationship between the same entity.

Secondly, many-to-one are supposed to have lazy=proxy by default so there's no need to define it explicitly.

Third, it just doesn't work and I'm unable to find out why. I tried one-to-one mappings before but I'm also mapping 0..1<-->1..0, and so it blew up when using constrained=true.

Anyone has a hint on this one?

Cheers,
PP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 6:31 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The problem with this kind of relation is that from one of the sides (from the entity containing the <one-to-one> link) it is not possible to know if there is 0 or 1 object without querying the other table. It doesn't matter if the other entity is mapped with lazy=true or not. To convince yourself about this, try executing the last SQL in your post manually and then try to see if you can know if the <one-to-one> link should be a proxy or a null. It is not possible to know this from the information that was selected.

There are some possible workarounds for this problem:

1. Try using a fetch join in your query. It should be possible to avoid the extra select statements in this case.

2. If the relation is between two different entities, put the <one-to-one> link in the entity that you expect is going to be used less often. Ok, this workaround will not help in this particular case, since only one entity is involved. Worked well for me though.

3. Map both sides as <many-to-one>. This can work but you must make sure that your correctly updates both ends of relation. This can be tricky and I don't recommend it unless nothing else works out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 3:27 am 
Newbie

Joined: Mon Feb 05, 2007 11:49 am
Posts: 13
Hello.

Too bad there's not mapping solution for this.
The fetch join did improve things a lot though.
I think i'll leave it at that.

Thanks,

Hugo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 7:58 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
Using <many-to-one> is the "general" solution, you just have to be careful to always update both ends of the association (which you should be doing anyway).

Using left join fetch can help, but it's not a real solution since the associations will still be loaded if you:
    a. forget to use left join fetch
    b. load using Session.load()
    c. etc.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.