-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to save a menu in the database?
PostPosted: Tue Aug 15, 2006 3:19 pm 
Newbie

Joined: Sat May 22, 2004 1:47 pm
Posts: 13
I would like to save my menu in the database using the folowing structure:

+Menu
|
Menu 1 (Link)
|
Menu 2 (Link)
|
+ Sub-Menu 1 (Link) (submenu is actually an instance of Menu)
|
Item 1 (Link)
|
Item 2 (Link)

I can´t find posts discussing this clearly.
If you have an example, I would appreciate (and surely thousands others).

Best Regards,
Sorry if I´m doing something wrong


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 3:46 pm 
Newbie

Joined: Sat May 22, 2004 1:47 pm
Posts: 13
It´s is working!

Here is my class:

Code:
public class Menu {
        private Long id;
   private String title;
   private String link;
   private Menu parent;
   private List<Menu> children;
   
   public Menu() {
      this(null, null);
   }
   
   public Menu(String title, String link) {
      this.title = title;
      this.link = link;
      children = new ArrayList<Menu>();
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getLink() {
      return link;
   }

   public void setLink(String link) {
      this.link = link;
   }

   public Menu getParent() {
      return parent;
   }
   
   public void setParent(Menu parent) {
      this.parent = parent;
   }
   
   public void setChildren(List<Menu> children) {
      this.children = children;
   }

   public List<Menu> getChildren() {
      return children;
   }
   
   public void addChild(Menu menu) {
      getChildren().add(menu);
      menu.setParent(this);
   }
}


Here is the hbm.xml:
Code:
   <many-to-one name="parent"
             column="ID_PARENT"
             class="Menu" />

        <bag name="children"
            inverse="true"
            cascade="all"
            lazy="false">
           <key column="ID_PARENT" />
           <one-to-many class="Menu" />
        </bag>


I love Hibernate!!

Hope it helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 3:48 pm 
Newbie

Joined: Sat May 22, 2004 1:47 pm
Posts: 13
It´s is working!

Here is my class:

Code:
public class Menu {
        private Long id;
   private String title;
   private String link;
   private Menu parent;
   private List<Menu> children;
   
   public Menu() {
      this(null, null);
   }
   
   public Menu(String title, String link) {
      this.title = title;
      this.link = link;
      children = new ArrayList<Menu>();
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getLink() {
      return link;
   }

   public void setLink(String link) {
      this.link = link;
   }

   public Menu getParent() {
      return parent;
   }
   
   public void setParent(Menu parent) {
      this.parent = parent;
   }
   
   public void setChildren(List<Menu> children) {
      this.children = children;
   }

   public List<Menu> getChildren() {
      return children;
   }
   
   public void addChild(Menu menu) {
      getChildren().add(menu);
      menu.setParent(this);
   }
}


Here is the hbm.xml:
Code:
   <many-to-one name="parent"
             column="ID_PARENT"
             class="Menu" />

        <bag name="children"
            inverse="true"
            cascade="all"
            lazy="false">
           <key column="ID_PARENT" />
           <one-to-many class="Menu" />
        </bag>


I love Hibernate!!

Hope it helps.


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