-->
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.  [ 4 posts ] 
Author Message
 Post subject: saveOrUpdate throws HibernateException
PostPosted: Fri Mar 12, 2004 5:45 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Hi,

I'm using Hibernate 2.1.1 on Jonas. I have a very simple class and I want to store objet of it.

The point is that the object I want to save may already be stored in the database, in this case I want to update it. So I though that the saveOrUpdate methode could be the right one.

But I get an HibernateException :
[main] impl.SessionFactoryObjectFactory - initializing class SessionFactoryObjectFactory
[main] impl.SessionFactoryObjectFactory - registered: 402882a9fb3e6c6e00fb3e6c728b0000 (unnamed)
[main] impl.SessionFactoryObjectFactory - no JNDI name configured
[main] impl.SessionFactoryImpl - instantiated session factory
[main] impl.SessionImpl - opened session
[main] transaction.JDBCTransaction - begin
[main] transaction.JDBCTransaction - current autocommit status:true
[main] transaction.JDBCTransaction - disabling autocommit
**************hibProcessState 0= open.running****************
[main] engine.Cascades - id unsaved-value strategy NULL
[main] impl.SessionImpl - saveOrUpdate() previously saved instance with id: 0
[main] impl.SessionImpl - updating [org.enhydra.shark.instancepersistence.data.HibernateProcessState#0]

[main] transaction.JDBCTransaction - commit
[main] impl.SessionImpl - flushing session
[main] impl.SessionImpl - Flushing entities and processing referenced collections
[main] impl.SessionImpl - Updating entity: [org.enhydra.shark.instancepersistence.data.HibernateProcessState#0]
[main] impl.SessionImpl - Processing unreferenced collections
[main] impl.SessionImpl - Scheduling collection removes/(re)creates/updates
[main] impl.SessionImpl - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
[main] impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
[main] impl.Printer - listing entities:
[main] impl.Printer - org.enhydra.shark.instancepersistence.data.HibernateProcessState{name=open.running, keyValue=open.running, id=0}
[main] impl.SessionImpl - executing flush
[main] persister.EntityPersister - Updating entity: [org.enhydra.shark.instancepersistence.data.HibernateProcessState#0]
[main] impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
[main] hibernate.SQL - update processstates set keyvalue=?, name=? where id=?
Hibernate: update processstates set keyvalue=?, name=? where id=?
[main] impl.BatcherImpl - preparing statement
[main] persister.EntityPersister - Dehydrating entity: [org.enhydra.shark.instancepersistence.data.HibernateProcessState#0]
[main] type.StringType - binding 'open.running' to parameter: 1
[main] type.StringType - binding 'open.running' to parameter: 2
[main] type.IntegerType - binding '0' to parameter: 3
[main] impl.BatcherImpl - Adding to batch
[main] impl.BatcherImpl - Executing batch size: 1
[main] impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
[main] impl.BatcherImpl - closing statement
[main] impl.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:118)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2311)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.enhydra.shark.transaction.SharkHibernateTransaction.commit(SharkHibernateTransaction.java:45)
at org.enhydra.shark.instancepersistence.HibernatePersistentManager.initActivityAndProcessStatesTable(HibernatePersistentManager.java:128)
at org.enhydra.shark.instancepersistence.Initialisation.init(Initialisation.java:32)
at javax.servlet.GenericServlet.init(GenericServlet.java:256)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:935)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:823)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3422)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3623)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:821)
at org.apache.catalina.core.ContainerBase.access$000(ContainerBase.java:162)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:182)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:805)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
at org.objectweb.jonas.web.catalina41.CatalinaJWebContainerServiceImpl.doRegisterWar(CatalinaJWebContainerServiceImpl.java:441)
at org.objectweb.jonas.web.AbsJWebContainerServiceImpl.registerWar(AbsJWebContainerServiceImpl.java:752)
at org.objectweb.jonas.web.AbsJWebContainerServiceImpl.doStart(AbsJWebContainerServiceImpl.java:344)
at org.objectweb.jonas.web.catalina41.CatalinaJWebContainerServiceImpl.doStart(CatalinaJWebContainerServiceImpl.java:245)
at org.objectweb.jonas.service.AbsServiceImpl.start(AbsServiceImpl.java:67)
at org.objectweb.jonas.service.ServiceManager.startServices(ServiceManager.java:308)
at org.objectweb.jonas.server.Server.start(Server.java:198)
at org.objectweb.jonas.server.Server.main(Server.java:311)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.objectweb.jonas.server.Bootstrap.main(Bootstrap.java:398)
net.sf.hibernate.HibernateException: Batch update row count wrong: 0


I have checked with the methode save by clearing the table before, and it works.

Here is the mappings :
<?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="org.enhydra.shark.instancepersistence.data.HibernateProcessState" table="processstates">

<id name="id">
<column name="id" not-null="true"/>
<generator class="increment"/>
</id>

<property name="keyValue">
<column name="keyvalue" not-null="true"/>
</property>

<property name = "name">
<column name="name" not-null="true"/>
</property>

</class>

</hibernate-mapping>


And here is the piece of useful code :
HibernateTransactionFactory hibFact = new HibernateTransactionFactory();

SharkTransaction t = hibFact.createTransaction();
HibernateProcessState hibProcessState = new HibernateProcessState();
HibernateActivityState hibActivityState = new HibernateActivityState();
Session session = ThreadLocalSession.currentSession();

for (int i=0; i<1/*activityAndProcessStates.length*/; i++) {
String state=activityAndProcessStates[i];

System.out.println("hibProcessState " + i + "= " + state);
hibProcessState.setKeyValue(state);
hibProcessState.setName(state);
session.saveOrUpdate(hibProcessState);
}
t.commit();
session.close();

Here is the POJO class :
public class HibernateProcessState {

private String keyValue;
private String name;
private int id;


public HibernateProcessState() {
}

public String getKeyValue() {
return keyValue;
}


public String getName() {
return name;
}

public void setKeyValue(String string) {
keyValue = string;
}

public void setName(String string) {
name = string;
}

public int getId() {
return id;
}

public void setId(int i) {
id = i;
}

}


Any idea, I saw that unsaved-value is important and by default check the identifier. But I think my problem is a little bit more complicated. The id is generated by incrementing the existing and last value in the DB table, but when I will try to saveOrUpdate the object, there have not yet id set.

Vladislav


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 5:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
private int id; --> id cannot be null, by default = 0 ? --> so saveOrUpdate execute a save since id is not null

try with Integer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 6:06 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Ok thanks, that was sure.

But is there a way, to check with saveOrUpdate if the object I want to save or update match to the entries in the DB table. I mean I'd like to be able to insert in the db if keyValue is not already present in the db and if keyValue is present in the DB to update it indepedently of the id.

But I guess I have to make a query for that ?

Vlad


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 6:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the best way is to query before but you may look at select-before-update attribute page 26 of the pdf reference doc 2.1.2


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