繁体   English   中英

实体类映射在hibernate中,复杂的关系

[英]Entity class mapping in hibernate, complex relation

我有四个类(A,B,C,D)都按关系连接。 我正在使用hibernate将它们存储在mysql数据库中。

课程结构

Class A {
    Integer id;           //primary key
    List<B> listOfB;      //one to many with B class, able to store order of B's

    //some other fields
}

/* This class can contain list of c, or list of d, or both.
 * need to keep order among c and d also if contains both list
 * for eg, B has list of c(c1, c2, c3) and D (d1, d2, d3)
 * order can be c1, d1, c2, d3, c3, d2
 */
Class B {
    Integer id;           //primary key
    List<C> listOfC;      //one to many with D class, able to store order of C's
    List<D> listOfD;      //one to many with C class, able to store order of D's

    //How to store order between list of c and list of d?       

     //some other field
}

Class C {
    Integer id;           //primary key
    List<D> listOfD;      //one to many with D class, able to store order of D's

     //some other field
}

Class D {
    Integer id;           //primary key
    String value;         //some value

}

这里A和B之间的关系是一对多,B到C是一对多,B到D是一对多,C到D是一对多。

List仅用于跟踪对象之间的顺序。 但我也想跟踪B类中c和d列表的顺序。

B can have following steps:
 1. C1
 2. D1
 3. c2
 4. D2

使用当前设计,我无法在c和d之间存储顺序。

请提出一些设计。 谢谢

您可以从抽象实体继承CD ,并在B使用单个列表。

抽象实体:

@Entity
@Inheritance
@DiscriminatorColumn(name = "discr")
public abstract class X {
    Integer id; 
    // ...
}

然后扩展它:

@Entity
@DiscriminatorValue("c")
public class C extends X {
    List<D> listOfD;
    // ...
}

@Entity
@DiscriminatorValue("d")
public class D extends X {
    String value;     
    // ...
}

最后:

@Entity
public class B {
    Integer id;
    List<X> listOfX; // single list for C and D
}

看一下继承状态 在我的例子中,我使用的是单表策略。

另外看看这个

显然你需要添加关系注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM