这种多对多映射方法在 Hibernate 中可行吗?

发布于 2024-10-14 21:03:53 字数 1016 浏览 1 评论 0原文

我有三个表:itemnodeitem_node 作为链接表,其中当然包含项目和节点的 ID。

我可以映射链接表(item_node),使相应的类 ItemNode 以某种方式保存对 Item 和 Node 类的引用吗?是否有任何已知的做法可以在 m:n 关系中映射链接表?

我会想象这样的事情:

<class name="test.model.ItemNode" table="ITEM_NODE">
    <composite-id name="ID" class="test.model.INCompID">
        <key-property name="item" column="ITEM_ID" />
        <key-property name="node" column="NODE_ID"/>
    </composite-id>
</class>

其中 INCompID 是复合 ID 的类:

public class INCompID {

private Item item;
private Node node;

//getters, setters and overriden 
//equals() and hashCode() methodes
}

ItemNode 已经映射并自行正常工作。

我知道这不是处理多对多关系的常用方法,但由于糟糕的延迟初始化异常,我在基于标准集/包的方法中遇到了相当烦人的问题,并且无法加载这些急切地收集集合,因为它们将包含大量数据(数据库表中的数十万行)。
Spring AOP 用于事务管理。 OpenSessionInViewFilter/Interceptor 似乎对这个特定问题也没有帮助,所以......

I've got three tables: item, node and item_node as linking table, which of course contains IDs of item and node.

Can I map the linking table (item_node) in such a way that corresponding class ItemNode holds somehow references to Item and Node classes? Is there any known practice for mapping link tables in m:n relationships?

I would imagine something like this:

<class name="test.model.ItemNode" table="ITEM_NODE">
    <composite-id name="ID" class="test.model.INCompID">
        <key-property name="item" column="ITEM_ID" />
        <key-property name="node" column="NODE_ID"/>
    </composite-id>
</class>

where INCompID is class for composite ID:

public class INCompID {

private Item item;
private Node node;

//getters, setters and overriden 
//equals() and hashCode() methodes
}

Item and Node are already mapped and working properly by themselves.

I know that this is not usual approach for dealing with many-to many relationships, but I'm having quite annoying problem with standard set/bag-based approach, because of the lousy lazy initialization exception, and there is no way to load these collections eagerly, because they would contain enormous amount of data (hundreds of thousands of rows in DB tables).
Spring AOP is used for transaction management. OpenSessionInViewFilter/Interceptor doesn't seem to be helpful for this particular problem either, so...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

眼趣 2024-10-21 21:03:53

我可以映射链接表(item_node),使相应的类 ItemNode 以某种方式保存对 Item 和 Node 类的引用吗?

你当然可以。查看此问题的另一种方法是想象 ItemNode 本身可能拥有一个属性(例如,“sequence_no”)。因此,ItemNode 本身可以有效地成为一个实体,而不仅仅是 Item 和 Node 之间的链接。

请参阅 Hibernate 测试套件中的示例:

https://github.com/hibernate/hibernate-core/tree/master/hibernate-core/src/test/java/org/hibernate/test/manytomanyassociationclass

是否有任何已知的做法可以在 m:n 关系中映射链接表?

有个人偏好,但不是最佳实践。有些人意识到事情可能会发生变化,他们为此做好了准备:他们将关系映射为一个实体。然后,有人认为这种关系也应该被视为OO模型中的关系。

Can I map the linking table (item_node) in such a way that corresponding class ItemNode holds somehow references to Item and Node classes?

You sure can. Another way to view this is imagining that ItemNode may hold a property by itself (for instance, "sequence_no"). Thus, ItemNode can effectively be an Entity by itself, instead of just a link between Item and Node.

See this example from the Hibernate test suite:

https://github.com/hibernate/hibernate-core/tree/master/hibernate-core/src/test/java/org/hibernate/test/manytomanyassociationclass

Is there any known practice for mapping link tables in m:n relationships?

There are personal preferences, but not best practices. There are people that realize that things can change, and they get prepared for it: they map the relationship as an entity. Then, there are people who believe that this relationship should be viewed just as a relationship in the OO model too.

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