-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to handle BatchUpdateException
PostPosted: Mon Oct 01, 2007 8:37 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

I am using JSF, Spring and Hibernate with mysql as my db.

I want to save pcode and pname,etc in the db.

I used getHibernateTemplate.save(obj);

I am facing an issue like when i add the same pcode and pname which is existing.... It is throwing Error : BatchUpdateException

Caused by: java.sql.BatchUpdateException: Duplicate entry 'T1-test1' for key 1

I am unable to handle or catch this exception.

<code>
public PartnerMast save(PartnerMast entity) {
try{
System.out.println("Calling Save Method.............");
entity.setIsActive("Y");
getHibernateTemplate().save(entity);
System.out.println("Record Saved..............");
}catch(Exception e){
System.out.println("Excep :"+e);
}
</code>
I am not able to catch
~~~~~~~~~~~~~~~~~~~~~
catch(BatchUpdateException e)
~~~~~~~~~~~~~~~~~~~~~

The thing is i want to catch this exception and display the msg n jsf ui.

Could anyone pls help me could i handle this exception.

Thanks,
Ambika☺


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 8:19 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
I don't think there is a way to tweak the hibernate, unless you would change it's code a lot. You will probrably need to do something like this:

Code:
...
} catch (Exception e) {
  if (causedByBatchUpdateException(e)) {
    //display it
  } else {
     throw e;
  }
}

...

private boolean causedByBatchUpdateException(Exception e)
{
  Exception cause = e;
  while (cause != null) {
    if (cause instanceof BatchUpdateException) {
       return true; //if it is your exception you want to handle, return true
    }
    cause = e.getCause(); //look in the cause
    if (e == cause) {
       cause = null; //end in case, exception is referencing itself
    }
  }
  return false;
}


Regards,
Pavol


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