例如:有三个类 A B C 最终要持久化的类是A 而 B C 都作为组件内在与A B,C都要使用@Embeddable标注声明为一个组件
class A 代码:
- @Entry
- public class A{
- private int id;
- private B b;
- private C c;
- ...
-
-
- public B getB(){
- ...
- }
-
- public C getC(){
-
- ....
- }
-
- }
@Entry
public class A{
private int id;
private B b;
private C c;
...
public B getB(){
...
}
public C getC(){
....
}
}
B 包含C
class B
- @Embeddable
- public class B{
- private Stirng bname;
- private C c;
- get set....
- }
@Embeddable
public class B{
private Stirng bname;
private C c;
get set....
}
class C
- @Embeddable
- public class C{
- private String cnam2;
-
- get set....
- }
@Embeddable
public class C{
private String cnam2;
get set....
}
持久化后 并没有将C的cnam重复持久化为两个字段如果需要这样则需要:
在A中使用:
@Enumerated
@AttributeOverrides(
{@AttributeOverride(name="bname",column = @Column(name="cname1") )}
)
这样cname将会再次被颜色为一个字段cnam1