[英]JPA Join table generated with primary keys instead of foreign keys
好吧,我在TeamEntity
和ContestTypeEntity
之間有 OneToMany 單向關系。
所以在我的TeamEntity
中,我有以下內容:
@OneToMany(targetEntity = ContestTypeEntity.class)
@JoinTable(name="team_contest_type")
private Set<ContestTypeEntity> contestTypes = new HashSet<>();
ContestTypeEntity
不知道這種關系。
通常,JPA 應該生成一個包含兩個外鍵的連接表team_contest_type
: team_entity_id
和contest_types_id
。
但是在實際生成的連接表中,這些是主鍵,所以我無法插入不唯一的值......這就是我想要做的。 我怎樣才能解決這個問題?
使用@JoinColumn而不是@JoinTable 。 請記住,在使用 JPA/Hibernate/ORM 時,您使用的是 object 引用,而不是表鍵。 這可以幫助您不要將單向與 OneToMany 一起使用: https://thorben-janssen.com/best-practices-many-one-one-many-associations-mappings/#Don8217t_use_unidirectional_one-to-many_associations
好的,所以雙方都有一個 Set 的 ManyToMany 關系解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.