Elegantly handling constraint violations in EJB/JPA environment?
Posted
by hallidave
on Stack Overflow
See other posts from Stack Overflow
or by hallidave
Published on 2010-03-25T22:30:29Z
Indexed on
2010/03/25
22:33 UTC
Read the original article
Hit count: 388
I'm working with EJB and JPA on a Glassfish v3 app server. I have an Entity class where I'm forcing one of the fields to be unique with a @Column annotation.
@Entity
public class MyEntity implements Serializable {
private String uniqueName;
public MyEntity() {
}
@Column(unique = true, nullable = false)
public String getUniqueName() {
return uniqueName;
}
public void setUniqueName(String uniqueName) {
this.uniqueName = uniqueName;
}
}
When I try to persist an object with this field set to a non-unique value I get an exception (as expected) when the transaction managed by the EJB container commits.
I have two problems I'd like to solve:
1) The exception I get is the unhelpful "javax.ejb.EJBException: Transaction aborted". If I recursively call getCause() enough times, I eventually reach the more useful "java.sql.SQLIntegrityConstraintViolationException", but this exception is part of the EclipseLink implementation and I'm not really comfortable relying on it's existence.
Is there a better way to get detailed error information with JPA?
2) The EJB container insists on logging this error even though I catch it and handle it.
Is there a better way to handle this error which will stop Glassfish from cluttering up my logs with useless exception information?
Thanks.
© Stack Overflow or respective owner