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.
PersistenceException
will invalidate the transaction context and rollback the transaction, not matter if you catch it or not. The javadoc says "All instances of PersistenceException except for instances of NoResultException, NonUniqueResultException, LockTimeoutException, and QueryTimeoutException will cause the current transaction, if one is active, to be marked for rollback.". That's a big difference with plain JDBC were you can swallow an exception (depending on what you do) and still commit. – Sthenic