如何在 Hibernate 的连接表中实现一对多关系

发布于 2025-01-02 22:52:08 字数 2248 浏览 1 评论 0原文

以下是映射文件:

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

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

发布评论

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

评论(1

莫言歌 2025-01-09 22:52:08

不要在网络上搜索。请阅读参考文档

这是 显示示例的部分一对多单向关联

你的映射没有意义。如果您希望 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.

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