I'm trying to implement a unique value constraint on a field using JPA and Hibernate with RequestFactory (GWT 2.5). My other constraints are implemented using JSR303 and I'd like to report violations to the client in the same way (calling onConstraintViolation
in the Receiver
). As I understand, the best option to implement this type of constraint is to set unique = true
when using the @Column
attribute. This is then enforced by the database when the transaction is committed and avoids any race conditions associated with asserting the uniqueness via querying.
I followed this thread's advice on implementing a ServiceLayerDecorator
to wrap the invoke
method with a begin and commit of the transaction. If the uniqueness constraint is violated, the commit operation will throw a RollbackException
.
How do I handle this exception so that it is propagated to the client as a constraint violation instead of a generic failure?
As far as I can tell from looking at SimpleRequestProcessor
, the validation happens separately to the invocation and there doesn't seem to be an obvious way to propagate failures from invoke
in this way.
You could try to implement a custom constraint for checking for uniqueness. See also https://community.jboss.org/wiki/AccessingTheHibernateSessionWithinAConstraintValidator.