问题
How to set the column name of the foreign key when setting up a one-to-many relationship in JPA?
I would like to change the name of "items_id" to "item_id"
@OneToMany
private List<Item> items;
I tried the following annotations with no success:
- @JoinColumn(name="item_id") // join table is not created
- @Column(name="item_id") // no effect
回答1:
You want to override the mappings of the default values of the join table, so the @JoinTable
annotation is the one to use. You want to override the name of the inverseJoinColumn from items_id
to item_id
:
@OneToMany
@JoinTable(inverseJoinColumns=@JoinColumn(name="item_id"))
List<Item> items;
@OneToMany
and @JoinColumn
makes a different mapping, a join table is not created and a foreign key is created in the referenced entity table(in the Item table in your case).
@Column
is used to override the names for entity attributes, not for relationships.
来源:https://stackoverflow.com/questions/49700245/jpa-onetomany-column-name