-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many-to-Many association
PostPosted: Wed Jan 11, 2006 9:26 am 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
Hi,

I have a many-to-many association between Fabricante and Revenda and I want to list all fabricante instances with each revenda of them.

I'm using this code to do it. Therefore, It's happening a bad behavior with a generated SQL.


fabricantes = (IList) NHibernateUtil.getSession()
.CreateCriteria(typeof(Fabricante))
.AddOrder(NHibernate.Expression.Order.Asc("NomeFabricante"))
.SetFetchMode("Revendas", NHibernate.FetchMode.Eager)
.List();


The result of this code repeat the same "fabricante" many times and by coincidence ;) the same quantity of the"revendas" for each "fabricante".

I read in the Hibernate in Action that when we use fetch collection, Hibernate doen´t return a distinct result list, and the book recommend use a Set collection to avoid duplicated "fabricante" from the List returned of the query.

If really this happening, I would like to know if there is a better way to do it, because using FetchMode.Eager is a very bad choice. You can imagine if I have 1000 "fabricante" and 1000 "revendas" to each "fabricante".

The resulted would be horrible!

If my thought about this point, is wrong. Please clear my mind.

Thanks in advance,
Cleiton


Top
 Profile  
 
 Post subject: Many-to-Many association
PostPosted: Wed Jan 11, 2006 10:56 am 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
Hi,

I found the solution to the problem.

Change the code from:
Code:
fabricantes = (IList) NHibernateUtil.getSession()
.CreateCriteria(typeof(Fabricante))
.AddOrder(NHibernate.Expression.Order.Asc("NomeFabricante"))
.SetFetchMode("Revendas", NHibernate.FetchMode.Eager)
.List();



To this code appending:
Quote:
.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity)


Code:
fabricantes = (IList) NHibernateUtil.getSession()
.CreateCriteria(typeof(Fabricante))
.AddOrder(NHibernate.Expression.Order.Asc("NomeFabricante"))
.SetFetchMode("Revendas", NHibernate.FetchMode.Eager)
.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity)
.List();



I found the solution in this topic:
http://forum.hibernate.org/viewtopic.php?t=952693

Thanks,
Cleiton


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