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.
But let me first give you a short introduction:
I have two Entities called "ClientDomain" (Web-Domain) and "Measurement".
Hibernate Mapping Files:
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:
<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 persiste开发者_Python百科nt.
A Measurement object is created like so:
Measurement measurement = new Measurement();
measurement.setDomain(domain);
measurement.setTime(new Date());
measurement.setNumberOfDocuments(22222);
measurementManager.insertMeasurement(measurement);
insertMeasurement(Measurement measurement):
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)
- wrapping the saveOrUpdate in a session.beginTransaction() and session.getTransaction().commit()
But so far I had no luck.
YOu have a one-to-many relationship from ClientDomain to measurement. But you have a one-to-one relationship from Measurement to ClientDomain.
Make the relationship in Measurement to ClientDomain a many-to-one relationship.
精彩评论