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

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

问题描述

我有实体类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?

干杯

推荐答案

据我所知,这是的无法以这种方式映射它,你必须为切换到一对 - 许多和许多一对一的关系,在中间的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.


有关这种缺乏evolutivity的,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天全站免登陆