-->
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.  [ 5 posts ] 
Author Message
 Post subject: Remove data from database in combination with saveOrUpdate()
PostPosted: Mon Sep 12, 2016 3:27 am 
Newbie

Joined: Mon Sep 12, 2016 3:21 am
Posts: 3
I am adjusting a Java program which writes incoming data into a table in a Firebird database. The database connection is abstracted by Hibernate. The command used to save the data is saveOrUpdate(). Some of these incoming data are new and need to be inserted, other exist already and need to be updated.

This works fine so far.
The problem I have occurs when removing rows from the database, e.g. by another thread. The next time data come in which existed already before the deletion Hibernate still tries to update and throws the exception

Code:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

Is there a possibility to tell Hibernate the removal of entries so it does not try an update the next time but an insert?

The program does not open a new session each time but retrieves the current session when accessing the stored data:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// do sth with session
session.getTransaction().commit();


Top
 Profile  
 
 Post subject: Re: Remove data from database in combination with saveOrUpdate()
PostPosted: Mon Sep 12, 2016 3:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You got that exception because Hibernate prevents losing updates.

One workaround would be to override @SQLUpdate and try to use the SQL MERGE clause which is supported by Firebase.


Top
 Profile  
 
 Post subject: Re: Remove data from database in combination with saveOrUpdate()
PostPosted: Mon Sep 12, 2016 4:05 am 
Newbie

Joined: Mon Sep 12, 2016 3:21 am
Posts: 3
Thank you for your fast reply.
What are common solutions for removing data from the database so this problem cannot occur?
Searching the internet shows that the way I am doing it (using HQL) is basically fine.
Or is it necessary that one needs to override @SQLUpdate as soon as rows should be deleted?


Top
 Profile  
 
 Post subject: Re: Remove data from database in combination with saveOrUpdate()
PostPosted: Mon Sep 12, 2016 4:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You could delete an entity with bulk delete via HQL, but if a concurrent Session already loaded the deleting entity, you'd still get an OptimisticLockingException. That's actually desirable in most situations since it notifies you that you're using an old version of the current loading data.

In the previous response, I already mentioned two solutions to this situation.


Top
 Profile  
 
 Post subject: Re: Remove data from database in combination with saveOrUpdate()
PostPosted: Mon Sep 12, 2016 5:09 am 
Newbie

Joined: Mon Sep 12, 2016 3:21 am
Posts: 3
Thank you


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