-->
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 question
PostPosted: Fri Oct 07, 2005 4:36 am 
Newbie

Joined: Wed Oct 05, 2005 9:51 am
Posts: 5
Hi people!

I have a problem in my code when I'm using SaveOrUpdate for inserting a new record into the database.

When I'm doing a commit it results in an HibernateException: Batch update row count.
This only happens wen I try to insert a brand new record.
When updating an already existing record, everything works fine.

I'be tried to do some changes in the mappingfile ( originally generated by Middlegen ); changing the generator class from "assigned" to something else. But I'm still getting the error. Also been experiencing with different options for "unsaved-value".

Another strange thing is this:
I'm pretty sure this was running OK last week when I was running it on Tomcat 5.0. Changed to JBOSS4 this week.



Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="no.politiet.pd.servers.tts.hibernate.Person"
table="person"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="person"
</meta>

<id
name="prsId"
type="java.lang.Integer"
column="prs_id"
unsaved-value="null"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.Integer"
column="prs_id"

</meta>
<generator class="assigned" />
</id>

<property
name="prsEtternavn"
type="java.lang.String"
column="prs_etternavn"
length="30"
>
.
.
.
.
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

session = HibernateUtil.currentSession();
tx = session.beginTransaction();

//register a person
Person p = new Person();
p.setPrsId(new Integer(req.getParameter("prs_id")));
p.setPrsFornavn(req.getParameter("prs_fornavn"));
session.saveOrUpdate(p);
tx.commit();
hibernateUtil.closeSession();


Full stack trace of any exception that occurs:
09:06:32,140 ERROR [SessionImpl] Could not synchronize database state with session
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
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 no.politiet.pd.servers.tts.servlets.ttsServlet.doGet(ttsServlet.java:177)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)

Name and version of the database you are using:
Sybase

The generated SQL (show_sql=true):
update person set prs_etternavn=?, prs_etternavn_sx=?, prs_fornavn=?, prs_alias=?, prs_fnr=?, prs_foedseldato=?, prs_statsborgerskap=?, prs_kjoenn_tp=?, prs_alg_id=?, sist_endret_bru=?, prs_maalform=?, prs_prikker=? where prs_id=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: do not confuse H, call save
PostPosted: Fri Oct 07, 2005 11:31 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
When generator is ‘assigned’ then saveOrUpdate will not work by very simple reason:
According to mapping unsaved value is null, but when you assign id to something, then for saveOrUpdate it means that object exists and H needs to update it. But since there is no such object in the DB, the update count is 0 and H expects 1, hence the error.

What to do:
- when use ‘assigned’ – simply call session.save( object ) to store a brand new object.
- Or change generator type, then if you pass to saveOrUpdate object with id=null, then H will generate new ID and save it.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 12:14 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
or use Interceptor


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.