地图字符串使用JPA / Hibernate的注解列表 [英] Map a list of strings with JPA/Hibernate annotations

查看:187
本文介绍了地图字符串使用JPA / Hibernate的注解列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做这样的事情:

    @Entity public class Bar {
        @Id @GeneratedValue long id;
        List<String> Foos
    }

和有FOOS在表中坚持这样的:

and have the Foos persist in a table like this:

foo_bars (
    bar_id int, 
    foo varchar(64)
);

更新:

我知道如何映射其他实体,但它是矫枉过正在许多情况下。它看起来像什么,我的建议是不可能的,而无需创建另一个实体,或在一些BLOB列一切都结束了。

I know how to map other entities, but it's overkill in many cases. It looks like what I'm suggesting isn't possible without creating yet another entity or ending up with everything in some blob column.

推荐答案

这是在<一个href=\"http://docs.jboss.org/hibernate/stable/annotations/reference/en/html%5Fsingle/#d0e2770\">Hibernate条款一个或元素,值集。有一个(休眠专用)注解吧。 JPA不支持这个(还)。

This is in Hibernate terms a "collection of values" or "elements". There is a (Hibernate specific) annotation for it. JPA does not support this (yet).

在短,诠释您的收藏是这样的​​:

In short, annotate your collection like this:

@CollectionOfElements
@JoinTable(
        table=@Table(name="..."),
        joinColumns = @JoinColumn(name="...") // References parent
)
@Column(name="...value...", nullable=false)

这将创建一个外键和限制必要的表。

This will create the necessary table with foreign keys and restrictions.

这篇关于地图字符串使用JPA / Hibernate的注解列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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