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