質問

I have an entity A with has a m: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.

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:

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. I am doing this on play framework 2.0 with ebean persistence. Any hints?

役に立ちましたか?

解決

You can't use a @ManyToMany, as by its definition it won't allow the duplications you want.

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.

I would link to ebeans documentation, but with it being a PDF... :(

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top