-->
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: find out offending record on ConstraionViolationException
PostPosted: Wed Jun 01, 2011 12:44 pm 
Newbie

Joined: Wed Jun 01, 2011 12:34 pm
Posts: 7
Greetings. Maybe I'm missing something but I'm really surprised that Hibernate is not reporting a constraint violation on insert, only when I call commit. At that point I may have inserted a number of records and I have no idea what the offending one was. Thoughts?

Details:
Java 6, Hibernate Core 3.3.1 GA.

Related code snippets:
Session session = sessionFactory.getCurrentSession();
tx = session.beginTransaction();
...

//bean is a JPA-annotated class. Why can't I get a violation notification right here, while saving?
session.save(bean);
...
tx.commit();


Call stack:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
.......


Top
 Profile  
 
 Post subject: Re: find out offending record on ConstraionViolationException
PostPosted: Fri Jun 03, 2011 5:23 am 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
In order to know the cause of constraint violation you can enable Hibernate log like this:
log4j.logger.org.hibernate.util.JDBCExceptionReporter=error

Hibernate now logs all failures related to JDBC in the console.

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: find out offending record on ConstraionViolationException
PostPosted: Fri Jun 03, 2011 9:59 pm 
Newbie

Joined: Wed Jun 24, 2009 9:10 pm
Posts: 10
Hibernate uses transaction-level write-behind mechanism which fires DML statements on transaction commit. This makes sense because this way locks used by database to execute DML statements are kept for as short time as possible.


Top
 Profile  
 
 Post subject: Re: find out offending record on ConstraionViolationException
PostPosted: Thu Jun 09, 2011 7:47 pm 
Newbie

Joined: Wed Jun 01, 2011 12:34 pm
Posts: 7
Thank you for the responses. Now I understand why im not getting violations on save.
I added:
log4j.logger.org.hibernate.util.JDBCExceptionReporter=error

to my log4j.properties but still I'm not seeing any primary key IDs being included in the exception.


Top
 Profile  
 
 Post subject: Re: find out offending record on ConstraionViolationException
PostPosted: Fri Jun 10, 2011 3:11 pm 
Newbie

Joined: Wed Jun 01, 2011 12:34 pm
Posts: 7
Also, with errors only being thrown on commit, how can I reject the offending record and commit the rest? I.e. I let's say don't want to reject the whole batch just because of one corrupt record in batch.
I heard that the spring batch framework allows you to do that easily but... I don't want to reach for spring batch at the moment.


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.