vegarwe wrote:
> 
> 
> arvinder wrote:
>> 
>> You can do this by having a many to many association between users and
>> groups as follows, assuming you have  User, Group and Group_User tables,
>> where Group_User is a link table, you can avoid n+1 selects by having the
>> following mapping in User.hbm.xml
>> <set name="groups" outer-join="true" table="Group_User">
>>    <key column="user_id"/>
>>    <many-to-many column="group_id" outer-join="true" class="Group"/>
>> </set>
>> 
> 
> Cool, I will try it out.
> 
> 
> arvinder wrote:
>> 
>> Note if you have outer-join="true" only as an attribute of the set
>> element it will eager fetch only the rows from the Group_User table and
>> will result in n+1 selects as you have described below, to get the
>> groups. But when you add outer-join="true" as an attribute in the
>> many-to-many element, it will fetch the group instances too by fetching
>> rows from Group table in the same query.
>> 
> 
> Ahhh, these little quirks that makes all the difference. Is there a good
> article, book, howto, paper, ... on this kinds of tricks. All the examples
> I find are a bit simple, mostly for getting started. I really good book on
> the subject would be great. (There is probably a jungle of not-so-great
> books out there.)
> I normally use Hibernate in Action book (this is in there), now they have
> a new edition called Java Persistence with Hibernate, you can also search
> hibernate forum.
> 
> 
> arvinder wrote:
>> 
>> Along with the above you need to set the hibernate property
>> hibernate.max_fetch_depth=2 (default is 1) as hibernate has to traverse
>> two levels to get the groups.
>> 
> 
> Which config? /WEB-INF/hibernate.properties? Will this effect other parts
> of my project (I'm guessing it will, but how...)
> 
> Yes either hibernate.cfg.xml or hibernate.properties, in your case it is
> the latter one. It will not effect other parts of your project unless you
> have such mapping at other places. This setting basically tells how far
> from the original entity to go in one query. In above case as it is 2 as
> you are going to Group_User and then Group table.
> 
> 
> arvinder wrote:
>> 
>> Now the down side is that this outer join query will be run each time a
>> user instance is loaded. You might encounter performance problems if you
>> are not using scalar queries to get a list of users.
>> 
> 
> How do I know if I'm using scalar queries for lists? (Sorry, I'm new at
> this.)
> 
> Scalar queries are when you are not fetching entitities but raw values
> (for more
> http://www.hibernate.org/hib_docs/reference/en/html/querysql.html). They
> have value in certain cases. For example if you are fetching a list of
> users for display, and you have mapping defined as above, it will have
> additional joins.
> Another thing you might try is not specifying 'set' outer-join attribute,
> but specify it in many-to-many. This way it should lazily load
> group_users(when you are getting the list) , but when you call
> user.getGroups() it should get group entities.
> I have not tried it though.
> 
> 
> arvinder wrote:
>> 
>> Hope this helps
>> 
> 
> Sure, thanks!
> 

-- 
View this message in context: 
http://www.nabble.com/Getting-the-architecture-right-tf4081789s2369.html#a11696515
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to