I've got a following Java class which is also a Hibernate entity:
@Entity
@Table(name = "category")
public class Category {
@ManyToOne
@JoinColumn(name="parent_id")
private Category parent;
public Category getParent() {
return parent;
}
public void setParent(Category parent) {
this.parent = parent;
}
The category represents a node in a category tree. I'm implementing a webservice which allows to CRUD categories. For instance, the interface has the ability to create a category tree node and it passes the category id as a parameter.
I want just to create a new Category object and persist it into the database without fetching the parent object. My data provider class looks like this:
public void createCategory(int parent_id, String name, CategoryType type) {
Category category = new Category();
category.setName(name);
// category.setParent(?); <- I don't have this object here
// category.setParentId(id); <- but I do have the id
category.setType(type);
this.categoryDao.save(category);
}
My question is: what can I do to create a new Category object with the parent_id set if assuming I won't call hibernate to fetch the parent for me (this would be stupid)? Can I provide a setParentId/getParentId method for Category class? What hibernate annotations would it have?