Hi,
I need help with a problem I am facing. I am currently getting an error when attempting to save an object with a one-to-one mapping. Below is the exception I am getting:
Code:
Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: attempted to assign id from null one-to-one property: KMS
My Mapping files are as follows:
Code:
<?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 Sep 11, 2007 3:40:08 PM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.t.KMS" table="KMS">
<id name="instrument" type="big_decimal" >
<column name="INSTRUMENT" precision="22" scale="0" not-null="true"/>
<generator class="com.t.IDGenerator" />
</id>
<property name="tableId" type="string">
<column name="TABLE_ID" length="20" not-null="true" />
</property>
<property name="creator" type="big_decimal">
<column name="CREATOR" precision="22" scale="0" not-null="true" />
</property>
<property name="createTime" type="date">
<column name="CREATE_TIME" length="7" not-null="true" />
</property>
<property name="instrgroup" type="string">
<column name="INSTRGROUP" length="20" />
</property>
<property name="actor" type="string">
<column name="ACTOR" length="30" />
</property>
<one-to-one name="ESI" class="com.t.ESI" cascade="all" />
</class>
<class name="com.t.ESI"
table="ESI">
<id name="instrument" column="INSTRUMENT" >
<generator class="foreign">
<param name="property">KMS</param>
</generator>
</id>
<one-to-one name="KMS" class="com.t.KMS" constrained="true" />
<property name="wkn" type="string">
<column name="WKN" length="12" />
</property>
<property name="updateUser" type="big_decimal">
<column name="UPDATE_USER" precision="22" scale="0"
not-null="true" />
</property>
<property name="updateTime" type="date">
<column name="UPDATE_TIME" length="7" not-null="true" />
</property>
</class>
</hibernate-mapping>
The IDGenerator is simply a class that returns an ID of type BIG_DECIMAL.
Code:
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;
public class IDGenerator implements IdentifierGenerator{
public Serializable generate(SessionImplementor sessionImplementor, Object object)
throws HibernateException {
Session session=sessionImplementor.getFactory().openSession();
String SQL_QUERY="SELECT MAX(instrument)+1 AS instrument FROM KMS "
+ " WHERE instrument > 100000000 AND instrument < 150000000";
Query query=session.createQuery(SQL_QUERY);
BigDecimal instrumentId=(BigDecimal)query.iterate().next();
return instrumentId;
}
}
Could someone please point me in the right direction as to what is wrong?