-->
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.  [ 2 posts ] 
Author Message
 Post subject: problem using <sql-insert> with native id generator
PostPosted: Thu Jun 02, 2005 5:15 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Hibernate version:3.0.5

I'm using the <sql-insert> element in my mapping file to get sql server to insert a GUID into a column in my table, e.g.

Code:
<sql-insert>INSERT INTO mytable(...,myguid) VALUES(?,?,...,NEWID())</sql-insert>


On this table the id has a native generator. As far as I can tell, this causes Hibernate to put something like 'select scope_identity()' on the end of the insert statement, which lets it get back the generated ID without an extra select after the insert (I don't fully understand this, I'm just guessing). But this doesn't happen when I use <sql-insert>, and the Hibernate code throws a NullPointer because it expects the generated ID to come back from the insert statement, and it doesn't.

You'll see that I've included output from the BasicEntityPersister showing the static SQL statements it uses with my table, and there is an 'Insert 0:' SQL statement and an 'Identity Insert:' statement, which are identical. If I don't use <sql-insert>, then the 'Identity Insert:' statement has 'select scope_identity()' on the end.

It seems like this 'select scope_identity()' needs to be on the end of the 'Identity Insert' statement, but isn't. Is this a bug, or am I doing something wrong??

Thanks in advance for you help.


Mapping documents:
Code:
<class
    name="org.waterford.db.data.ObjScore"
    table="ObjScore"
   mutable="true"
   lazy="false"
>
    <id
        name="objscoreid"
        type="java.lang.Integer"
        column="objscoreid"
    >
        <generator class="native" />
    </id>

    <property
        name="objtreeid"
        type="java.lang.String"
        column="objtreeid"
        length="32"
    />
    <property
        name="objdate"
        type="java.sql.Timestamp"
        column="objdate"
        length="23"
    />
    <property
        name="performance"
        type="java.lang.Integer"
        column="performance"
        length="10"
    />
    <property
        name="correct"
        type="java.lang.Integer"
        column="correct"
        length="10"
    />
    <property
        name="total"
        type="java.lang.Integer"
        column="total"
        length="10"
    />
    <property
        name="avgtime"
        type="java.lang.Integer"
        column="avgtime"
        length="10"
    />
    <property
        name="verbosescore"
        type="java.lang.Integer"
        column="verbosescore"
        length="10"
    />
    <property
        name="objscoreguid"
        type="java.lang.String"
        column="objscoreguid"
        not-null="true"
        length="36"
        insert="false"
        update="false"
    />
    <property
        name="testguid"
        type="binary"
        column="testguid"
        not-null="false"
        length="18"
        insert="false"
        update="false"
    />
    <property
        name="automaticityreview"
        type="java.lang.Boolean"
        column="automaticityreview"
        length="1"
    />

    <property
        name="studid"
        type="java.lang.Integer"
        column="studid"
        length="10"
        not-null="false"
    />
    <many-to-one
       name="student"
       class="org.waterford.db.data.StudInfo"
       not-null="true"
       column="studid"
       insert="false"
       update="false"
    />
       
    <property
        name="scoretypeid"
        type="java.lang.Integer"
        column="scoretypeid"
        length="10"
        not-null="false"
    />
   
   <!-- ASSOCIATIONS -->
   <set
        name="scores"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="objscoreid" />
        </key>
        <one-to-many
            class="org.waterford.db.data.Score"
        />
    </set>
   
   <set
      name="objstuds"
      lazy="true"
      inverse="true"
   >
       <key column="objscoreid"/>
      <one-to-many class="org.waterford.db.data.ObjStud"/>
      
   </set>
   
   <set
      name="itemscores"
      lazy="true"
      inverse="true"
      cascade="all-delete-orphan"
   >
       <key column="objscoreid"/>
      <one-to-many class="org.waterford.db.data.ItemScore"/>
      
   </set>
   
   <!-- bi-directional many-to-one association with Activity -->
   <property
      name="actid"
      column="actid"
      type="java.lang.Integer"
      length="10"
      not-null="false"
   />
   <many-to-one
      name="activity"
      column="actid"
      class="org.waterford.db.data.Activity"
      not-null="false"         
       insert="false"
       update="false"
   />
   
   <!-- bi-directional many-to-one association with Objective -->
   <property
      name="objid"
      column="objid"
      type="java.lang.Integer"
      length="10"
      not-null="false"
   />
   <many-to-one
      name="objective"
      column="objid"
      class="org.waterford.db.data.Objective"
      not-null="false"         
       insert="false"
       update="false"
   />

   <sql-insert callable="true">INSERT INTO objscore(objtreeid, objdate, performance, correct, total, avgtime, verbosescore, automaticityreview, studid, scoretypeid, actid, objid, testguid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,NEWID())</sql-insert>

</class>


Full stack trace of any exception that occurs:

Quote:
Caused by: java.lang.NullPointerException
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1755)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2178)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:247)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:165)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:71)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463)
at org.waterford.common.data.db.hibernate.HibernateDbSession.save(HibernateDbSession.java:242)
at org.waterford.db.business.CoreBO.save(CoreBO.java:107)
at org.waterford.sequencer.engine.Simulator.simulateScore(Simulator.java:335)
... 6 more


Name and version of the database you are using:
SQL Server 2000 (8.0)

The generated SQL (show_sql=true):

Quote:
Hibernate: INSERT INTO objscore(objtreeid, objdate, performance, correct, total, avgtime, verbosescore, automaticityreview, studid, scoretypeid, actid, objid, testguid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,NEWID())



Debug level Hibernate log excerpt:
Quote:
641 DEBUG [main] entity.BasicEntityPersister - Static SQL for entity: org.waterford.db.data.ObjScore
641 DEBUG [main] entity.BasicEntityPersister - Version select: select objscoreid from ObjScore where objscoreid =?
641 DEBUG [main] entity.BasicEntityPersister - Snapshot select: select objscore_.objscoreid, objscore_.objtreeid as objtreeid8_, objscore_.objdate as objdate8_, objscore_.performance as performa4_8_, objscore_.correct as correct8_, objscore_.total as total8_, objscore_.avgtime as avgtime8_, objscore_.verbosescore as verboses8_8_, objscore_.automaticityreview as automat11_8_, objscore_.studid as studid8_, objscore_.scoretypeid as scorety13_8_, objscore_.actid as actid8_, objscore_.objid as objid8_ from ObjScore objscore_ where objscore_.objscoreid=?
641 DEBUG [main] entity.BasicEntityPersister - Insert 0: INSERT INTO objscore(objtreeid, objdate, performance, correct, total, avgtime, verbosescore, automaticityreview, studid, scoretypeid, actid, objid, testguid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,NEWID())
641 DEBUG [main] entity.BasicEntityPersister - Update 0: update ObjScore set objtreeid=?, objdate=?, performance=?, correct=?, total=?, avgtime=?, verbosescore=?, automaticityreview=?, studid=?, scoretypeid=?, actid=?, objid=? where objscoreid=?
641 DEBUG [main] entity.BasicEntityPersister - Delete 0: delete from ObjScore where objscoreid=?
641 DEBUG [main] entity.BasicEntityPersister - Identity insert: INSERT INTO objscore(objtreeid, objdate, performance, correct, total, avgtime, verbosescore, automaticityreview, studid, scoretypeid, actid, objid, testguid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,NEWID())


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 1:27 pm 
Newbie

Joined: Fri Jul 21, 2006 1:24 pm
Posts: 1
Did you ever resolve this issue? I ask because I'm trying to do the same thing.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.