Java 使用 JPA/Hibernate 注释映射字符串列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/796347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:36:03  来源:igfitidea点击:

Map a list of strings with JPA/Hibernate annotations

javahibernatejpaannotations

提问by danb

I want to do something like this:

我想做这样的事情:

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

and have the Foos persist in a table like this:

并让 Foos 保留在这样的表中:

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

UPDATE:

更新:

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.

我知道如何映射其他实体,但在许多情况下它是矫枉过正的。如果不创建另一个实体或以某个 blob 列中的所有内容结束,我的建议似乎是不可能的。

采纳答案by Maarten Winkels

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

这在Hibernate 术语中是“值的集合”或“元素”。有一个(特定于 Hibernate 的)注释。JPA 不支持这个(还)。

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.

这将创建具有外键和限制的必要表。

回答by Michael Pralow

create an entity 'FooBars'

创建一个实体 'FooBars'

refactor the attribut 'Foos' to

将属性 'Foos' 重构为

@OneToMany List Foos

@OneToMany 列出 Foos

回答by Vanger

I'm think it's that what you need:

我认为这就是你需要的:

@Entity 
public class Bar {
    @Id @GeneratedValue long id;

    @OneToMany(mappedBy="bar")   //"bar" = field name in mapping class
    List<FooBar> Foos;
}

@Entity 
public class FooBar {
    @Id @GeneratedValue long id;

    @ManyToOne
    @JoinColumn(name="bar_Id")  
    Bar bar;
}

回答by Renaud

If you store your list as an array, it works:

如果您将列表存储为数组,则它可以工作:

setFoos(String[] foos);

you can transform it like this:

你可以像这样转换它:

setFoos(myList.toArray(new String[myList.size()]));

回答by Max Z

Here is how you would do this if you are using JPA2:

如果您使用 JPA2,您将如何执行此操作:

@Entity public class Bar {
   @Id @GeneratedValue long id;

   @ElementCollection
   @CollectionTable(name="foo_bars", joinColumns=@JoinColumn(name="bar_id"))
   @Column(name="foo")
   List<String> Foos;
 }

For a clearer example see section 2.2.5.3.3 in the Hibernate Annotations Reference Guide.

有关更清晰的示例,请参阅Hibernate Annotations Reference Guide第 2.2.5.3.3 节。

回答by kranti

Here 'Foos' is List of String, So it is unidirectional. We can do this in one model class using @ElementCollection annotation.

这里'Foos'是字符串列表,所以它是单向的。我们可以使用@ElementCollection 注释在一个模型类中完成此操作。

@Entity 
@Table(name="bar")
public class Bar {

        @Id @GeneratedValue 
        long id;

        @ElementCollection
        @JoinTable(
            name="foo_bars",
            joinColumns = @JoinColumn( name="bar_id")
          )
        @Column(name="foo")
        List<String> Foos;
    }

In DB bar_id is the foreign key in foo_bars table

在 DB bar_id 是 foo_bars 表中的外键