-->
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.  [ 7 posts ] 
Author Message
 Post subject: how do I catch org.hibernate.exception.ConstraintViolationEx
PostPosted: Tue Feb 24, 2009 10:48 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
Hi All,

The following question might sound dumb but I'll ask it anyway. how can I catch
org.hibernate.exception.ConstraintViolationException
exceptions if I'm using JPA?

In facade bean I have an Insert method that persists the object, something like:

public void insertObject(Object entity) {
try {
em.persist(entity);
} catch (javax.persistence.EntityExistsException e) {
System.err.println(e.getMessage());
} catch (java.lang.IllegalStateException e) {
System.err.println(e.getMessage());
} catch (java.lang.IllegalArgumentException e) {
System.err.println(e.getMessage());
} catch (javax.persistence.TransactionRequiredException e) {
System.err.println(e.getMessage());
}
}

if I add a catch block like:

catch (org.hibernate.exception.ConstraintViolationException e){
// do something
}

the exception isn't catch, so how can I do to catch this exception?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 11:01 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
The exception which is thrown is a javax.persistence.RollbackException, and only has ConstraintViolationException as cause. So you have to catch the RollbackException.

Rating is welcome. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: thanks
PostPosted: Tue Feb 24, 2009 11:23 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
You already have 59 isn't that enough? :-) just kidding

Let me test it (although I'm sure it will work fine).

Thanks


Top
 Profile  
 
 Post subject: where do I put the catch ?
PostPosted: Tue Feb 24, 2009 11:38 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
Where do I put the catch block? In the insertObject in facade bean, like:

public void insertObject(Object entity) {
try {
em.persist(entity);
} catch (javax.persistence.EntityExistsException e) {
System.err.println(e.getMessage());
} catch (java.lang.IllegalStateException e) {
System.err.println(e.getMessage());
} catch (java.lang.IllegalArgumentException e) {
System.err.println(e.getMessage());
} catch (javax.persistence.TransactionRequiredException e) {
System.err.println(e.getMessage());
}
} catch (javax.persistence.RollbackException e) {

}
}


or in the method calling the insertObject method in my backing bean?

Anyway it doesn't work regardless of were I put it.

Thanks :-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 11:45 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Where you put it depends on where you want to catch it and how you handle it.

Are you sure it is not catched? Since you have an empty catch-block I guess you still see your exception in the log. That could be, because Hibernate still logs the exception.

In order to check if it is really catched, add System.out.println("---CATCHED---"); or something like that to the catch-block.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 7:37 am 
Newbie

Joined: Fri Jan 30, 2009 6:31 am
Posts: 13
Location: Bucharest/Romania
sorry for the delayed reply I've tried all different combinations but I still can't catch the exception. here's what I have now:

facade
=====

public void insertObject(Object entity)
throws javax.transaction.RollbackException {
try {
em.persist(entity);
} catch (javax.persistence.EntityExistsException e) {
System.err.println(e.getMessage());
} catch (java.lang.IllegalStateException e) {
System.err.println(e.getMessage());
} catch (java.lang.IllegalArgumentException e) {
System.err.println(e.getMessage());
} catch (javax.persistence.TransactionRequiredException e) {
System.err.println(e.getMessage());
}
}

client
====

try {

beanRemote.insertObject(cmk);

} catch (javax.transaction.RollbackException e) {
System.err.println("transaction.RollbackException");
System.err.println(e.getMessage());
} catch (javax.persistence.RollbackException e) {
System.err.println("persistence.RollbackException");
System.err.println(e.getMessage());
} catch (Exception e) {
System.err.println("Exception");
System.err.println(e.getMessage());
}

the output is:

Exception
javax.transaction.RollbackException: [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] Can't commit because the transaction is in aborted state

so the Exception catch block is the catching the error.

Am I doing things right here?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 4:09 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
hmm, really looking weird, as you explicitly catch a javax.transaction.RollbackException, which is thrown. Are the linenumbers of your stack trace correct?

_________________
-----------------
Need advanced help? http://www.viada.eu


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