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".

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

非常感谢!!

4

1 回答 1

2

我想关系如下:

一个 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"/>
于 2011-07-07T06:49:22.143 回答