使用属性建模manyToMany关系 [英] Modelling a manyToMany relationship with attributes

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

问题描述

我在两个类之间有一个ManyToMany关系,例如类Customer和类Item。顾客可以购买多件商品,不同的顾客可以购买商品。我需要在这种关系中存储额外的信息,例如购买物品的那一天。我想知道这通常是如何在JPA中建模的,因为我不知道如何在代码中表达这一点。我是否必须创建一个新类来模拟关系的所有属性并在其他类之间建立manyToMany关系,或者是更好的方法吗?

I have a ManyToMany relationship between two classes, for instance class Customer and class Item. A customer can buy several items and an item can be bought by different customers. I need to store extra information in this relationship, for example the day when the item was bought. I wonder how is this usually modelled in JPA, cause I'm not sure how to express this in code. Do I have to create a new class to model all the attributes of the relationship and make a manyToMany relationship between the other classes or is a better way to do this?

谢谢

推荐答案

推荐的方法是创建一个新的关联类来存储所需的属性,以及两个一对多关联涉及的双方。

The recommended way is to create a new association class to store the needed attributes, and two one-to-many associations to the two parties involved.

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

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