Attributes on Many-to-Many relationships (Hibernate)

大憨熊 提交于 2020-01-10 19:27:38

问题


I have entity classes A and C. They are mapping the tables tblA and tblC and have a many-to-many relationship between them, with tblB to map between them. tblB contains A_ID, C_ID and SetDate, the last one being the date it was set, thus an attribute to the relationship. My question is, how do I best map in this attribute? At the moment they're unmapped, like this:

A:

@ManyToMany(targetEntity=C.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
@JoinTable(name="tblB", joinColumns=@JoinColumn(name="A_ID"), inverseJoinColumns=@JoinColumn(name="C_ID") )
private Collection<C> Cs;

C:

@ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "Cs", targetEntity = A.class )
private Collection<A> As;

How should I get tblB.SetDate out of this?

Cheers

Nik


回答1:


From what I know, it is not possible to map it this way, you have to switch to One-To-Many and a Many-To-One relationships, with your B in the middle. Your date will be an attribute of B.


For this lack of evolutivity, the Hibernate documentation recommends to avoid the Many-To-Many in general, and use the two relationships from the beginning.




回答2:


See

@ManyToMany Hibernate Question (can add extra field?)

And

how to make a composite primary key (java persistence annotation)

regards,



来源:https://stackoverflow.com/questions/1476600/attributes-on-many-to-many-relationships-hibernate

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