-->
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: how to mapping like this?
PostPosted: Wed Nov 26, 2003 3:43 am 
Newbie

Joined: Wed Aug 27, 2003 2:49 am
Posts: 8
example:
menu{id,name,fid}
so a menu A have any children menu .and the fid of children menu equals the id of A.

how to mapping ?


Top
 Profile  
 
 Post subject: Re: how to mapping like this?
PostPosted: Wed Nov 26, 2003 3:57 am 
Newbie

Joined: Wed Aug 27, 2003 2:49 am
Posts: 8
users
Code:
<class name="Users" table="Users">
      <id name="userid" type="string">
         <column name="userid" />
         <generator class="uuid.hex"/>
      </id>
      <property name="password" column="password" type="string"/>
      <property name="enabled" column="enabled" type="boolean"/>
      <property name="userNick" column="userNick" type="string"/>
      <set name="menus" inverse="true" lazy="true" cascade="all">
         <key column="userid"/>
         <one-to-many class="Menu"/>
      </set>
      <property name="userName" column="userName" type="string"/>
   </class>

menu:
Code:
<class name="Menu" table="Menu">
                <id name="menuid" type="string">
                        <column name="menuid" />
                        <generator class="uuid.hex"/>
                </id>
                <property name="menuName" column="menuName" type="string"/>
                <many-to-one name="user" class="Users" column="userid" not-null="true"/>
                <property name="link" column="link" type="string"/>
                <property name="description" column="description" type="string"/>
                <set name="childmenus" lazy="true" cascade="all" inverse="true">
                        <key column="fid"/>
                        <one-to-many class="Menu"/>
                </set>
        </class>


it work success between users and menu.
but how to do with the menu and its childmenus;

menu.java
Code:
public class Menu {
   private Set childmenus;
   private String description;
   private String link;
   private String menuid;// the Hibernate identifier
   private String menuName;
   private Users user;
..........................
//other is common setter and getter
}


when i add a children menu to a menu.
Code:
   child=new Menu();
                     child.setMenuName("childtest");
                     child.setDescription(" a child menu for file0");
                     child.setUser(users);
                     Set childmenus=new HashSet();
                     childmenus.add(child);
                     menu.setChildmenus(childmenus);


but the new record in the database has not a fid .

pls tell me why?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 2:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Since you inverse=true the collection, the other side is responsibel of updating the link.
http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue

1. Add a many-to-one association to "parentMenu", and do childMenu.setParentMenu(parentMenu);

2. set inverse="false" (default). But this may be inefficient see the parent child relationship chapter on the reference guide.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: composite pattern on wiki
PostPosted: Wed Nov 26, 2003 3:26 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Check out the composite pattern on the wiki at:
http://www.hibernate.org/86.html

Quote:
but the new record in the database has not a fid .

You haven't mapped it that I can see.
You need:
Code:
<many-to-one name="parent" column="fid" cascade="save-update" class="Menu"/>

...and of course add the parent Menu reference to your Menu class.
This will make it a bidirectional association as per epbernard's reply.


Top
 Profile  
 
 Post subject: Re: composite pattern on wiki
PostPosted: Wed Nov 26, 2003 11:12 pm 
Newbie

Joined: Wed Aug 27, 2003 2:49 am
Posts: 8
i have correct my code.

and it is ok!

but now i have another problem .
i want that the User Object get the menus not include the childmenus.
example:
user->menu(top level)->childmenus

how can i implement it?


Top
 Profile  
 
 Post subject: Re: composite pattern on wiki
PostPosted: Thu Nov 27, 2003 3:03 am 
Newbie

Joined: Wed Aug 27, 2003 2:49 am
Posts: 8
i change my user.hbm.xml.


Code:
<set name="menus" inverse="true" lazy="true" cascade="all" [b]where="fid is null"[/b]>
         <key column="userid"/>
         <one-to-many class="Menu"/>
      </set>


and it is ok now.


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.