Yesterday I wrote about self mapping collections in Hibernate. I used XML mapping document to define the relationship. Today I would like to show how to achieve the same result using Java Annotations. So without further adu here is the java class:
@Entity
@Table(name = "CATALOGUE")
public class Catalogue implements Serializable { @Id
@Column(name = "ID")
private Integer id;
@Column(name = "CONTENT", nullable = true)
private String content;
@ManyToOne
@JoinColumn(name = "PARENT_ID")
private Catalogue parent;
@OneToMany(mappedBy = "parent")
private Set<Catalogue> children = new HashSet<Catalogue>();
public Catalogue() {
}
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Set<Catalogue> getChildren() {
return children;
}
public void setChildren(Set<Catalogue> children) {
this.children = children;
}
public void addChild(Catalogue child) {
child.setParent(this);
children.add(child);
}
public Catalogue getParent() {
return parent;
}
public void setParent(Catalogue parent) {
this.parent = parent;
}
}
No comments:
Post a Comment