-->
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.  [ 6 posts ] 
Author Message
 Post subject: INSERT ON DUPLICATE IGNORE with hibernate
PostPosted: Mon Mar 08, 2010 4:58 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
HI!

Somehow, always my hibrenate throws:
java.sql.BatchUpdateException: Duplicate entry '105-213' for key 1

When the database doesn't have a duplicate record! it seems to me that it's trying to update although i don't ask it to do it.

I thought, as a temporarily solution, to use on duplicate ignore.

How do i ask Hibernate to use it?

As well, what could be a reason to hibernate to insert when i do find?


Top
 Profile  
 
 Post subject: Re: INSERT ON DUPLICATE IGNORE with hibernate
PostPosted: Mon Mar 08, 2010 6:36 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
java.sql.BatchUpdateException: Duplicate entry '105-213' for key 1


This exception actually comes from your database,
so you must enable 'ignore duplicate' directly on index on the database (if your database offers this feature at all).

Quote:
As well, what could be a reason to hibernate to insert when i do find?


Most probably you are calling a query without changing the default flush mode,
which means that implicitly flush() is called (performing all scheduled insert, update and delete actions).

Quote:
When the database doesn't have a duplicate record!


It's obvious that on the database you never will see duplicate records, because the unique-constraint don't permits it.
But you are trying to insert a record which has the same unique-constraint key as another already inserted record.


Top
 Profile  
 
 Post subject: Re: INSERT ON DUPLICATE IGNORE with hibernate
PostPosted: Mon Mar 08, 2010 6:52 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
Quote:
Most probably you are calling a query without changing the default flush mode

what do i need to change in the code?
i simply use:
Code:
@SuppressWarnings("unchecked")
   @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
   public Collection<T> findByQuery(String queryString, Object... values) {
      return getHibernateTemplate().find(queryString, values);
   }


Top
 Profile  
 
 Post subject: Re: INSERT ON DUPLICATE IGNORE with hibernate
PostPosted: Mon Mar 08, 2010 7:04 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
getHibernateTemplate()


Do you use Hibernate with Spring? (Unfortunately I don't know Spring).

Anyway: the implicite flush() on the query only anticipates the duplicate key exception.
Disabling the implicite flush() only defers the duplicate key exception to the commit-time
(except you are not intended to commit the transaction at all, but rollback it).
So you have to investigate how to avoid the duplicate insert.


Top
 Profile  
 
 Post subject: Re: INSERT ON DUPLICATE IGNORE with hibernate
PostPosted: Mon Mar 08, 2010 7:12 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
The problem is that i never insert duplicate keys!

I think that hibernate session is not being closed properly, and it thinks that it has new data to insert to the database.

why would it happen?


Top
 Profile  
 
 Post subject: Re: INSERT ON DUPLICATE IGNORE with hibernate
PostPosted: Mon Mar 08, 2010 7:38 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
The problem is that i never insert duplicate keys!


But somehow something in your code is doing inserts with duplicate keys.
Please enable sql logging either with P6Spy or log4j/jdbc/JDBCAppender,
and report the sql-statements which are executed.
Im sure the concerning sql-statements can be figured out.


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