-->
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: duplicate key violates unique constraint - urgent
PostPosted: Mon Aug 14, 2006 3:33 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hi, I have a tableA where am trying to modify 2 different instances from tableA. That trying to modify row1 and row2. field 'reference' is unique. I aim is to make a copy of row1's reference then delete row1 and finally update row2's reference with that of deleted row1's reference. I got a duplicate key violate unique constraint. But am using the same hibernate session to do the updates, hibernate should know that row1 has been deleted. can anyone please explain me why this is so.

thanks in advance

[b]Hibernate version:3.1.3[/b]

[b]Mapping documents:[/b]

[b]Code between sessionFactory.openSession() and session.close():

HibernateUtil.beginTransaction();
TableA a1 = TableA.get(id1);
String ref = a.getReference();
TableA a2 = TableA.get(id2);

HibernateUtil.currentSession().delete(a1);

a2.setReference(ref);
HibernateUtil.currentSession().update(a2);
HibernateUtil.commitTransaction();

[/b]

[b]Full stack trace of any exception that occurs:
2006-08-14 11:08:54,519 : JDBCExceptionReporter.logExceptions : SQL Error: 0, SQLState: null
2006-08-14 11:08:54,534 : JDBCExceptionReporter.logExceptions : Batch entry 0 update tableA set reference=tuitui, type_id=1 where id=82
90104 was aborted. Call getNextException to see the cause.
2006-08-14 11:08:54,534 : JDBCExceptionReporter.logExceptions : SQL Error: 0, SQLState: 23505
2006-08-14 11:08:54,534 : JDBCExceptionReporter.logExceptions : ERROR: duplicate key violates unique constraint "reference_key"
2006-08-14 11:08:54,534 : AbstractFlushingEventListener.performExecutions : Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JTATransaction.commit(JTATransaction.java:135)
at com.gloptv.saga.server.tools.HibernateUtil.commitTransaction(HibernateUtil.java:205)
[/b]

[b]Name and version of the database you are using:postgresql 8[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 5:05 am 
Newbie

Joined: Fri Aug 11, 2006 4:12 am
Posts: 13
Hi ,
Try flushing the session after
HibernateUtil.currentSession().delete(a1);

P.S. do not for the CREDITS

Rgds,
Alok.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 6:02 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
thanks for your quick reply, but I want all these modifications to be made in only 1 transaction. If I call flush() after delete, and just before calling session.update(a2), an error occurs, will all my modifications be rolledback?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 7:01 am 
Newbie

Joined: Fri Aug 11, 2006 4:12 am
Posts: 13
Hi

see , when u say update or delete ....only the instances in the server are updated or deleted , but when u say flush, corresponding queries are fired..
and when u say commit, all those queries are commited...so all that should happen in one transaction in your case ...

Rgds,
Alok


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.