休眠与联接表的一对多关系,并在联接表中添加列

编程入门 行业动态 更新时间:2024-10-27 03:41:56
本文介绍了休眠与联接表的一对多关系,并在联接表中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找在两个表之间具有@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.

更多推荐

休眠与联接表的一对多关系,并在联接表中添加列

本文发布于:2023-10-28 18:47:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537466.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!