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.  [ 13 posts ] 
Author Message
 Post subject: loading three tables with relation 1-n
PostPosted: Mon Jun 30, 2008 10:39 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hello,

I would like to load data from three tables :

Dictionary <-1-n-> DictionaryEntry <-1-n-> DictionaryResult

The problem is that the loading is very slow : 30 seconds and thousands of queries sent to the database...

Here is the mapping file I used :

<class name="Dictionary" lazy="false">
<id name="ID">
<generator class="assigned" />
</id>
<property name="Kwrd" />
<bag name="DictionaryEntry_List" lazy="false">
<key column="Dictionary_ID"/>
<one-to-many class="DictionaryEntry" />
</bag>
</class>

<class name="DictionaryEntry" lazy="false">
<id name="ID">
<generator class="assigned" />
</id>
<property name="Kwrd" />
<many-to-one name="Dictionary" column="Dictionary_ID" class="Dictionary" />
<bag name="DictionaryResult_List" lazy="false">
<key column="DictionaryEntry_ID"/>
<one-to-many class="DictionaryResult" />
</bag>
</class>

<class name="DictionaryResult" lazy="false">
<id name="ID">
<generator class="assigned" />
</id>
<property name="Word" />
<many-to-one name="DictionaryEntry" column="DictionaryEntry_ID" class="DictionaryEntry" />
</class>

If I add fetch="join"on both bag of Dictionary and DictionaryEntry, the loading is much faster (about 3 seconds) but I get multiple instances of the same Dictionaries...

Do you have a solution for that problem ?

Regards,

mathmax


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 2:45 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Does anybody know how to have efficiency of the second method without doubloons?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 2:26 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If you have a criteria query, you can use a result transformer:

.SetResultTransformer(CriteriaUtil.DistinctRootEntity)

which removes the duplicate root entities. If that's not possible, try searching the forum for "duplicate items" or "DistinctRootEntity", there are lot's of posts about this.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 3:57 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
But why does nHibernate not generate the duplicate instances automatically?
The first method (without using fetch=join) doesn't generate duplicate instances but it is very slow...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 4:31 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
That's the documented behaviour with outer join fetching ...

http://www.hibernate.org/117.html#A12

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 8:25 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
.SetResultTransformer(CriteriaUtil.DistinctRootEntity) do the trick, but I wonder if the duplicate instances are created and then removed or if the duplicate items are filtered before instantiation ?

If I simply have a parent-children relation with a fetch=join, then there is no duplicated instances although it should be the default behavior as explained in the link you gave. I should have three tables in relations with two fetch=join to get duplicated items. Why ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 8:54 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think it depends on the way you do the query, UniqueResult() or List(). Actually you should get duplicates with a simple parent child relationship. How did you write the query ?

Quote:
but I wonder if the duplicate instances are created and then removed or if the duplicate items are filtered before instantiation ?


You only get one instance of the object. After the first one is created, hibernate pulls that one from the session cache instead of reinstantiating it over and over again.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 9:22 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Quote:
How did you write the query ?


I just use a criteria to get the result.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 10:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
criteria.List() ord criterial.UniqueResult() ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 11:10 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
criteria.List()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 11:41 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I assume the reason for that is that crit.List() returns an System.ArrayList which itself takes care of the duplicates. Your collections are bags which allow duplicate items.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 12:11 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Sorry, I use Criteria.List<T>(). (the generic version)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 12:22 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Shouldn't matter, still the same explanation ... hibernate uses its own implementation for bags (PersistentGenericBag).

_________________
--Wolfgang


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