简体   繁体   English

休眠+春季+保持一对一关系+空外键

[英]Hibernate + Spring + persist one-to-one relation + empty foreign key

I am trying to persist a one-to-one relation with Spring 1.2.8 and Hibernate 3.2.7ga but the foreign key is always empty. 我试图与Spring 1.2.8Hibernate 3.2.7ga保持一对一的关系,但外键始终为空。

But let me first give you a short introduction: 但首先让我简要介绍一下:

I have two Entities called "ClientDomain" (Web-Domain) and "Measurement". 我有两个称为“ ClientDomain”(Web域)和“ Measurement”的实体。

Hibernate Mapping Files: 休眠映射文件:

ClientDomain.hbm.xml: ClientDomain.hbm.xml:

<hibernate-mapping package="statistics.jobs.domain">

<class name="ClientDomain" table="domains" lazy="false">

<id column="id" name="id" type="int">
  <generator class="native"/>
</id>

<property name="version" not-null="false"/>
<property name="url"/>
<property name="etrackerApiUser" column="etracker_api_user"/>
<property name="etrackerApiKey" column="etracker_api_key"/>
<property name="etrackerUsername" column="etracker_username"/>
<property name="etrackerPassword" column="etracker_password"/>

<set 
  name="measurements" 
  table="measurements" 
  lazy="false" 
  cascade="merge,save-update" 
  inverse="true">
  <key column="domain" />
  <one-to-many class="Measurement"/>
</set>   

</class>

</hibernate-mapping>

Measurement.hbm.xml: Measurement.hbm.xml:

<hibernate-mapping package="statistics.jobs.domain">

<class name="Measurement" table="measurements" lazy="false">

<id column="id" name="id" type="int">
  <generator class="native"/>
</id>

<property name="version" not-null="false"/>
<property name="time" type="java.util.Date"/>
<property name="numberOfDocuments" column="documents"/>

<one-to-one 
  name="domain" 
  class="ClientDomain" 
  cascade="merge,save-update"
/>   

</class>

</hibernate-mapping>

I am getting some statistics from various source for a domain and then want to persist them. 我从某个域的各种来源获取一些统计信息,然后想要保留它们。 In this way I have a history for a domain. 这样,我就拥有一个域的历史记录。

The ClientDomain objects are already persistent. ClientDomain对象已经是持久的。

A Measurement object is created like so: 这样创建一个Measurement对象:

Measurement measurement = new Measurement();
measurement.setDomain(domain);
measurement.setTime(new Date());
measurement.setNumberOfDocuments(22222);
measurementManager.insertMeasurement(measurement);

insertMeasurement(Measurement measurement): insertMeasurement(测量尺寸):

public void insertMeasurement(Measurement measurement) {    
  getHibernateTemplate().saveOrUpdate(measurement);
}

This is the result: 结果如下:

+----+---------+-----------+---------------------+--------+
| id | version | documents | time                | domain |
+----+---------+-----------+---------------------+--------+
| 82 |    NULL |     22222 | 2009-11-16 14:28:32 |   NULL |
| 83 |    NULL |     22222 | 2009-11-16 14:28:33 |   NULL |
| 84 |    NULL |     22222 | 2009-11-16 14:28:34 |   NULL |
+----+---------+-----------+---------------------+--------+

I already checked if the domain instance is correct. 我已经检查了域实例是否正确。 Everything is as it should be. 一切都应有。

So what am I doing wrong? 那我在做什么错? Why is the domain foreign key not saved along with the measurement? 为什么域外键没有与测量一起保存?

What I tried so far: 到目前为止我尝试过的是:

  • calling persist instead of saveOrUpdate (also changed cascaded to persist) 调用persist而不是saveOrUpdate(也更改为级联为persist)
  • wrapping the saveOrUpdate in a session.beginTransaction() and session.getTransaction().commit() 将saveOrUpdate包装在session.beginTransaction()和session.getTransaction()。commit()中

But so far I had no luck. 但是到目前为止,我还没有运气。

YOu have a one-to-many relationship from ClientDomain to measurement. 从ClientDomain到度量,您之间存在一对多的关系。 But you have a one-to-one relationship from Measurement to ClientDomain. 但是,从Measurement到ClientDomain,您之间存在一对一的关系。

Make the relationship in Measurement to ClientDomain a many-to-one relationship. 使Measurement与ClientDomain之间的关系成为多对一关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM