如何在 Hibernate 的连接表中实现一对多关系
以下是映射文件:
BusinessCard.hbm.xml
<hibernate-mapping>
<class name="com.hibernate.BusinessCard" table="BUSINESSCARD">
<id length="4" name="id" type="int">
<column length="4" name="ID"/>
<generator class="increment"/>
</id>
<property generated="never" lazy="false" name="name" type="java.lang.String">
<column length="50" name="NAME"/>
</property>
<property generated="never" lazy="false" name="description" type="java.lang.String">
<column length="250" name="DESCRIPTION"/>
</property>
</class>
</hibernate-mapping>
BusinessGup.hbm.xml
<hibernate-mapping>
<class name="com.hibernate.BusinessGroup" table="BUSINESSGROUP">
<id name="id" type="int">
<column name="ID" length="4"/>
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="25"/>
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" length="250"/>
</property>
</class>
</hibernate-mapping>
BusinessContact.hbm.xml (加入我想要进行一对多映射的表)
<hibernate-mapping>
<class name="com.hibernate.BusinessContact" table="BUSINESSCONTACT">
<id name="id" type="int">
<column name="ID" length="4"/>
<generator class="increment" />
</id>
<property name="businessId" type="java.lang.Integer">
<column name="BUSINESSID" length="4"/>
</property>
<property name="groupId" type="java.lang.Integer">
<column name="GROUPID" length="4"/>
</property>
</class>
</hibernate-mapping>
因此,在 BUSINESSCONTACT 表上,我尝试在名片和业务组之间进行一对多映射。不幸的是,尽管在网上搜索了解决方案,但我无法做到这一点。有人能帮我解决这个问题吗?
谢谢..
here are mapping files :
BusinessCard.hbm.xml
<hibernate-mapping>
<class name="com.hibernate.BusinessCard" table="BUSINESSCARD">
<id length="4" name="id" type="int">
<column length="4" name="ID"/>
<generator class="increment"/>
</id>
<property generated="never" lazy="false" name="name" type="java.lang.String">
<column length="50" name="NAME"/>
</property>
<property generated="never" lazy="false" name="description" type="java.lang.String">
<column length="250" name="DESCRIPTION"/>
</property>
</class>
</hibernate-mapping>
BusinessGup.hbm.xml
<hibernate-mapping>
<class name="com.hibernate.BusinessGroup" table="BUSINESSGROUP">
<id name="id" type="int">
<column name="ID" length="4"/>
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="25"/>
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" length="250"/>
</property>
</class>
</hibernate-mapping>
BusinessContact.hbm.xml (Join table where I want to do one to many mapping)
<hibernate-mapping>
<class name="com.hibernate.BusinessContact" table="BUSINESSCONTACT">
<id name="id" type="int">
<column name="ID" length="4"/>
<generator class="increment" />
</id>
<property name="businessId" type="java.lang.Integer">
<column name="BUSINESSID" length="4"/>
</property>
<property name="groupId" type="java.lang.Integer">
<column name="GROUPID" length="4"/>
</property>
</class>
</hibernate-mapping>
So, on BUSINESSCONTACT table I am trying to do one-to-many mapping between businesscard and businessgroup. Unfortunately I couldnT do that despite searching solution on the web. Can anyone help me about this issue?
Thx..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在网络上搜索。请阅读参考文档。
这是 显示示例的部分一对多单向关联。
你的映射没有意义。如果您希望 BusinessGroup 和 BusinessCard 之间存在一对多关联,则 BusinessGroup 类应该具有 BusinessCard 的集合。
不应该有 BusinessContact 类,因为 BUSINESSCONTACT 表只是一个连接表,而不是一个实体。它的唯一用途是保持其他实体之间的关联,因此它完全由关联管理。
Don't search on the web. Read the reference documentation instead.
Here's the section showing an example of a one-to-many unidirectional association.
Your mapping doesn't make sense. If you want a one-to-many association between BusinessGroup and BusinessCard, then the BusinessGroup class should have a collection of BusinessCards.
There shouldn't be a class BusinessContact, since the BUSINESSCONTACT table is just a join table, and not an entity. Its only use is to hold the association between the other entities, and it's thus completely managed by the association.