![](/img/trans.png)
[英]how to mapping an OneToMany relation with Weak Entity in 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之間存儲順序。
請提出一些設計。 謝謝
您可以從抽象實體繼承C
和D
,並在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.