Hibernate 如何在中间表中添加列
详细信息如下。
我有 2 个具有一对多关系的实体表。
首先,Exam
类有很多Category
类。 Hibernate 生成此表
______________
Exam
- Id
- Name
- Category
______________
Category
- Id
- Name
______________
Exam_Category
- Exam_Id
- Category_Id
我需要在 Exam_Category
表中添加额外的列 例如:
______________
Exam_Category
- Exam_Id
- Category_Id
* User_Id
我将如何实现这一目标。如果有的话,我将如何获得 user_id
值,因为 Exam_Category
未公开。谢谢
Here's the details.
I have 2 entity tables with one to many relationship.
First, Exam
class has many Category
class. Hibernate generates this tables
______________
Exam
- Id
- Name
- Category
______________
Category
- Id
- Name
______________
Exam_Category
- Exam_Id
- Category_Id
I need to add an extra column in Exam_Category
table
ex:
______________
Exam_Category
- Exam_Id
- Category_Id
* User_Id
How am i going to accomplish this. if ever, how will i also get the user_id
value because the Exam_Category
is not exposed. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
做到这一点的最佳方法是使用元素集合。
这种方法比将其映射为实体更清晰,因为从逻辑上讲,它本来就不是一个实体。
然后,您可以在
Exam
中添加一些其他方法来获取所有类别(不包括用户)或将其作为地图检索,等等。如果需要,您可以通过封装完全隐藏此中间对象。The best way to do this is to use a collection of elements.
This approach is cleaner than mapping it as an entity, since, logically, it is not an entity to begin with.
Then you can add some other methods in
Exam
to get all the categories (without the users) or to retrieve it as a map, or whatever. You can completely hide this intermediate object through encapsulation, if that is required.这是涉及三个表(三个外键)的多对多关系的情况。这在 Hibernate 中无法通过简单的映射来实现。您必须扩展实体类的定义以支持“中间”表上的自定义主键列·
查看此参考。
This is the case of many-to-many relationship with three tables (three foreign keys) involved. This cannot be achieved in Hibernate by simple mapping. You will have to extend your definition of Entity classes to support custom primary key column on the "middle" table·
Check out this reference.
您可以在考试课程中使用地图字段。请参阅这篇文章
You could use a Map field in your Exam class. See this post