Announcement Announcement Module
No announcement yet.
Annotation driven JSR-303 validation on service and dao tier Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation driven JSR-303 validation on service and dao tier

    Hi all,
    I'm building our own DAO tier based on a Datanucleus plugin I've written and Cassandra. I want to use the Hibernate implementation of the validator to validate objects before they're persisted in the data access tier. However, my objects are never validated before they're persisted. I have the following basic User model.

    public class User {
    	private String email;
    	private String password;
    	private String firstName;
    	private String lastName;
    //getters and setters..
    In my DAO, I have this implementation

    	public void save(@Valid T model) {
    However, my model is never validated. Here is what I have in my context configuration.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	xmlns:xsi="" xmlns:context=""
    	<!--  Tell spring to scan our classes for annotations -->
    	<context:component-scan base-package="com.spidertracks.dataaccess" />
    	<bean id="pmf"
    		<property name="jdoProperties">
    				<prop key="javax.jdo.PersistenceManagerFactoryClass">org.datanucleus.jdo.JDOPersistenceManagerFactory
    				<prop key="javax.jdo.option.ConnectionURL">${cassandra.url}</prop>
    	<!-- Transaction Manager for PMF -->
    	<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
    		<property name="persistenceManagerFactory">
    			<ref local="pmf" />
    	<!--  tell our transaction interceptors to look at our annotations -->
    	<tx:annotation-driven transaction-manager="transactionManager" />
    	<!--  set up our JSR 303 validator to use annotations -->
    	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    		references a properties file on the classpath. Set up internally for
    		unit testing, but overridden by each node individually
    	<bean id="config"
    		<property name="location"
    <aop:config proxy-target-class="true">
    		<aop:pointcut id="validatableMethodExecution"
    			expression="execution(* com.spidertracks.dataaccess..*.*(..)) and @annotation(javax.validation.Valid)" />
    		<aop:aspect id="validateArgumentsAndReturnObject" ref="validatorAspect">
    			<aop:around pointcut-ref="validatableMethodExecution" method="validate" />
    	<!----> <bean id="validatorAspect" class="com.spidertracks.aviator.dataaccess.ModelValidatorAspect" ></bean>
    Here is my pointcut

    public class ModelValidatorAspect {
    	private Validator validator;
        public Object validate(ProceedingJoinPoint pjp) throws Throwable {
        	System.out.println("DOING INTERCEPTION!");
            Set<ConstraintViolation> violations =new HashSet<ConstraintViolation>();
            for(Object obj : pjp.getArgs()) {
                if(obj != null) {
            if(!violations.isEmpty()) {
               throw new ValidationException();
            Object ret = pjp.proceed();
            return ret;
    As you can see I've marked my User argument to my save method to be validated. However it isn't validated. What other setup do I need to do to get my validation working? Note that I'm using validation on my Service and DAO tiers, not on the web tier with MVC.

    Also, we'll be using Wicket for our web tier. A plugin already exists.

    Is it possible for my business to tier to add extra validation errors? For instance, when the user changes their password, if the current password supplied is incorrect, the input fails validation in the change password use case.

    In regards to getting things working on the business tier, I've tried the code from this blog, but it doesn't seem to work.

    When I apply my own aop interception, it's never invoked. It's like it's not picking up on my validation of my method arguments.


    Last edited by tnine; May 31st, 2010, 06:05 AM. Reason: updated to appropriate title

  • #2
    Anyone? I'm a bit stuck, help would be greatly appreciated.


    • #3
      Is your ModelValidatorAspect actually getting invoked? I'm guessing not, since you have a System.exit() in your example code.

      Your pointcut expression doesn't look quite right to me:

      execution(* com.spidertracks.dataaccess..*.*(..)) and @annotation(javax.validation.Valid)
      I think this will intercept any method in the specified package where the @Valid annotation is applied to the method. In your case, you don't have the @Valid annotation on the method itself, but on one of the arguments to the method. Try moving the annotation to the method and see if that makes the pointcut work the way you are expecting.