在许多方面用于基本类型的一对多映射 [英] one-to-many mapping for basic type at many side

查看:87
本文介绍了在许多方面用于基本类型的一对多映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道基于基本类型的基于注释的一对多映射,例如Person有许多昵称。 Person是类类型,昵称是基本类型String。一个人很多昵称。 hibernate / stable / annotations / reference / en / html / entity.html#entity-hibspec-singleassocrel =nofollow noreferrer> Hibernate Annotations手册,其中有一个可疑类似于您的示例:


在一些简单情况下,不要
需要关联两个实体,而
只需创建一个基本$ b $的集合b类型或可嵌入的对象。使用
@ElementCollection。

public class User {

[...]
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆