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:
parent
c0e94443b1
commit
ef90b00b14
|
@ -39,6 +39,7 @@ import org.hibernate.util.ReflectHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
class TypeSafeActivator {
|
class TypeSafeActivator {
|
||||||
|
|
||||||
|
@ -148,12 +149,17 @@ class TypeSafeActivator {
|
||||||
if ( canApplyNotNull ) {
|
if ( canApplyNotNull ) {
|
||||||
hasNotNull = hasNotNull || applyNotNull( property, descriptor );
|
hasNotNull = hasNotNull || applyNotNull( property, descriptor );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// apply bean validation specific constraints
|
||||||
applyDigits( property, descriptor );
|
applyDigits( property, descriptor );
|
||||||
applySize( property, descriptor, propertyDesc );
|
applySize( property, descriptor, propertyDesc );
|
||||||
applyMin( property, descriptor );
|
applyMin( property, descriptor );
|
||||||
applyMax( 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(
|
hasNotNull = hasNotNull || applyConstraints(
|
||||||
descriptor.getComposingConstraints(),
|
descriptor.getComposingConstraints(),
|
||||||
property, propertyDesc, null,
|
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() )
|
if ( Size.class.equals( descriptor.getAnnotation().annotationType() )
|
||||||
&& String.class.equals( propertyDesc.getElementClass() ) ) {
|
&& String.class.equals( propertyDescriptor.getElementClass() ) ) {
|
||||||
@SuppressWarnings( "unchecked" )
|
@SuppressWarnings("unchecked")
|
||||||
ConstraintDescriptor<Size> sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
|
ConstraintDescriptor<Size> sizeConstraint = ( ConstraintDescriptor<Size> ) descriptor;
|
||||||
int max = sizeConstraint.getAnnotation().max();
|
int max = sizeConstraint.getAnnotation().max();
|
||||||
Column col = (Column) property.getColumnIterator().next();
|
Column col = ( Column ) property.getColumnIterator().next();
|
||||||
if ( max < Integer.MAX_VALUE ) col.setLength( max );
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,15 @@ import org.hibernate.mapping.Property;
|
||||||
import org.hibernate.test.annotations.TestCase;
|
import org.hibernate.test.annotations.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Test verifying that DDL constraints get applied when Bean Validation / Hibernate Validator are enabled.
|
||||||
|
*
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public class DDLTest extends TestCase {
|
public class DDLTest extends TestCase {
|
||||||
|
|
||||||
public void testBasicDDL() {
|
public void testBasicDDL() {
|
||||||
PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() );
|
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();
|
Column stateColumn = (Column) classMapping.getProperty( "state" ).getColumnIterator().next();
|
||||||
assertEquals( stateColumn.getLength(), 3 );
|
assertEquals( stateColumn.getLength(), 3 );
|
||||||
Column zipColumn = (Column) classMapping.getProperty( "zip" ).getColumnIterator().next();
|
Column zipColumn = (Column) classMapping.getProperty( "zip" ).getColumnIterator().next();
|
||||||
|
@ -23,9 +25,20 @@ public class DDLTest extends TestCase {
|
||||||
public void testApplyOnIdColumn() throws Exception {
|
public void testApplyOnIdColumn() throws Exception {
|
||||||
PersistentClass classMapping = getCfg().getClassMapping( Tv.class.getName() );
|
PersistentClass classMapping = getCfg().getClassMapping( Tv.class.getName() );
|
||||||
Column serialColumn = (Column) classMapping.getIdentifierProperty().getColumnIterator().next();
|
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 {
|
public void testApplyOnManyToOne() throws Exception {
|
||||||
PersistentClass classMapping = getCfg().getClassMapping( TvOwner.class.getName() );
|
PersistentClass classMapping = getCfg().getClassMapping( TvOwner.class.getName() );
|
||||||
Column serialColumn = (Column) classMapping.getProperty( "tv" ).getColumnIterator().next();
|
Column serialColumn = (Column) classMapping.getProperty( "tv" ).getColumnIterator().next();
|
||||||
|
|
|
@ -1,19 +1,22 @@
|
||||||
package org.hibernate.test.annotations.beanvalidation;
|
package org.hibernate.test.annotations.beanvalidation;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Embeddable;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.Future;
|
import javax.validation.constraints.Future;
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.Size;
|
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.Valid;
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
public class Tv {
|
public class Tv {
|
||||||
|
@ -21,17 +24,28 @@ public class Tv {
|
||||||
@Id
|
@Id
|
||||||
@Size(max = 2)
|
@Size(max = 2)
|
||||||
public String serial;
|
public String serial;
|
||||||
|
|
||||||
|
@Length(max=5)
|
||||||
|
public String model;
|
||||||
|
|
||||||
public int size;
|
public int size;
|
||||||
|
|
||||||
@Size(max = 2)
|
@Size(max = 2)
|
||||||
public String name;
|
public String name;
|
||||||
|
|
||||||
@Future
|
@Future
|
||||||
public Date expDate;
|
public Date expDate;
|
||||||
|
|
||||||
@Size(min = 0)
|
@Size(min = 0)
|
||||||
public String description;
|
public String description;
|
||||||
|
|
||||||
@Min(1000)
|
@Min(1000)
|
||||||
public BigInteger lifetime;
|
public BigInteger lifetime;
|
||||||
@NotNull @Valid
|
|
||||||
|
@NotNull
|
||||||
|
@Valid
|
||||||
public Tuner tuner;
|
public Tuner tuner;
|
||||||
|
|
||||||
@Valid
|
@Valid
|
||||||
public Recorder recorder;
|
public Recorder recorder;
|
||||||
|
|
||||||
|
@ -46,5 +60,4 @@ public class Tv {
|
||||||
@NotNull
|
@NotNull
|
||||||
public BigDecimal time;
|
public BigDecimal time;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue