这种多对多映射方法在 Hibernate 中可行吗?
我有三个表:item、node 和 item_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
}
Item 和 Node 已经映射并自行正常工作。
我知道这不是处理多对多关系的常用方法,但由于糟糕的延迟初始化异常,我在基于标准集/包的方法中遇到了相当烦人的问题,并且无法加载这些急切地收集集合,因为它们将包含大量数据(数据库表中的数十万行)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你当然可以。查看此问题的另一种方法是想象 ItemNode 本身可能拥有一个属性(例如,“sequence_no”)。因此,ItemNode 本身可以有效地成为一个实体,而不仅仅是 Item 和 Node 之间的链接。
请参阅 Hibernate 测试套件中的示例:
https://github.com/hibernate/hibernate-core/tree/master/hibernate-core/src/test/java/org/hibernate/test/manytomanyassociationclass
有个人偏好,但不是最佳实践。有些人意识到事情可能会发生变化,他们为此做好了准备:他们将关系映射为一个实体。然后,有人认为这种关系也应该被视为OO模型中的关系。
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
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.