I have a Model class. This class should be able to reference itself, i.e. the resulting "output" from the model should be like this.
Some instanceOf Model.class
|-> Some instanceOf Model.class having 开发者_开发百科parent instance referenced as parent_id
|-> Some instanceOf Model.class having ...........
The instances represent geographical entities organized in a hierarchy. To be honest I have no idea how to implement this.
@Entity
class MyClass {
@Id
private Long id;
@ManyToOne
private MyClass parent;
@OneToMany
private Set<MyClass> children;
}
Here's a place to start. Use parent and/or children as needed depending on how you want to navigate the hierarchy. I'll leave it to you to fill in the details.
As shown in your question, you have a @OneToOne
@Entity
public class SomeClass {
@Id
private Long id;
@OneToOne
private SomeClass relatedTo;
}
regards,
精彩评论