views:

131

answers:

2

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.

A: 
@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.

kem
A: 

Hi,

As shown in your question, you have a @OneToOne

@Entity
public class SomeClass {

    @Id
    private Long id;

    @OneToOne
    private SomeClass relatedTo;

}

regards,

Arthur Ronald F D Garcia