在许多方面用于基本类型的一对多映射 [英] one-to-many mapping for basic type at many side
问题描述
我想知道基于基本类型的基于注释的一对多映射,例如Person有许多昵称。 Person是类类型,昵称是基本类型String。一个人很多昵称。 hibernate / stable / annotations / reference / en / html / entity.html#entity-hibspec-singleassocrel =nofollow noreferrer> Hibernate Annotations手册,其中有一个可疑类似于您的示例:
在一些简单情况下,不要
public class User {
需要关联两个实体,而
只需创建一个基本$ b $的集合b类型或可嵌入的对象。使用
@ElementCollection。
[...]
public String getLastname(){...}
@ElementCollection
@CollectionTable(name = 昵称,joinColumns = @ JoinColumn(name =user_id))
@Column(name =nickname)
public Set< String> getNicknames(){...}
}
注意: Hibernate Annotations版本,
@ElementCollection
被称为@CollectionOfElements
。I want to know annotation based one-to-many mapping for basic type for example Person has many nick names. Person is class type and nick name is basic type String. One Person many nick names.
解决方案Check out section 2.2.5.3.3 of the Hibernate Annotations manual, which has an example suspiciously similar to yours:
In some simple situation, do don't need to associate two entities but simply create a collection of basic types or embeddable objects. Use the @ElementCollection in this case.
@Entity public class User { [...] public String getLastname() { ...} @ElementCollection @CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id")) @Column(name="nickname") public Set<String> getNicknames() { ... } }
Note: In older versions of Hibernate Annotations,
@ElementCollection
was called@CollectionOfElements
.这篇关于在许多方面用于基本类型的一对多映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!