[英]@ElementCollection over @OneToMany or vice-versa
Let's assume I do have an Offer
entity.假设我确实有一个Offer
实体。 It has a property salaries
that may be a list of Salary
objects.它有一个可以是Salary
对象列表的属性salaries
。 The Salary
cannot exist without an Offer
, so does it mean I should use @ElementCollection
or may not? Salary
没有Offer
就不能存在,所以这是否意味着我应该使用@ElementCollection
或不可以? When should we use @ElementCollection
and when @OneToMany
.我们应该何时使用@ElementCollection
以及何时使用@OneToMany
。 The same question comes to my mind when we talk about @Embeddable
and @Embedded
, is it worth using it, or perhaps it's better to have a @OneToOne
relationship.当我们谈论@Embeddable
和@Embedded
时,我会想到同样的问题,是否值得使用它,或者拥有@OneToOne
关系会更好。
class Salary(
val salary_from: Integer,
val salary_to: Integer,
val salary_type: Type
)
class Offer(
@ElementCollection
@CollectionTable(name = "offer_salaries", joinColumns = @JoinColumn(name = "offer_id"))
@OrderColumn
@Column(name = "salaries")
val salaries: List<Salary>;
)
@OneToMany
is meant for when you want to map to a db table as an entity. @OneToMany
适用于当您想将 map 作为实体添加到数据库表时。 @ElementCollection
: you can look at it as a regular collection which is used for mapping non-entities. @ElementCollection
:您可以将其视为用于映射非实体的常规集合。
ref: Difference between @OneToMany and @ElementCollection?参考: @OneToMany 和 @ElementCollection 之间的区别?
EG: you have a Perfume
entity and it has 2
lists. EG:你有一个Perfume
实体,它有2
列表。
@ElementCollection
) since it contains some attributes which are not entities.气味- 是一个属性列表(应该是@ElementCollection
),因为它包含一些不是实体的属性。@oneToMany
since it should be mapped as an entity)客户- 是一个实体列表(应该是@oneToMany
,因为它应该被映射为一个实体)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.