Java – JPA @ elementcollection list specifies the connection column name

I have the following entities:

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

When I set hibernate to autocreate, the set table created is

SHIRT_COLORS
 shirt_id
 color

How to annotate my entity so that the connection column is not the connection between entity and PK, so that the created table is:

SHIRT_COLORS
 id
 color

I tried @ joincolumn, but I didn't work In fact, shirt in production_ The colors table is managed outside the application, and the column names have been defined

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>();
    ...
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>