-->
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: @OneToMany mapping with parent child entity of same class
PostPosted: Thu Feb 23, 2006 7:01 pm 
Newbie

Joined: Thu Feb 23, 2006 6:44 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:hibernate-annotations-3.1beta8

I am having difficulty trying to map an entity which contains a parent entity and collection of children entities of the same class type using annotations. Basically, I want to create a tree of parent-children entities using the example below:

@Entity
@Table(name = "T_ORGANIZATION")
public class Organization implements IOrganization, java.io.Serializable {

private Long _id = null;
private String _name = null;
private IOrganization _parent = null;
private ArrayList<IOrganization> _children = null;

public Organization() {
_children = new ArrayList<IOrganization>();
}

public void setId(Long id) {
_id = id;
}

@Id
@GeneratedValue
@Column(name = "ORG_ID")
public Long getId() {
return _id;
}

public void setName(String name) {
_name = name;
}

@Column(name = "ORG_NAME")
public String getName() {
return _name;
}

public void setParent(IOrganization parent) {
_parent = parent;
}

@OneToOne(cascade = CascadeType.ALL, targetEntity = Organization.class)
@JoinColumn(name="PARENT_ID")
public IOrganization getParent() {
return _parent;
}

public void setChildren(Collection<IOrganization> children) {
_children = new ArrayList<IOrganization>(children);
}

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, targetEntity=Organization.class)
// HOW DO I JOIN THIS???
public Collection<IOrganization> getChildren() {
return _children;
}
}


TABLE T_ORGANIZATION:
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| ORG_ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| PARENT_ID | int(10) unsigned | YES | | NULL | |
| ORG_NAME | varchar(50) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+

How do I go about mapping the child relationships when I just want to keep the parent child relationships in the same table? The parent reference will work for me, but am having difficulty storing and fetching the children.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 4:13 pm 
Newbie

Joined: Fri Jul 29, 2005 4:43 am
Posts: 7
Try this, though I am not sure if it will work with interfaces:
Code:
public @Entity @Table(name="T_ORGANIZATION") class Organization implements IOrganization {

   private Organization parentOrganization = null;/
   
   private List<Organization> children = null;
   
   /**
    * @return the parent organization
    */
   public @ManyToOne(optional=true) Organization getParentOrganization() {
      return parentOrganization;
   }
   
   /**
    * @param parentOrganization the PARENT ORGANIZATION to set
    */
   public void setParentOrganization(Organization parentOrganization) {
      this.parentOrganization = parentOrganization;
   }
   

   @OneToMany(cascade = CascadeType.ALL, mappedBy="parentOrganization")
   public List<Organization> getChildren() {
      return children;
   }

}


I just tried it and it seems to work. The IOrganization interface that I created already extends Serializable so I didn't make Organisation implement it.
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.  [ 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.