简体   繁体   English

JPA 使用主键而不是外键生成的连接表

[英]JPA Join table generated with primary keys instead of foreign keys

Well I have a OneToMany unidirectionnal relationship between TeamEntity and ContestTypeEntity .好吧,我在TeamEntityContestTypeEntity之间有 OneToMany 单向关系。

So in my TeamEntity I have the following:所以在我的TeamEntity中,我有以下内容:

@OneToMany(targetEntity = ContestTypeEntity.class)
@JoinTable(name="team_contest_type")
private Set<ContestTypeEntity> contestTypes = new HashSet<>();

ContestTypeEntity doesn't know about the relation. ContestTypeEntity不知道这种关系。

Normally, JPA should generate a join table team_contest_type with two foreign keys: team_entity_id and contest_types_id .通常,JPA 应该生成一个包含两个外键的连接表team_contest_typeteam_entity_idcontest_types_id

But in the join table actually generated, those are primary keys, so I'm not able to insert not unique values... And that's what I want to do.但是在实际生成的连接表中,这些是主键,所以我无法插入不唯一的值......这就是我想要做的。 How can I fix this?我怎样才能解决这个问题?

Use @JoinColumn instead of @JoinTable .使用@JoinColumn而不是@JoinTable And remember that when working with JPA/Hibernate/ORM you are working with object references, not with table keys.请记住,在使用 JPA/Hibernate/ORM 时,您使用的是 object 引用,而不是表键。 This can help you not to use unidirectional with OneToMany: https://thorben-janssen.com/best-practices-many-one-one-many-associations-mappings/#Don8217t_use_unidirectional_one-to-many_associations这可以帮助您不要将单向与 OneToMany 一起使用: https://thorben-janssen.com/best-practices-many-one-one-many-associations-mappings/#Don8217t_use_unidirectional_one-to-many_associations

Ok so a ManyToMany relation with a Set on both sides solved my problem.好的,所以双方都有一个 Set 的 ManyToMany 关系解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM