多对多关系的属性(休眠) [英] Attributes on Many-to-Many relationships (Hibernate)

查看:55
本文介绍了多对多关系的属性(休眠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体类 A 和 C.它们映射表 tblA 和 tblC 并且它们之间具有多对多关系,而 tblB 在它们之间进行映射.tblB 包含 A_ID、C_ID 和 SetDate,最后一个是设置的日期,因此是关系的一个属性.我的问题是,我如何最好地映射这个属性?目前它们未映射,如下所示:

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:

答:

@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;

我应该如何从中获得 tblB.SetDate?

How should I get tblB.SetDate out of this?

干杯

尼克

推荐答案

据我所知,不可能这样映射,你必须切换到 One-To- 多对一关系,中间是你的 B.您的日期将是 B 的一个属性.

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.

由于缺乏进化性,Hibernate 文档建议一般避免多对多,并从一开始就使用这两种关系.

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.

这篇关于多对多关系的属性(休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆