-->
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: Annoating an "organization tree". Parent/child in
PostPosted: Mon Jun 11, 2007 8:45 am 
Newbie

Joined: Mon Jun 11, 2007 8:25 am
Posts: 1
Hi, my first post here. Thought i just add an example of an annotated entity with a parent/child association in same table since i've never found it myself when i needed it.
Also i would like feedback if my method is the standard/best/optimal way for doing this. Any suggestions is appreciated!

The entity (not an organization but the concept is the same, each musicgenre can have sub genres and a parent genre),
Code:
import java.util.*;
import javax.persistence.*;

@Entity
@Table (name="music_genre")
public class MusicGenre {
   
   @Id
   @GeneratedValue (strategy=GenerationType.AUTO, generator="musicgenre_seq_gen")
   @SequenceGenerator(name="musicgenre_seq_gen", sequenceName="MUSICGENRE_SEQ")
   private Long id = new Long(-1);
   
   @OneToOne
   @JoinColumn (name="parent_id")
   private MusicGenre parentMusicGenre = null;
   
   @OneToMany (cascade=CascadeType.ALL)
   @JoinColumn(name="parent_id")
   private List<MusicGenre> childMusicGenres = new ArrayList<MusicGenre>();
   
   private String name = "";
   
   public MusicGenre() {
      
   }
   
   public MusicGenre(String name) {
      this.name = name;
   }

   public Long getId() {
      return id;
   }

   public String getName() {
      return name;
   }

   public MusicGenre getParentMusicGenre() {
      return parentMusicGenre;
   }

   public void setName(String name) {
      this.name = name;
   }

   public void setParentMusicGenre(MusicGenre parentMusicGenre) {
      this.parentMusicGenre = parentMusicGenre;
   }
   
   public void addChildMusicGenre(MusicGenre childMusicGenre) {
      childMusicGenre.setParentMusicGenre(this);
      childMusicGenres.add(childMusicGenre);
   }
   
   public List<MusicGenre> getChildMusicGenres() {
      return Collections.unmodifiableList(childMusicGenres);
   }
   
   public void setChildMusicGenres(List<MusicGenre> childMusicGenres) {
      this.childMusicGenres = childMusicGenres;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 9:06 am 
Newbie

Joined: Tue Jun 12, 2007 9:05 am
Posts: 1
Hi,
How to do the same using a .hbm.xml file for the same class?
Thanks


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.