Hibernate 如何在中间表中添加列

发布于 2024-12-01 12:39:44 字数 505 浏览 1 评论 0原文

详细信息如下。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒数 2024-12-08 12:39:44

做到这一点的最佳方法是使用元素集合。

@Entity
public class Exam {
    @CollectionOfElements(fetch = FetchType.LAZY)
    @JoinTable(name = "EXAM_CATEGORY", joinColumns = @JoinColumn(name = "FK_EXAM"))
    public Set<CategoryEntry> getCategories() {
        return this.categories;
    }
}

@Embeddable
public class CategoryEntry{

    private Category category;
    private User user;

    @ManyToOne
    @JoinColumn(name = "FK_CATEGORY", nullable = false)
    public Category getCategory() {
        return this.category;
    }

    @ManyToOne
    @JoinColumn(name = "FK_USER", nullable = false)
    public User getUser() {
        return this.user;
    }

// ... setters and such

}

@Entity
public class Category {
}

这种方法比将其映射为实体更清晰,因为从逻辑上讲,它本来就不是一个实体。

然后,您可以在 Exam 中添加一些其他方法来获取所有类别(不包括用户)或将其作为地图检索,等等。如果需要,您可以通过封装完全隐藏此中间对象。

The best way to do this is to use a collection of elements.

@Entity
public class Exam {
    @CollectionOfElements(fetch = FetchType.LAZY)
    @JoinTable(name = "EXAM_CATEGORY", joinColumns = @JoinColumn(name = "FK_EXAM"))
    public Set<CategoryEntry> getCategories() {
        return this.categories;
    }
}

@Embeddable
public class CategoryEntry{

    private Category category;
    private User user;

    @ManyToOne
    @JoinColumn(name = "FK_CATEGORY", nullable = false)
    public Category getCategory() {
        return this.category;
    }

    @ManyToOne
    @JoinColumn(name = "FK_USER", nullable = false)
    public User getUser() {
        return this.user;
    }

// ... setters and such

}

@Entity
public class Category {
}

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.

淑女气质 2024-12-08 12:39:44

这是涉及三个表(三个外键)的多对多关系的情况。这在 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.

凯凯我们等你回来 2024-12-08 12:39:44

您可以在考试课程中使用地图字段。请参阅这篇文章

You could use a Map field in your Exam class. See this post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文