Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA @ElementCollection List specify join column name

Tags:

java

jpa

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:

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.

like image 585
jeff Avatar asked Feb 27 '14 16:02

jeff


1 Answers

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>();
    ...
like image 150
wypieprz Avatar answered Nov 01 '22 14:11

wypieprz