HHH-5281 apply @Length ddl constraints (without loading HV classes)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19747 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2010-06-16 10:43:40 +00:00
parent c0e94443b1
commit ef90b00b14
3 changed files with 61 additions and 15 deletions

View File

@ -39,6 +39,7 @@ import org.hibernate.util.ReflectHelper;
/**
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
class TypeSafeActivator {
@ -148,12 +149,17 @@ class TypeSafeActivator {
if ( canApplyNotNull ) {
hasNotNull = hasNotNull || applyNotNull( property, descriptor );
}
// apply bean validation specific constraints
applyDigits( property, descriptor );
applySize( property, descriptor, propertyDesc );
applyMin( property, descriptor );
applyMax( property, descriptor );
//pass an empty set as composing constraints inherit the main constraint and thus are matching already
// apply hibernate validator specific constraints - we cannot import any HV specific classes though!
applyLength( property, descriptor, propertyDesc );
// pass an empty set as composing constraints inherit the main constraint and thus are matching already
hasNotNull = hasNotNull || applyConstraints(
descriptor.getComposingConstraints(),
property, propertyDesc, null,
@ -213,14 +219,28 @@ class TypeSafeActivator {
}
}
private static void applySize(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDesc) {
private static void applySize(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
if ( Size.class.equals( descriptor.getAnnotation().annotationType() )
&& String.class.equals( propertyDesc.getElementClass() ) ) {
@SuppressWarnings( "unchecked" )
ConstraintDescriptor<Size> sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
&& String.class.equals( propertyDescriptor.getElementClass() ) ) {
@SuppressWarnings("unchecked")
ConstraintDescriptor<Size> sizeConstraint = ( ConstraintDescriptor<Size> ) descriptor;
int max = sizeConstraint.getAnnotation().max();
Column col = (Column) property.getColumnIterator().next();
if ( max < Integer.MAX_VALUE ) col.setLength( max );
Column col = ( Column ) property.getColumnIterator().next();
if ( max < Integer.MAX_VALUE ) {
col.setLength( max );
}
}
}
private static void applyLength(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
if ( "org.hibernate.validator.constraints.Length".equals(descriptor.getAnnotation().annotationType().getName())
&& String.class.equals( propertyDescriptor.getElementClass() ) ) {
@SuppressWarnings("unchecked")
int max = (Integer) descriptor.getAttributes().get( "max" );
Column col = ( Column ) property.getColumnIterator().next();
if ( max < Integer.MAX_VALUE ) {
col.setLength( max );
}
}
}

View File

@ -6,13 +6,15 @@ import org.hibernate.mapping.Property;
import org.hibernate.test.annotations.TestCase;
/**
* Test verifying that DDL constraints get applied when Bean Validation / Hibernate Validator are enabled.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
public class DDLTest extends TestCase {
public void testBasicDDL() {
PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() );
//new ClassValidator( Address.class, ResourceBundle.getBundle("messages", Locale.ENGLISH) ).apply( classMapping );
Column stateColumn = (Column) classMapping.getProperty( "state" ).getColumnIterator().next();
assertEquals( stateColumn.getLength(), 3 );
Column zipColumn = (Column) classMapping.getProperty( "zip" ).getColumnIterator().next();
@ -23,9 +25,20 @@ public class DDLTest extends TestCase {
public void testApplyOnIdColumn() throws Exception {
PersistentClass classMapping = getCfg().getClassMapping( Tv.class.getName() );
Column serialColumn = (Column) classMapping.getIdentifierProperty().getColumnIterator().next();
assertEquals( "Vaidator annotation not applied on ids", 2, serialColumn.getLength() );
assertEquals( "Validator annotation not applied on ids", 2, serialColumn.getLength() );
}
/**
* HHH-5281
*
* @throws Exception in case the test fails
*/
public void testLengthConstraint() throws Exception {
PersistentClass classMapping = getCfg().getClassMapping( Tv.class.getName() );
Column modelColumn = (Column) classMapping.getProperty( "model" ).getColumnIterator().next();
assertEquals( modelColumn.getLength(), 5 );
}
public void testApplyOnManyToOne() throws Exception {
PersistentClass classMapping = getCfg().getClassMapping( TvOwner.class.getName() );
Column serialColumn = (Column) classMapping.getProperty( "tv" ).getColumnIterator().next();

View File

@ -1,19 +1,22 @@
package org.hibernate.test.annotations.beanvalidation;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Embeddable;
import javax.validation.Valid;
import javax.validation.constraints.Future;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import javax.validation.constraints.NotNull;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Length;
/**
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
@Entity
public class Tv {
@ -21,17 +24,28 @@ public class Tv {
@Id
@Size(max = 2)
public String serial;
@Length(max=5)
public String model;
public int size;
@Size(max = 2)
public String name;
@Future
public Date expDate;
@Size(min = 0)
public String description;
@Min(1000)
public BigInteger lifetime;
@NotNull @Valid
@NotNull
@Valid
public Tuner tuner;
@Valid
public Recorder recorder;
@ -46,5 +60,4 @@ public class Tv {
@NotNull
public BigDecimal time;
}
}