-->
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.  [ 6 posts ] 
Author Message
 Post subject: newbie: Hierarchical menustructure from database with rights
PostPosted: Wed Aug 10, 2005 9:28 pm 
Newbie

Joined: Wed Aug 10, 2005 9:08 pm
Posts: 6
Hi All,

I've just registered to the hibernate forum.
I'm developing a web application. I'd like to load the menus from a database, but always only the menus belonging to an appropriate role.

So I have 3 tables:
user_roles(id, descr)
menus (id, descr, link, pos, type, parent_id)
menu_rights(menu_id, role_id)

I created the appropriate base xml files. (After the tutorial, so nothing complex,.)
(For example in the UserRole.hbm.xml:
...
<set name="menus" table="menu_rights" >
<key column="role_id"/>
<many-to-many column="menu_id" class="Menu"/>
</set>
.....

in the Menu.hbm.xml:
...
<set name="children" inverse="true" lazy="true">
<key column="parent_id"/>
<one-to-many class="Menu"/>
</set>
......

)

My problems are:
- When calling UserRole.getMenus() I get all the menus belonging to the role, but I'd like to get only the top-level menus:
- After getting the top-level menus, and then calling menu.getChildren() I get all the direct sub-menus, but I would like to get only the ones that belong to the role with that I got the top-level menus. So I would like always to see the menus that belong to an appropriate role.
Can I do it with the hibernate?

Thnx
George


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 10:50 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
Have you tried formula?

I haven't tested this,

<set name="menus" table="menu_rights" >
<key column="role_id"/>
<many-to-many formula="select menu_id from menu_rights, menus where menu_rights.id=menu_id and parent_id is null" class="Menu"/>
</set>

assuming that the parent id is null for top level menus.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 2:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
look at h3 filters

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 8:41 am 
Newbie

Joined: Wed Aug 10, 2005 9:08 pm
Posts: 6
Hi!

It's very strange. I'm getting ClassCastException with this, but when I use column instead of formula it works (at least I got all the menus:)).

George


anar wrote:
Have you tried formula?

I haven't tested this,

<set name="menus" table="menu_rights" >
<key column="role_id"/>
<many-to-many formula="select menu_id from menu_rights, menus where menu_rights.id=menu_id and parent_id is null" class="Menu"/>
</set>

assuming that the parent id is null for top level menus.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 8:54 am 
Newbie

Joined: Wed Aug 10, 2005 9:08 pm
Posts: 6
Ok I will look at it. But in the Menu class I don't store role_id, I think that is the problem. From a menu I'm always getting all the children and not only the children with the appropriate roles.
It would be good to get the whole Menustructure when calling UserRole.getMenus(). I mean the whole tree, and then when I call menu.getChildren() then no sql will be executed but the previously results would be used. So without hibernate I would do it in that way I think: building the menustructure(tree) in advance from the role. Maybe it is possible also with hibernate?

Or Should I create the menu class from the menu_rights and menu tables (Maybe join in the hibernate-mapping?) so that I always have the role_id in the Menu class?

max wrote:
look at h3 filters


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 2:00 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I tried a where=.. condition in the set instead of formula. the where condition worked.

<set name="menus" table="menu_rights" where="parent_id is null">
<key column="role_id"/>
<many-to-many column="menu_id" class="Menu"/>
</set>


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