使用 JPA/Hibernate 注释映射字符串列表 [英] Map a list of strings with JPA/Hibernate annotations

查看:33
本文介绍了使用 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.

推荐答案

This is in Hibernate 术语值的集合"或元素".有一个(特定于 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天全站免登陆