HV-361 Making sure that multiple column checks are properly added

This commit is contained in:
Hardy Ferentschik 2010-11-10 19:38:47 +01:00
parent c22a6be533
commit c07ee1a4c0
3 changed files with 168 additions and 59 deletions

View File

@ -24,6 +24,7 @@
package org.hibernate.cfg.beanvalidation;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
@ -31,10 +32,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Collection;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Digits;
@ -42,6 +39,9 @@ import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -59,6 +59,7 @@ import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.SingleTableSubclass;
import org.hibernate.util.ReflectHelper;
import org.hibernate.util.StringHelper;
/**
* @author Emmanuel Bernard
@ -72,7 +73,9 @@ class TypeSafeActivator {
public static void activateBeanValidation(EventListeners eventListeners, Properties properties) {
ValidatorFactory factory = getValidatorFactory( properties );
BeanValidationEventListener beanValidationEventListener = new BeanValidationEventListener( factory, properties );
BeanValidationEventListener beanValidationEventListener = new BeanValidationEventListener(
factory, properties
);
{
PreInsertEventListener[] listeners = eventListeners.getPreInsertEventListeners();
@ -110,7 +113,9 @@ class TypeSafeActivator {
for ( PersistentClass persistentClass : persistentClasses ) {
final String className = persistentClass.getClassName();
if ( className == null || className.length() == 0) continue;
if ( className == null || className.length() == 0 ) {
continue;
}
Class<?> clazz;
try {
clazz = ReflectHelper.classForName( className, TypeSafeActivator.class );
@ -141,7 +146,9 @@ class TypeSafeActivator {
Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() );
boolean hasNotNull;
if ( property != null ) {
hasNotNull = applyConstraints( propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull );
hasNotNull = applyConstraints(
propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull
);
if ( property.isComposite() && propertyDesc.isCascaded() ) {
Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass();
@ -151,7 +158,8 @@ class TypeSafeActivator {
* Otherwise, all sub columns should be left nullable
*/
final boolean canSetNotNullOnColumns = activateNotNull && hasNotNull;
applyDDL( prefix + propertyDesc.getPropertyName() + ".",
applyDDL(
prefix + propertyDesc.getPropertyName() + ".",
persistentClass, componentClass, factory, groups,
canSetNotNullOnColumns
);
@ -167,7 +175,9 @@ class TypeSafeActivator {
Set<Class<?>> groups, boolean canApplyNotNull) {
boolean hasNotNull = false;
for ( ConstraintDescriptor<?> descriptor : constraintDescriptors ) {
if ( groups != null && Collections.disjoint( descriptor.getGroups(), groups) ) continue;
if ( groups != null && Collections.disjoint( descriptor.getGroups(), groups ) ) {
continue;
}
if ( canApplyNotNull ) {
hasNotNull = hasNotNull || applyNotNull( property, descriptor );
@ -186,7 +196,8 @@ class TypeSafeActivator {
hasNotNull = hasNotNull || applyConstraints(
descriptor.getComposingConstraints(),
property, propertyDesc, null,
canApplyNotNull );
canApplyNotNull
);
}
return hasNotNull;
}
@ -196,8 +207,10 @@ class TypeSafeActivator {
@SuppressWarnings("unchecked")
ConstraintDescriptor<Min> minConstraint = (ConstraintDescriptor<Min>) descriptor;
long min = minConstraint.getAnnotation().value();
Column col = (Column) property.getColumnIterator().next();
col.setCheckConstraint( col.getName() + ">=" + min );
String checkConstraint = col.getName() + ">=" + min;
applySQLCheck( col, checkConstraint );
}
}
@ -207,10 +220,18 @@ class TypeSafeActivator {
ConstraintDescriptor<Max> maxConstraint = (ConstraintDescriptor<Max>) descriptor;
long max = maxConstraint.getAnnotation().value();
Column col = (Column) property.getColumnIterator().next();
col.setCheckConstraint( col.getName() + "<=" + max );
String checkConstraint = col.getName() + "<=" + max;
applySQLCheck( col, checkConstraint );
}
}
private static void applySQLCheck(Column col, String checkConstraint) {
if ( StringHelper.isNotEmpty( col.getCheckConstraint() ) ) {
checkConstraint = col.getCheckConstraint() + " AND " + checkConstraint;
}
col.setCheckConstraint( checkConstraint );
}
private static boolean applyNotNull(Property property, ConstraintDescriptor<?> descriptor) {
boolean hasNotNull = false;
if ( NotNull.class.equals( descriptor.getAnnotation().annotationType() ) ) {
@ -256,7 +277,9 @@ class TypeSafeActivator {
}
private static void applyLength(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
if ( "org.hibernate.validator.constraints.Length".equals(descriptor.getAnnotation().annotationType().getName())
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" );
@ -294,7 +317,9 @@ class TypeSafeActivator {
property = associatedClass.getProperty( element );
}
else {
if ( ! property.isComposite() ) return null;
if ( !property.isComposite() ) {
return null;
}
property = ( (Component) property.getValue() ).getProperty( element );
}
}
@ -303,7 +328,9 @@ class TypeSafeActivator {
catch ( MappingException e ) {
try {
//if we do not find it try to check the identifier mapper
if ( associatedClass.getIdentifierMapper() == null ) return null;
if ( associatedClass.getIdentifierMapper() == null ) {
return null;
}
StringTokenizer st = new StringTokenizer( propertyName, ".", false );
while ( st.hasMoreElements() ) {
String element = (String) st.nextElement();
@ -311,7 +338,9 @@ class TypeSafeActivator {
property = associatedClass.getIdentifierMapper().getProperty( element );
}
else {
if ( ! property.isComposite() ) return null;
if ( !property.isComposite() ) {
return null;
}
property = ( (Component) property.getValue() ).getProperty( element );
}
}
@ -332,8 +361,10 @@ class TypeSafeActivator {
factory = ValidatorFactory.class.cast( unsafeProperty );
}
catch ( ClassCastException e ) {
throw new HibernateException( "Property " + FACTORY_PROPERTY
+ " should contain an object of type " + ValidatorFactory.class.getName() );
throw new HibernateException(
"Property " + FACTORY_PROPERTY
+ " should contain an object of type " + ValidatorFactory.class.getName()
);
}
}
}

View File

@ -38,31 +38,29 @@ import org.hibernate.testing.junit.RequiresDialectFeature;
/**
* @author Vladimir Klyushnikov
* @author Hardy Ferentschik
*/
public class DDLWithoutCallbackTest extends TestCase {
@RequiresDialectFeature(value = DialectChecks.SupportsColumnCheck.class,
comment = "Not all databases support column checks")
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
public void testListeners() {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) );
assertDatabaseConstraintViolationThrown( ch );
}
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
public void testMinAndMaxChecksGetApplied() {
MinMax minMax = new MinMax(1);
assertDatabaseConstraintViolationThrown( minMax );
minMax = new MinMax(11);
assertDatabaseConstraintViolationThrown( minMax );
minMax = new MinMax(5);
Session s = openSession();
Transaction tx = s.beginTransaction();
try {
s.persist( ch );
s.persist( minMax );
s.flush();
fail( "expecting SQL constraint violation" );
}
catch ( ConstraintViolationException e ) {
fail( "invalid object should not be validated" );
}
catch ( org.hibernate.exception.ConstraintViolationException e ) {
if ( getDialect().supportsColumnCheck() ) {
// expected
}
else {
fail( "Unexpected SQL constraint violation [" + e.getConstraintName() + "] : " + e.getSQLException() );
}
}
tx.rollback();
s.close();
}
@ -82,7 +80,31 @@ public class DDLWithoutCallbackTest extends TestCase {
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Address.class,
CupHolder.class
CupHolder.class,
MinMax.class
};
}
private void assertDatabaseConstraintViolationThrown(Object o) {
Session s = openSession();
Transaction tx = s.beginTransaction();
try {
s.persist( o );
s.flush();
fail( "expecting SQL constraint violation" );
}
catch ( ConstraintViolationException e ) {
fail( "invalid object should not be validated" );
}
catch ( org.hibernate.exception.ConstraintViolationException e ) {
if ( getDialect().supportsColumnCheck() ) {
// expected
}
else {
fail( "Unexpected SQL constraint violation [" + e.getConstraintName() + "] : " + e.getSQLException() );
}
}
tx.rollback();
s.close();
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by
* third-party contributors as indicated by either @author tags or express
* copyright attribution statements applied by the authors. All
* third-party contributions are distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.beanvalidation;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
/**
* @author Hardy Ferentschik
*/
@Entity
public class MinMax {
@Id
@GeneratedValue
private Long id;
@Max(10)
@Min(2)
private Integer value;
private MinMax() {
}
public MinMax(Integer value) {
this.value = value;
}
}