Hibernate 中的多对一关系
我想在数据库的两个字段之间建立多对一的关系。我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜这种关系如下:
一个ApplicationField有很多设备。
一个设备可以引用多个应用程序字段。
如果这是真的,那么您的映射中存在一些错误。
将设备集替换为以下内容(Id 列中的更改):
将多对一映射更新为:
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):
Update many-to-one mapping as: