• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Session save and update.

 
Madhu Kumar Vatts
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an application where we retrieve data from one DB2 and save it to sybase. In this process we use hibernate to perform this task, My question is when I find the same row in sybase to save, it throws and exception, as u know duplicate key exception. With out throwing this can I automatically update the row when I find a duplicate row, if so how?? The reason for this is it takes time while throwing this exception, and I wanted to improve the performance of this application.

Thanks for answering
Madhu
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Kumar"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Also thanks for the great responses you have given to others in your recent posts.

Mark
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe Hibernate has this ability. I recall that the Session has a saveOrUpdate() method -- would this do it? I'm just getting started with Hibernate. I know the concepts but not the API.

However, if you find that the majority of the cases are updates (versus inserts), you could improve performance by querying for an existing object first. If it's found, do an update. If not, do an insert.

The rationale is that when you choose wrong (attempt insert and fail or query and find no row) you end up performing two database operations. Therefore, you want to minimize the chance that you are wrong.

The other option, of course, is to use a stored procedure for this. It would still require the same database queries, but it would involve only one trip to the database.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic