开发者

Hibernate parent-child modeling

开发者 https://www.devze.com 2022-12-21 04:23 出处:网络
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.

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,

0

精彩评论

暂无评论...
验证码 换一张
取 消