Reputation: 5351
I would like to store hierarchical folders in database. F.e
@Entity
public class Folder {
int id
String name
Folder parentFolder
}
So if folder is in subfolder he should store information about parentFolder If Folder is in root/top folder, it dosnt have any parentFolder, so there will be null
How should I set hibernate using annotation to achive this?
My entity class:
@Entity
@Table(name="common__Role")
public class Role {
/** The id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
/**
* Parent Role id.
*/
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ROLE_ID")
public Role role;
/** The name. */
@Constraints.Required
public String name;
Upvotes: 1
Views: 563
Reputation: 8282
This should be your Entity ..
@Entity
@Table("FOLDER")
public class Folder {
@Id
private long id;
@Column(name="NAME")
private String name;
@ManyToOne(fetch=FetchType.LAZY,cascade = {CascadeType.PERSIST,CascadeType.MERGE })
@JoinColumn(name="PARENT_ID")
private Folder parentFolder;
//Getter and Setter
}
Maybe you should follow this tutorial
How to persist ManyToOne
Folder f= new Folder();
f.setName("name");
Folder fParent = entityManager.find(Folder .class, 1L);
f.setParent(fParent);
entityManager.persist(f);
See also this tutorial
Upvotes: 2