-->
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: saveOrUpdate with assigned generator and version
PostPosted: Mon Sep 20, 2004 2:23 pm 
Newbie

Joined: Fri Sep 17, 2004 5:11 pm
Posts: 2
I am trying to call the "saveOrUpdate" function to save an object that has no matching entry in the database (i.e. I want it to insert a new row). My call to "saveOrUpdate" tries, instead, to do an update and throws an error.

I am using the "assigned" generator for my id field because I am dealing with legacy data.

I have searched the docs and this forum and it looks like all that is needed is an appropriately formed <version> tag with hibernate 2.1, but I can't seem to get everything in place.

My mapping file includes the following entries:
<id name="brandCode" type="java.lang.String" column="brand_code">
<generator class="assigned"/>
</id>
<version column="hiberVersion" name="hiberVersion" type="integer" unsaved-value="null"/>

My bean has these entries:
private java.lang.String brandCode;
private int hiberVersion = -1;

public java.lang.String getBrandCode() {return this.brandCode;}
public void setBrandCode(java.lang.String brandCode) {this.brandCode = brandCode;}
public int getHiberVersion() {return hiberVersion;}
public void setHiberVersion(int integer) {hiberVersion = integer;}

It is my understanding that this should be enough to allow proper use of the saveOrUpdate command, but I still see the SQL statement generated trying to perform an update instead of an insert. Any help would be appreciated.

Thanks
-Wick


Information requested for posts to this forum follows:
Hibernate version: 2.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.nutra.labels.databeans.Brand" table="lb_brand">
<id name="brandCode" type="java.lang.String" column="brand_code">
<generator class="assigned"/>
</id>
<version column="hiberVersion" name="hiberVersion" type="integer" unsaved-value="null"/>
<property name="brandName" type="java.lang.String" column="brand_name" not-null="true" length="20"/>
<property name="tollFreePhone" type="java.lang.String" column="TollFree_Phone" not-null="true" length="14"/>
<property name="webSite" type="java.lang.String" column="WebSite" not-null="true" length="50"/>
<property name="slogan" type="java.lang.String" column="slogan" length="255"/>
<property name="guaranteeNote" type="java.lang.String" column="guarantee_note" length="255"/>
<property name="promoStatement" type="java.lang.String" column="promo_statement" length="255"/>
<property name="upcPrefix" type="java.lang.String" column="upc_prefix" length="10"/>
<property name="frontPanelImage" type="java.lang.String" column="FrontPanel_image" length="255"/>
<property name="backPanelLogo" type="java.lang.String" column="BackPanel_logo" length="255"/>
<property name="logoImage" type="java.lang.String" column="logo_image" length="255"/>
<property name="lastModified" type="java.sql.Timestamp" column="last_modified" length="23"/>
<property name="fontFamily" type="java.lang.String" column="FontFamily" length="50"/>
<!-- associations -->
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Session s = sessionFactory.openSession();
Transaction tx = s.beginTransaction();
s.saveOrUpdate(brand);
tx.commit();
s.close();

Full stack trace of any exception that occurs:
SEVERE: Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:684)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.nutra.labels.databeans.Brand.main(Brand.java:265)
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:684)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.nutra.labels.databeans.Brand.main(Brand.java:265)

Name and version of the database you are using:
Sybase 12.0

The generated SQL (show_sql=true):
Hibernate: update lb_brand set hiberVersion=?, brand_name=?, TollFree_Phone=?, WebSite=?, slogan=?, guarantee_note=?, promo_statement=?, upc_prefix=?, FrontPanel_image=?, BackPanel_logo=?, logo_image=?, last_modified=?, FontFamily=? where brand_code=? and hiberVersion=?

Debug level Hibernate log excerpt:
Sep 20, 2004 12:10:36 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.6
Sep 20, 2004 12:10:37 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.sybase.jdbc2.jdbc.SybDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.hibernate.cache.EhCacheProvider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.SybaseDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=testuser, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:sybase:Tds:dev-db:6100/eapps, hibernate.connection.password=xxx, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
Sep 20, 2004 12:10:37 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: using java.io streams to persist binary types
Sep 20, 2004 12:10:37 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Sep 20, 2004 12:10:37 PM net.sf.hibernate.cfg.Configuration configure
INFO: configuring from file: hibernate.cfg.xml
Sep 20, 2004 12:10:38 PM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: com/nutra/labels/databeans/Brand.hbm.xml
Sep 20, 2004 12:10:41 PM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: com.nutra.labels.databeans.Brand -> lb_brand
Sep 20, 2004 12:10:41 PM net.sf.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Sep 20, 2004 12:10:41 PM net.sf.hibernate.cfg.Configuration secondPassCompile
Sep 20, 2004 12:10:41 PM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.SybaseDialect
Sep 20, 2004 12:10:41 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximim outer join fetch depth: 1
Sep 20, 2004 12:10:41 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use outer join fetching: true
Sep 20, 2004 12:10:41 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Sep 20, 2004 12:10:41 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 1
Sep 20, 2004 12:10:41 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.sybase.jdbc2.jdbc.SybDriver at URL: jdbc:sybase:Tds:dev-db:6100/eapps
Sep 20, 2004 12:10:41 PM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=testuser, password=xxx}
Sep 20, 2004 12:10:41 PM net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use scrollable result sets: true
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use JDBC3 getGeneratedKeys(): false
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: false
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: echoing all SQL to stdout
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {no='N', true=1, yes='Y', false=0}
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: cache provider: net.sf.hibernate.cache.EhCacheProvider
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: query cache factory: net.sf.hibernate.cache.StandardQueryCacheFactory
Sep 20, 2004 12:10:42 PM net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
Sep 20, 2004 12:10:42 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Sep 20, 2004 12:10:44 PM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Sep 20, 2004 12:10:44 PM net.sf.hibernate.cache.UpdateTimestampsCache <init>
INFO: starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
Sep 20, 2004 12:10:44 PM net.sf.ehcache.config.Configurator configure
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/javatools/hibernate-2.1/lib/ehcache-0.9.jar!/ehcache-failsafe.xml
Sep 20, 2004 12:10:44 PM net.sf.hibernate.cache.EhCache <init>
WARNING: Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
Sep 20, 2004 12:10:44 PM net.sf.hibernate.cache.StandardQueryCache <init>
INFO: starting query cache at region: net.sf.hibernate.cache.StandardQueryCache
Sep 20, 2004 12:10:44 PM net.sf.hibernate.cache.EhCache <init>
WARNING: Could not find configuration for net.sf.hibernate.cache.StandardQueryCache. Configuring using the defaultCache settings.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 3:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
of course if your unsaved-value="null", your version property should be an Integer which is null in the unsaved state.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 5:34 pm 
Newbie

Joined: Fri Sep 17, 2004 5:11 pm
Posts: 2
Oops -- thanks.

To try and justify myself, I had made the change of the "unsaved-value" to "undefined", but apparently my java IDE will copy any file in my source directory to my compile directory, but only overwrites class files, so the change to the mapping file was never copied to the compile directory.

Fortunately I pulled the posted source through a means other than my IDE. Thanks again for your help.

-Wick


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.