带有@OneToMany属性的可嵌入实体 [英] Embeddable entity with @OneToMany attribute
问题描述
@Entity
public class A {
@Id
@GeneratedValue
私人长ID;
@Embedded
private B b;
// getters and setters
}
@Embeddable
public class B {
@OneToMany
private List< C> ; CLIST;
// getters和setters
}
@实体
公共类C {
@Id
@GeneratedValue
private Long ID;
//其他字段,getters和setters
}
使用模式自动生成功能与 Hibernate
我得到一个额外的表,其中包含 A
和 C $之间的映射C $ C>。但我想通过将
A
的 id 添加到 C 中来实现一对多关系。 code>(例如没有附加表格)。
这可能吗?如果是的话,我应该使用什么样的注释来创建这样的映射?
通常情况下,@JoinColumn 注释。
@OneToMany
@JoinColumn(name =A_ID)
private列表与LT c取代; CLIST;
如果您对可嵌入列中的A_ID名称不满意,则可以在实体中覆盖列名A:
@AssociationOverride(name =cList,
joinColumns = @JoinColumn(name =SOME_NAME_FOR_JOIN_COLUMN_IN_TABLE_C) )
@Embedded
private B b;
Say, I have following entities:
@Entity
public class A {
@Id
@GeneratedValue
private Long id;
@Embedded
private B b;
//getters and setters
}
@Embeddable
public class B {
@OneToMany
private List<C> cList;
//getters and setters
}
@Entity
public class C {
@Id
@GeneratedValue
private Long id;
//other fields, getters and setters
}
Using schema-autogeneration feature with Hibernate
I get an additional table which contains mappings between A
and C
. But I'd like to implement a one-to-many relationship by adding A
's id into C
(e.g without additional table).
Is this possible? If yes, what annotations should I use to create such a mapping?
In general that is possible with @JoinColumn annotation. It works also with embeddables.
@OneToMany
@JoinColumn(name="A_ID")
private List<C> cList;
If you are not happy with A_ID name for column given in embeddable, you can override column name in entity A:
@AssociationOverride(name= "cList",
joinColumns = @JoinColumn(name="SOME_NAME_FOR_JOIN_COLUMN_IN_TABLE_C"))
@Embedded
private B b;
这篇关于带有@OneToMany属性的可嵌入实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!