3

I was trying to create a somewhat more complex Cross-Field Bean-Validation in the style of this here. What quite quickly became a problem, as soon as I returned false on validating the instance of my entity, that was passed to my custom validator.

I thus created a field level constraint annotation, as described in the Hibernate tutorial, but even simpler. Actually my validation was only:

@Overrides
public bool isValid(Object value, ConstraintValidatorContext arg1){
    System.out.println("##### isValid call for example.");
    return false;
}

This worked fantastically. As soon as I annotated a Field with the Annotation, any persistence-attempt was smashed and the field was marked as invalid.

Now back to my class-level validator:

@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = CrossFieldConstraintValidator.class)
public @interface CrossFieldConstraint {

    String message() default "CrossFieldConstraint violation";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

Not much to see here, just your standard Constraint Annotation, which defines a message, where it's validated and where it may be put.

now when passing that to my CrossFieldConstraintValidator I recieved some overlong stacktrace. I'll paste a "slightly" shortened version, which keeps all: caused by, but removes some of the trail on it...

 19:04:31,806 WARN  [com.arjuna.ats.arjuna] (http-localhost-127.0.0.1-8443-3) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffffac10b1bd:32279610:5328864c:15fa, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@1380d1b >: javax.persistence.PersistenceException: error during managed flush
    at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1486) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:109) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:273)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:93)
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:164)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1165)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:117)
    at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:92) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:232) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
    at org.jboss.as.ejb3.security.AuthorizationInterceptor.processInvocation(AuthorizationInterceptor.java:106) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
**Caused by:** javax.validation.ConstraintViolationException: Validation failed for classes [company.crm.model.Project] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='CrossFieldConstraint violation', propertyPath=, rootBeanClass=class company.crm.model.Project, messageTemplate='CrossFieldConstraint violation'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:175) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:186) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:315) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:104) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    ... 88 more

**19:04:31,861 ERROR** [org.jboss.ejb3.invocation] (http-localhost-127.0.0.1-8443-3) JBAS014134: EJB Invocation failed on component ProjectServiceBean for method public abstract void company.crm.services.ProjectService.addProject(long,company.crm.model.Project) throws javax.transaction.TransactionRolledbackException,javax.persistence.OptimisticLockException: javax.ejb.EJBTransactionRolledbackException: Transaction rolled back
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleEndTransactionException(CMTTxInterceptor.java:115) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:95) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:232) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
    at org.jboss.as.ejb3.security.AuthorizationInterceptor.processInvocation(AuthorizationInterceptor.java:106) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
    at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:76) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
**Caused by:** javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1177)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:117)
    at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:92) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    ... 79 more
**Caused by:** javax.persistence.PersistenceException: error during managed flush
    at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1486) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:109) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:273)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:93)
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:164)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1165)
    ... 82 more
**Caused by:** javax.validation.ConstraintViolationException: Validation failed for classes [company.crm.model.Project] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='CrossFieldConstraint violation', propertyPath=, rootBeanClass=class comapany.crm.model.Project, messageTemplate='CrossFieldConstraint violation'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:175) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    ... 88 more

I then proceeded to reduce the complexity of the problem more and more until I got to the point, where my CrossFieldConstraintValidator was nothing more than:

public class CrossFieldConstraintValidator 
       implements ConstraintValidator<CrossFieldConstraint, Object>{
    @Override
    public void initialize(final CrossFieldConstraint annotation) {

    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext validatorContext) {
    try{
        System.out.println("### isValid - " + value + "  " + validatorContext.toString());

        return false;       
    }catch(Exception logIt){
        System.out.println("Validation Failed with Exception: " +  logIt.toString());
        System.out.println("ValidatorContext: "+ validatorContext.toString());
        return false;
    }
}

But I still got the same error. I am running on JBoss 7.1.1.Final and using Primefaces.

The Entity I pass is something like this:

@Entity
@CrossFieldConstraint
public class Project{

  @Id
  @GeneratedValue
  private long id;

  @ManyToOne
  private Customer owner;
}

The persistence happens with a service class in the following manner:

@Inject
EntityManager entityManager;

public void add(Project proj, long ownerId){
   Customer managedOwner = entityManager.find(Customer.class, ownerId);
   proj.setCustomer(managerCustomer);
   entityManager.persist(proj);
}
Community
  • 1
  • 1
Vogel612
  • 5,620
  • 5
  • 48
  • 73
  • is your validation method being hit or not? – radai Mar 20 '14 at 18:41
  • @radai It's being hit and returns false. You can see that by the ConstraintViolationException. Also I had a log to System.out in place, but decided to spare you from it, as it adds no value to the actual problem – Vogel612 Mar 20 '14 at 18:48
  • so you have a validator that always returns false (meaning invalid), and a TX that fails because your validator returned false, as instructed... what is the issue then? – radai Mar 20 '14 at 18:51
  • @radai the issue is, that the code crashes and does not nicely mark anything as invalid as shown when using field-level annotations. The problem is, as soon as I put correct logic in there, it **can always** return false, and if everything comes crashing down then, we have a real problem. Also the TX does not fail in the right place. It should instead of current behavior throw a wrapping `EJBTransactionRolledbackException` after catching the `PersistenceException`. – Vogel612 Mar 20 '14 at 18:52
  • its very possible im still not getting it, but AFAIK bean validation is done at TX commit time, so NOT right when you call persist() - which is why youre getting the exception from arjuna - the TX manager. you could either try forcing the validation earlier by qualifying the "proj" argument with the @Valid annotation (http://docs.jboss.org/hibernate/beanvalidation/spec/1.1/api/javax/validation/Valid.html) or inject a validation factory yourself and validate the proj arg before calling persist – radai Mar 20 '14 at 19:04
  • @radai thanks for your help. I decided to ignore the Arjuna warning and instead handle the error manually... – Vogel612 Mar 25 '14 at 08:29

1 Answers1

1

I decided to ignore the Warning that comes from Arjuna, and instead now catch the EJBTransactionRolledBackException. What was missing was the part, that I needed to stop deferral from the view in the Controller I used.

public boolean processSave(){
   boolean success = true;
   try{
      //persistence operation via service class
   }
   catch(OptimisticLockException e){
      success = false;
      //add a faces message
   }
   catch(EJBTransactionRolledbackException e){
      success = false;
      //add another faces message
   }
   return success;
}

just as a small info this is the code used for the view-action save:

/**
* Triggers the save process as business logic and uses the result to determine
* what to return
* @return Navigation rule as String depending on the outcome of the save process
*/
public String save() {
   return processSave() ? navigationRuleSuccess : navigationRuleFailure;
}
Vogel612
  • 5,620
  • 5
  • 48
  • 73