JPA @ElementCollection List specify join column name

java, jpa

Solution

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>();
    ...

Problem

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.

Original source