Java JPA @ElementCollection List 指定连接列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22075199/
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-13 13:25:15  来源:igfitidea点击:

JPA @ElementCollection List specify join column name

javajpa

提问by jeff

I have the following entity:

我有以下实体:

@Entity
public class Shirt implements Serializable {

    @Id
    @Size(max=9)
    private String id;

    @ElementCollection
    @CollectionTable(name="SHIRT_COLORS")
    @Column(name="color")
    private List<String> colors = new ArrayList<String>();
    ...

The collections table created when I set hibernate to autocreate is

当我将休眠设置为自动创建时创建的集合表是

SHIRT_COLORS
 shirt_id
 color

How do I annotate my Entity so that the join column isn't a concatenation of the entity and pk so that the the table created is:

如何注释我的实体,以便连接列不是实体和 pk 的串联,以便创建的表是:

SHIRT_COLORS
 id
 color

I've tried @JoinColumn but that didn't work. In actuality, the SHIRT_COLORS table in production is managed outside of the app and the column names are already defined.

我试过@JoinColumn 但这没有用。实际上,生产中的 SHIRT_COLORS 表是在应用程序之外管理的,并且已经定义了列名。

采纳答案by wypieprz

Try this:

尝试这个:

@Entity
public class Shirt implements Serializable {

    @Id
    @Size(max=9)
    private String id;

    @ElementCollection
    @CollectionTable(
        name = "SHIRT_COLORS",
        joinColumns=@JoinColumn(name = "id", referencedColumnName = "id")
    )
    @Column(name="color")
    private List<String> colors = new ArrayList<String>();
    ...