休眠与联接表的一对多关系,并在联接表中添加列 [英] Hibernate one to many relationship with join table with addition columns in join table

查看:70
本文介绍了休眠与联接表的一对多关系,并在联接表中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在两个表之间具有@OneToMany关系并在联接表中具有额外属性的方法,但我找不到周围有用的示例.抱歉,这听起来很la脚,但有什么可以建议我这样做的好方法.

I am looking for a way to have a @OneToMany relation between two tables and having extra attributes in the join table and I am not able to find much useful examples around. Sorry if this sounds lame but can any suggest me a good way for it.

推荐答案

如果联接表中还有其他列,则它不再是联接表,您需要一种方法来获取和设置这些列中的值.因此答案很简单:该表需要映射为一个实体.

If you have additional columns in the join table, it's not a join table anymore, and you need a way to get and set values in these columns. So the answer is simple: the table needs to be mapped as an entity.

例如,假设您有一个Person实体和一个Address实体.这个人有几个地址.现在,我们假设必须对每个地址进行分类:家庭住址,专业地址等.要映射地址的类别,您需要一个实体:

For example, let's say you have a Person entity and an Address entity. And the person has several addresses. Now let's say each address must be categorized: home address, professional address, etc. To be able to map the category of the address, you need an entity:

Person 1 ---> N CategorizedAddress 1 ---> 1 Address

CategorizedAddress将具有地址类型的属性,以及包含类别的属性.

And CategorizedAddress would have a property of type Address, and a property containing the category.

这篇关于休眠与联接表的一对多关系,并在联接表中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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