Hibernate 中的多对一关系

发布于 2024-11-19 00:03:43 字数 2242 浏览 6 评论 0原文

我想在数据库的两个字段之间建立多对一的关系。我正在使用 PostgreSQL 数据库和 Hibernate。这些表是 ApplicationField 和 Device。第一个有 2 列:AppFieldId 和 Name。第二个包含 NodeId、Description 和 AppFieldId。 ApplicationField 的休眠映射是:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 05-may-2011 13:21:52 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cartif.database.ApplicationField" table="APPLICATIONFIELD">
    <id name="iAppFieldId" column="applicationfieldid" type="java.lang.Integer">
        <generator class="sequence">
            <param name="sequence">s_applicationfield</param>
        </generator>
    </id>
    <property column="name" lazy="false" name="name" type="java.lang.String"/>
    <set name="devices">
        <key column="appfieldid" />
        <one-to-many column="nodeid" class="com.cartif.zigbee.device.Device"/>
    </set>
</class>
</hibernate-mapping>

对于设备:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                               "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 07-abr-2011 13:29:19 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cartif.zigbee.device.Device" table="Device">
    <id column="nodeid" name="iIdentifier" type="java.lang.Integer"/>
    <property column="description" generated="never" lazy="false" name="description" type="java.lang.String"/>
    <set name="appField">
        <key column="nodeid"/>
        <many-to-one column="appfieldid" class="com.cartif.database.ApplicationField"/>
    </set>
</class>
</hibernate-mapping>

在 Java 类上,我在 ApplicationField 类上有一个 List devices,在 Device 类上有一个 ApplicationField appField。但是,当我尝试创建 sessionFactory 时,出现如下异常:

org.xml.sax.SAXParseException: Attribute "column" must be declared for element type "one-to-many".

我应该如何处理表之间的关系?

多谢!!

I would like to establish a relationship many-to-one between two fields on database. I am using PostgreSQL database and Hibernate. The tables are ApplicationField and Device. The first one has 2 columns: AppFieldId and Name. The second one has NodeId, Description and AppFieldId. The hibernate mapping for ApplicationField is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 05-may-2011 13:21:52 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cartif.database.ApplicationField" table="APPLICATIONFIELD">
    <id name="iAppFieldId" column="applicationfieldid" type="java.lang.Integer">
        <generator class="sequence">
            <param name="sequence">s_applicationfield</param>
        </generator>
    </id>
    <property column="name" lazy="false" name="name" type="java.lang.String"/>
    <set name="devices">
        <key column="appfieldid" />
        <one-to-many column="nodeid" class="com.cartif.zigbee.device.Device"/>
    </set>
</class>
</hibernate-mapping>

And for Device:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                               "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 07-abr-2011 13:29:19 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cartif.zigbee.device.Device" table="Device">
    <id column="nodeid" name="iIdentifier" type="java.lang.Integer"/>
    <property column="description" generated="never" lazy="false" name="description" type="java.lang.String"/>
    <set name="appField">
        <key column="nodeid"/>
        <many-to-one column="appfieldid" class="com.cartif.database.ApplicationField"/>
    </set>
</class>
</hibernate-mapping>

On Java classes I have a List devices on ApplicationField class and ApplicationField appField on Device class. However, when I try to create a sessionFactory, I obtain an exception like:

org.xml.sax.SAXParseException: Attribute "column" must be declared for element type "one-to-many".

How should I do the relationship between the tables?

Thanks a lot!!

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

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

发布评论

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

评论(1

离不开的别离 2024-11-26 00:03:43

我猜这种关系如下:

一个ApplicationField有很多设备。
一个设备可以引用多个应用程序字段。

如果这是真的,那么您的映射中存在一些错误。

将设备集替换为以下内容(Id 列中的更改):

<set name="devices">
        <key column="applicationfieldid" />
        <one-to-many class="com.cartif.zigbee.device.Device"/>
</set>   

将多对一映射更新为:

<many-to-one name="appField" column="applicationfieldid" class="com.cartif.database.ApplicationField"/>

I guess the relationship is as follow:

One ApplicationField has many Devices.
One Device can refer to Many ApplicationField.

If It is true, then there are some mistakes in your mapping.

Replace devices set with following (change in Id column):

<set name="devices">
        <key column="applicationfieldid" />
        <one-to-many class="com.cartif.zigbee.device.Device"/>
</set>   

Update many-to-one mapping as:

<many-to-one name="appField" column="applicationfieldid" class="com.cartif.database.ApplicationField"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文