简体   繁体   English

JPA @ManyToMany关系在联接表上没有主复合键

[英]JPA @ManyToMany relationship without primary compound key on join table

I have an entity A with has am:n relationship to an entity B, however for every A there can not only be multiple B, but in addition multiple of the exact same B. 我有一个与实体B具有am:n关系的实体A,但是对于每个A来说,不仅可以有多个B,而且可以有多个完全相同的B。

I tried defining the relation like this: 我试图定义这种关系:

@Entity
class A {
    @Id
    public Long id;

    @ManyToMany
    public List<B> bs = new ArrayList<B>();
}

and

@Entity
class B {
    @Id
    public Long id;
}

which gives me the following generated DDL for the join table: 这为联接表提供了以下生成的DDL:

create table a_b (
a_id                         bigint not null,
b_id                        bigint not null,
constraint pk_a_b primary key (a_id, b_id))
;

The DDL is fine except for the primary compound key, because this means one A can only have one specific B a single time. 除主复合键外,DDL很好,因为这意味着一个A一次只能有一个特定的B。 I am doing this on play framework 2.0 with ebean persistence. 我在具有ebean持久性的play framework 2.0上执行此操作。 Any hints? 有什么提示吗?

You can't use a @ManyToMany, as by its definition it won't allow the duplications you want. 您不能使用@ManyToMany,因为根据其定义,它不会允许您想要的重复项。

What you want is either a list of elements (like JPA @ElementCollection ) or to keep the tables unrelated in the model and use a Query to retrieve the B associated to A. 您想要的是元素列表(例如JPA @ElementCollection )或使表在模型中不相关,并使用查询来检索与A关联的B。

I would link to ebeans documentation, but with it being a PDF... :( 我会链接到ebeans文档,但是它是PDF ... :(

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

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