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.