JPA OneToMany column name

爱⌒轻易说出口 提交于 2021-01-03 06:28:05

问题


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:

  1. @JoinColumn(name="item_id") // join table is not created
  2. @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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!