-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: One-to-one mapping problem.
PostPosted: Wed Sep 19, 2007 7:56 am 
Newbie

Joined: Tue Sep 11, 2007 12:17 pm
Posts: 9
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 9:11 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
I am not at all sure about it, but it looks strange, that you are referencing a property in capital letters, since property names by convention start with a lowercase letter.

But your ID Generator does not work, assuming there is more then one thread using it. If to threads requesting a new ID they will receive the same ID. And on inserting the record in the database the one coming second will fail.

regards
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 9:18 am 
Newbie

Joined: Tue Sep 11, 2007 12:17 pm
Posts: 9
schauder wrote:
I am not at all sure about it, but it looks strange, that you are referencing a property in capital letters, since property names by convention start with a lowercase letter.

But your ID Generator does not work, assuming there is more then one thread using it. If to threads requesting a new ID they will receive the same ID. And on inserting the record in the database the one coming second will fail.

regards
Jens


Hi Jens,

Thanks for the response. That is a good point about the Identifier generator and will need to be addressed. However, I am trying to test this manually and even if I provide the InstrumentID by making the generator class="assigned", I still get the same error. It seems that it is trying to save the ESI table before the KSM table has been saved thereby getting a null id in the instrument field of the ESI table.

Point also noted about naming conventions.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.