-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: New Annotations
PostPosted: Fri Dec 24, 2010 7:17 am 
Beginner
Beginner

Joined: Sat Oct 09, 2004 2:35 pm
Posts: 43
Location: Tenerife
Hi,

any way that new annotations (made by users) be used to generate ddl using SchemaExport?

Using Hibernate-validator 3.x I had this annotation

Code:
package com.apl.base.annotation;

import org.hibernate.validator.ValidatorClass;

import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;


/**
* Default value for column
*
* @author  Jesús Marín
*/
@Documented
@Retention(RUNTIME)
@Target({ METHOD, FIELD })
@ValidatorClass(DefaultValidator.class)
public @interface Default {

    /** regular expression */
    String value();

    String message() default "{validator.default}";
}


validated by

Code:
package com.apl.base.annotation;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;

import org.hibernate.validator.PropertyConstraint;
import org.hibernate.validator.Validator;

import java.io.Serializable;


/**
* generate default constraint
*
* @author  Jesús Marín
*/
public class DefaultValidator
    implements Validator<Default>, PropertyConstraint, Serializable {

    /**
     */
    private static final long serialVersionUID = -5431593412647658561L;

    private String value;

    public void apply(Property property) {
        Column col = (Column) property.getColumnIterator().next();

        if ((value != null) && !"".equals(value)) {
            col.setDefaultValue(value);
        }
    }

    public void initialize(Default parameters) {
        value = parameters.value();
    }

    public boolean isValid(Object value) {
        return true;
    }
}


and everything worked fine...
but as I changed into validator 4.1
and adapted my code to

Code:
package com.apl.base.annotation;

import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;


/**
* Default value for column
*
* @author  Jesús Marín
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Constraint(validatedBy = DefaultValidator.class)
@Documented
public @interface Default {

    /** regular expression */
    String value();

    String message() default "{com.apl.constraints.default}";
   
    Class<?>[] groups() default {};

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


Code:
package com.apl.base.annotation;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
* sets default column constraint
*
* @author  Jesús Marín
*/
public class DefaultValidator
    implements ConstraintValidator<Default, Object>, Serializable {

    /**
     */
    private static final long serialVersionUID = -5431593412647658561L;

    private String value;

//    public void apply(Property property) {
//        Column col = (Column) property.getColumnIterator().next();
//
//        if ((value != null) && !"".equals(value)) {
//            col.setDefaultValue(value);
//        }
//    }

    public void initialize(Default parameters) {
        value = parameters.value();
    }

    public boolean isValid(Object object, ConstraintValidatorContext constraintContext) {
        boolean isValid = false;
        if (object != null)
           isValid = true;
        return isValid;
    }
}


now there's no way that SchemaExport generate this DDL constraint...

any way to add non-official constraints to the list used to generate DDL?

Best regards


Top
 Profile  
 
 Post subject: Re: New Annotations
PostPosted: Wed Dec 29, 2010 1:32 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

There is currently no way to apply DDL constraints from/for custom constraints.
Hibernate Validator 4.x is an implementation of Bean Validation and has as such no dependency to classes like org.hibernate.mapping.Property. It is not really in the scope of Bean Validation to apply database constraints.
A possible solution might be the introduction of a Hibernate Validator specific interface which ConstraintValidator classes can implement. The problem here is to decide what to do if a custom constraint has multiple ConstraintValidator implementations including maybe multiple implementations of this interface. Which one do you apply?
If you are interested in this feature I recommend you to create an issue here - http://opensource.atlassian.com/project ... /browse/HV - to discuss a possible solution. Even better if you could include a patch ;-)

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.