Reputation: 2500
How to make hierarhical mapping Hibernate?
For example: Category
id parent_id name
1 0 Root
2 1 Sub-root 1
3 1 Sub-root 2
4 2 Sub-(sub-root 1)
Is it possible to make lazy mapping for such Category
object?
Upvotes: 0
Views: 684
Reputation: 23246
It is not exactly clear what you are asking.
However it would appear you are talking about a self-referencing relationship rather than Inheritance so you can then map as below. The default fetch strategy should be same as for any other @OneToMany
i.e. LAZY.
@Entity
public class Category{
@Id
private Long id;
@ManyToOne
@JoinColumn(name = "parent_id")
private Category parent;
@OneToMany(mappedBy = "parent")
private Set<Category> subCategories;
}
Upvotes: 2
Reputation: 13566
I believe you want to ask about inheritance of entities. I recommend using JPA inheritance strategies. There are 3 available.
Different strategies have different advantages and disadvantages. You can choose according to your need.
Upvotes: 1