-->
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: Update not Insert when version matches unsaved-value
PostPosted: Wed Jun 04, 2008 10:49 am 
Newbie

Joined: Wed Jun 04, 2008 10:05 am
Posts: 1
Hi

I'm having a bit of an issue with version. When the value of the version matches the value of my version's unsaved-value, hibernate is issuing an SQL UPDATE instead of an SQL INSERT.

I am trying to do it this way as I would like the ID to be application defined, rather than having a hibernate-generated ID.

I've done a fair bit of documentation reading, googling and forum reading on this matter but none of the suggestions have worked so far. Basically whenever I try a session.saveOrUpdate, hibernate will try an update, rather than an insert, even though my version is set to null (the unsaved-value).

Hibernate version: 2.1.8
Database: Oracle 10g

Mapping documents:
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">

<hibernate-mapping>

   <!-- Agency Details -->
   <class name="com.thistle.portlet.exts.trust.Agency" table="Agency" dynamic-update="true" optimistic-lock="version">
      <id name="iata" column="iata" type="java.lang.String" unsaved-value="any"/>
      
      <version
           column="sys_version"
           name="_version"
           type="integer"
           unsaved-value="null"
      />
      
      <property name="name">
         <column name="name"/>
      </property>
      
      <set name="trusted" cascade="all" lazy="false" table="Agency_Trusted_Corporation">
           <key column="agency_iata"/>
           <many-to-many column="corp_id"
              lazy="false"
               unique="false"
               class="com.thistle.portlet.exts.trust.Corporation"/>
       </set>
           
        <set name="reserved" cascade="all" lazy="false" table="Agency_Reserved_Corporation">
           <key column="agency_iata"/>
           <many-to-many column="corp_id"
              lazy="false"
               unique="false"
               class="com.thistle.portlet.exts.trust.Corporation"/>
       </set>
       
      <set name="reservedTravellers" cascade="all" lazy="false" table="Agency_Reserved_Traveller">
           <key column="agency_iata"/>
           <many-to-many column="trav_opera_id"
              lazy="false"
               unique="false"
               class="com.thistle.portlet.exts.trust.Traveller"/>
       </set>
   </class>
   
   
   <!-- Corporate details as  -->
   <class name="com.thistle.portlet.exts.trust.Corporation" table="Corporation" dynamic-update="true" optimistic-lock="version">
      <id name="cid" column="cid" type="java.lang.String" unsaved-value="any"/>
      
      <version
           column="sys_version"
           name="_version"
           type="integer"
           unsaved-value="null"
      />

      <property name="name">
         <column name="name" />
      </property>
      
      <set name="reservedTravellers" cascade="all" lazy="false" table="Corporate_Reserved_Traveller">
           <key column="corp_id"/>
           <many-to-many column="trav_opera_id"
              lazy="false"
               unique="false"
               class="com.thistle.portlet.exts.trust.Traveller"/>
       </set>
   </class>
   
   
   <!-- Traveller details as  -->
   <class name="com.thistle.portlet.exts.trust.Traveller" table="Traveller" dynamic-update="true" optimistic-lock="version">
      <id name="operaId" column="operaId" type="java.lang.String" unsaved-value="any"/>
      
      <version
           column="sys_version"
           name="_version"
           type="integer"
           unsaved-value="null"
      />
   </class>

</hibernate-mapping>


All my hibernate objects extend HibernateItem, and as such have their _version Integer set to null, if it is a new item:
Code:
public class HibernateItem {
   private Integer _version = null;
   ...
   getter and setter...
    ...
}


saveOrUpdate method:
Code:
   public void saveOrUpdateAgency(final Agency agency) {
      
      log.debug("Attempting to commit Agency.");
      log.debug("IATA: "+agency.getIata());
      log.debug("VERSION: "+agency.get_version());
      log.debug("NAME: "+agency.getName());
       
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(final Session session) throws HibernateException {
               session.save(agency);
               return null;
            }
        };
   }


Log:
The logs show the version going in as null.
Code:
Attempting to commit Agency.
IATA: iata_test0
VERSION: null
NAME: Test Agency 0


However, despite this fact, hibernate does not do an insert, instead it runs an update, which naturally updates nothing, as there is nothing in the tables with the same IATA, or CID (which are my IDs).

Any help much appreciated! Thanks


Top
 Profile  
 
 Post subject: Re: Update not Insert when version matches unsaved-value
PostPosted: Mon Jun 07, 2010 4:41 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
I'm having the same problem. I also use a nullable version with unsaved-value="undefined", and also tried unsaved-value="null".
But still Hibernate issues an UPDATE statement instead of an INSERT statement with an assigned identifier.

Can anyone help?


Top
 Profile  
 
 Post subject: Re: Update not Insert when version matches unsaved-value
PostPosted: Tue Jun 08, 2010 5:52 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
I've found the solution to this problem, or the cause.

I am using Maven and the Maven project structure.
I discovered that when runnin "mvn test" my unit tests for Oracle passed. Running my unit tests in the Eclipse IDE failed.

Apparently I assumed changing my Hibernate mapping files under src/test/resources would be sufficient for running the tests inside the IDE. But they are only loaded when running "mvn test".
I changed the mapping file under src/main/resources, and then my unit tests passed also in Eclipse.

I use mapping files separately under src/test/resources and src/main/resources because under src/main/resources I only have mapping files for mapping on the Oracle database, while under src/test/resources I have separate mapping files for running under Oracle and HSQL DB.
Perhaps I need to rethink that structure and see if it isn't more appropriate to put the mapping files right next to the domain model POJO's. That way I only have to change one mapping file for both databases, but I thought I had problems in HSQL DB when using the mapping file I need for Oracle.

I need specific settings because I try to build an application around the (legacy) HR example schema in the Oracle 10g eXpress Edition database. And I thought they were not always compatible with HSQL DB.


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.