开发者

how to associate single entity to multiple entities(pojo classes)

does anyone here knows the correct way of associating a single entity(a pojo class) to multiple classes.. im currently working on a situ开发者_运维知识库ation where mainClass has a one-to-many relationship to subClass and subClass has one-to-many relationship to unitsClass too. the relationship of these classes looks like this:

  • mainClass - oneToMany - subClass
  • subClass - manyToOne - mainClass AND oneToMany - unitsClass
  • unitsClass - manyToOne - subClass

i dont know if its possible for subClass to contain multiple associations to multiple classes.if not, what would be the best way to address this problem? im using hibernate annotations.

hope someone could help me out on this.

thank you so much!


Yes. In the simplest form it would look like this:

@Entity
public class MainClass {
   @OneToMany
   private List<SubClass> subclasses;

   // Id and other fields
}

@Entity
public clsas SubClass {
    @ManyToOne
    private MainClass mainClass 

    @OneToMany
    private List<UnitClass> unitClasses;
}

@Entity
public class UnitClass {
    @ManyToOne
    private SubClass subClass;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜