I'm using this type of mapping; It works using the initial insert, but when making modifications (e.g. deleting children) and updating, I get an exception thrown.
I've just opened a thread regarding this issue
here. Again, this still doesn't work completely for me, but initial insert and selects DO work, so I must be in the right direction.
Fairly straightforward, actually, just notice that you have to implement Comparable for the SortedSet to work (I'm guessing that you don't REALLY need a list).
Hope this helps.
Code:
@Entity
public class Category implements Comparable<Category>{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "category_id")
private Long categoryId; //primary key
@Column(name = "name")
private String categoryName;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL, optional = true)
@JoinColumn(name = "PARENT_CATEGORY_ID", nullable = true)
@Fetch(FetchMode.SELECT)
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Category parentCategory;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="parentCategory")
@Sort(type = SortType.NATURAL)
@Fetch(FetchMode.SUBSELECT)
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private SortedSet<Category> childCategories;
}