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.  [ 7 posts ] 
Author Message
 Post subject: SaveOrUpdate again.Understand the question had been asked.
PostPosted: Fri Sep 24, 2004 6:13 am 
Newbie

Joined: Fri Jul 02, 2004 5:17 am
Posts: 7
I understand saveOrUpdate had been asked before in the forum. I still couldn't get it to work after days of frustration.

Mapping file was generated by Middlegen. Did adjustment to it by adding not-null="true". Hoping that it would solved the problem. Also added unsaved-value="undefined" to id.


Hibernate version:
2.16

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

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="igts.server.database.etClasses.Admin"
table="admin"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="admin"
</meta>

<id
name="adminNric"
type="java.lang.String"
column="admin_NRIC"
unsaved-value="undefined"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.String"
column="admin_NRIC"
not-null="true"
unsaved-value="undefined"
</meta>
<generator class="assigned" />
</id>

<property
name="strAdminName"
type="java.lang.String"
column="str_admin_name"
not-null="true"
length="50"
>
<meta attribute="field-description">
@hibernate.property
column="str_admin_name"
not-null="true"
length="50"
</meta>
</property>
<property
name="strPassword"
type="java.lang.String"
column="str_password"
not-null="true"
length="50"
>
<meta attribute="field-description">
@hibernate.property
column="str_password"
not-null="true"
length="50"
</meta>
</property>

<!-- associations -->

</class>
</hibernate-mapping>


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

try
{

session = HibernateUtil.getSession();

tx = session.beginTransaction();
session.saveOrUpdate(obj);

tx.commit();
}
catch (HibernateException he )
{
logger.fatal("Close Session Error");
}
finally
{
try
{
session.close();
}
catch(HibernateException e)
{
logger.fatal("Close Session Error");
}
}
Full stack trace of any exception that occurs:
ERROR net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2379) : 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 igts.server.database.impl.hibernate.HibernateManager.save(HibernateManager.java:448)
at igts.server.database.IOS_emulator.saveEmulation(IOS_emulator.java:237)
at igts.server.database.IOS_emulator.saveButtonActionPerformed(IOS_emulator.java:193)
at igts.server.database.IOS_emulator.access$1(IOS_emulator.java:190)
at igts.server.database.IOS_emulator$2.actionPerformed(IOS_emulator.java:100)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Name and version of the database you are using:
MySQL 4.0.18

The generated SQL (show_sql=true):
update admin set str_admin_name=?, str_password=? where admin_NRIC=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 6:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
SQL insert, update or delete failed (row not found)


you're surely trying to update something that is not in db
so saveOrUpdate:
- save if id = unsaved value
- update if not

What is the value of obj.id?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 9:25 am 
Newbie

Joined: Fri Aug 27, 2004 5:36 pm
Posts: 16
Location: Pittsburgh
If the mapping for id is "assigned" type, you have to excplicity call save or update method.

saveOrUpdate would try update all the time if id mapping is "assigned"


correct if i am wrong.. Anthony.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 9:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
yes you're correct ;)
i missed generator = assigned

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 10:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Not quite. You can use a version or timestamp property with an unsaved-value, or you can implement Interceptor.isUnsaved().

This is a FAQ!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 26, 2004 9:33 pm 
Newbie

Joined: Fri Jul 02, 2004 5:17 am
Posts: 7
How do you introduce a timestamp property with an unsaved-value? And how do you implement Interceptor.isUnsaved()?

Thanks for all the help. I appreciate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 27, 2004 2:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Gavin said it was a FAQ, so just search in the FAQ

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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