HV-361 Making sure that multiple column checks are properly added
This commit is contained in:
parent
c22a6be533
commit
c07ee1a4c0
|
@ -24,6 +24,7 @@
|
||||||
package org.hibernate.cfg.beanvalidation;
|
package org.hibernate.cfg.beanvalidation;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -31,10 +32,6 @@ import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.StringTokenizer;
|
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.Validation;
|
||||||
import javax.validation.ValidatorFactory;
|
import javax.validation.ValidatorFactory;
|
||||||
import javax.validation.constraints.Digits;
|
import javax.validation.constraints.Digits;
|
||||||
|
@ -42,6 +39,9 @@ import javax.validation.constraints.Max;
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -59,6 +59,7 @@ import org.hibernate.mapping.PersistentClass;
|
||||||
import org.hibernate.mapping.Property;
|
import org.hibernate.mapping.Property;
|
||||||
import org.hibernate.mapping.SingleTableSubclass;
|
import org.hibernate.mapping.SingleTableSubclass;
|
||||||
import org.hibernate.util.ReflectHelper;
|
import org.hibernate.util.ReflectHelper;
|
||||||
|
import org.hibernate.util.StringHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
|
@ -72,7 +73,9 @@ class TypeSafeActivator {
|
||||||
|
|
||||||
public static void activateBeanValidation(EventListeners eventListeners, Properties properties) {
|
public static void activateBeanValidation(EventListeners eventListeners, Properties properties) {
|
||||||
ValidatorFactory factory = getValidatorFactory( properties );
|
ValidatorFactory factory = getValidatorFactory( properties );
|
||||||
BeanValidationEventListener beanValidationEventListener = new BeanValidationEventListener( factory, properties );
|
BeanValidationEventListener beanValidationEventListener = new BeanValidationEventListener(
|
||||||
|
factory, properties
|
||||||
|
);
|
||||||
|
|
||||||
{
|
{
|
||||||
PreInsertEventListener[] listeners = eventListeners.getPreInsertEventListeners();
|
PreInsertEventListener[] listeners = eventListeners.getPreInsertEventListeners();
|
||||||
|
@ -110,7 +113,9 @@ class TypeSafeActivator {
|
||||||
for ( PersistentClass persistentClass : persistentClasses ) {
|
for ( PersistentClass persistentClass : persistentClasses ) {
|
||||||
final String className = persistentClass.getClassName();
|
final String className = persistentClass.getClassName();
|
||||||
|
|
||||||
if ( className == null || className.length() == 0) continue;
|
if ( className == null || className.length() == 0 ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
Class<?> clazz;
|
Class<?> clazz;
|
||||||
try {
|
try {
|
||||||
clazz = ReflectHelper.classForName( className, TypeSafeActivator.class );
|
clazz = ReflectHelper.classForName( className, TypeSafeActivator.class );
|
||||||
|
@ -141,7 +146,9 @@ class TypeSafeActivator {
|
||||||
Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() );
|
Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() );
|
||||||
boolean hasNotNull;
|
boolean hasNotNull;
|
||||||
if ( property != null ) {
|
if ( property != null ) {
|
||||||
hasNotNull = applyConstraints( propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull );
|
hasNotNull = applyConstraints(
|
||||||
|
propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull
|
||||||
|
);
|
||||||
if ( property.isComposite() && propertyDesc.isCascaded() ) {
|
if ( property.isComposite() && propertyDesc.isCascaded() ) {
|
||||||
Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass();
|
Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass();
|
||||||
|
|
||||||
|
@ -151,7 +158,8 @@ class TypeSafeActivator {
|
||||||
* Otherwise, all sub columns should be left nullable
|
* Otherwise, all sub columns should be left nullable
|
||||||
*/
|
*/
|
||||||
final boolean canSetNotNullOnColumns = activateNotNull && hasNotNull;
|
final boolean canSetNotNullOnColumns = activateNotNull && hasNotNull;
|
||||||
applyDDL( prefix + propertyDesc.getPropertyName() + ".",
|
applyDDL(
|
||||||
|
prefix + propertyDesc.getPropertyName() + ".",
|
||||||
persistentClass, componentClass, factory, groups,
|
persistentClass, componentClass, factory, groups,
|
||||||
canSetNotNullOnColumns
|
canSetNotNullOnColumns
|
||||||
);
|
);
|
||||||
|
@ -167,7 +175,9 @@ class TypeSafeActivator {
|
||||||
Set<Class<?>> groups, boolean canApplyNotNull) {
|
Set<Class<?>> groups, boolean canApplyNotNull) {
|
||||||
boolean hasNotNull = false;
|
boolean hasNotNull = false;
|
||||||
for ( ConstraintDescriptor<?> descriptor : constraintDescriptors ) {
|
for ( ConstraintDescriptor<?> descriptor : constraintDescriptors ) {
|
||||||
if ( groups != null && Collections.disjoint( descriptor.getGroups(), groups) ) continue;
|
if ( groups != null && Collections.disjoint( descriptor.getGroups(), groups ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ( canApplyNotNull ) {
|
if ( canApplyNotNull ) {
|
||||||
hasNotNull = hasNotNull || applyNotNull( property, descriptor );
|
hasNotNull = hasNotNull || applyNotNull( property, descriptor );
|
||||||
|
@ -186,7 +196,8 @@ class TypeSafeActivator {
|
||||||
hasNotNull = hasNotNull || applyConstraints(
|
hasNotNull = hasNotNull || applyConstraints(
|
||||||
descriptor.getComposingConstraints(),
|
descriptor.getComposingConstraints(),
|
||||||
property, propertyDesc, null,
|
property, propertyDesc, null,
|
||||||
canApplyNotNull );
|
canApplyNotNull
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return hasNotNull;
|
return hasNotNull;
|
||||||
}
|
}
|
||||||
|
@ -196,8 +207,10 @@ class TypeSafeActivator {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
ConstraintDescriptor<Min> minConstraint = (ConstraintDescriptor<Min>) descriptor;
|
ConstraintDescriptor<Min> minConstraint = (ConstraintDescriptor<Min>) descriptor;
|
||||||
long min = minConstraint.getAnnotation().value();
|
long min = minConstraint.getAnnotation().value();
|
||||||
|
|
||||||
Column col = (Column) property.getColumnIterator().next();
|
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;
|
ConstraintDescriptor<Max> maxConstraint = (ConstraintDescriptor<Max>) descriptor;
|
||||||
long max = maxConstraint.getAnnotation().value();
|
long max = maxConstraint.getAnnotation().value();
|
||||||
Column col = (Column) property.getColumnIterator().next();
|
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) {
|
private static boolean applyNotNull(Property property, ConstraintDescriptor<?> descriptor) {
|
||||||
boolean hasNotNull = false;
|
boolean hasNotNull = false;
|
||||||
if ( NotNull.class.equals( descriptor.getAnnotation().annotationType() ) ) {
|
if ( NotNull.class.equals( descriptor.getAnnotation().annotationType() ) ) {
|
||||||
|
@ -256,7 +277,9 @@ class TypeSafeActivator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void applyLength(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
|
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() ) ) {
|
&& String.class.equals( propertyDescriptor.getElementClass() ) ) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
int max = (Integer) descriptor.getAttributes().get( "max" );
|
int max = (Integer) descriptor.getAttributes().get( "max" );
|
||||||
|
@ -294,7 +317,9 @@ class TypeSafeActivator {
|
||||||
property = associatedClass.getProperty( element );
|
property = associatedClass.getProperty( element );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ( ! property.isComposite() ) return null;
|
if ( !property.isComposite() ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
property = ( (Component) property.getValue() ).getProperty( element );
|
property = ( (Component) property.getValue() ).getProperty( element );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,7 +328,9 @@ class TypeSafeActivator {
|
||||||
catch ( MappingException e ) {
|
catch ( MappingException e ) {
|
||||||
try {
|
try {
|
||||||
//if we do not find it try to check the identifier mapper
|
//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 );
|
StringTokenizer st = new StringTokenizer( propertyName, ".", false );
|
||||||
while ( st.hasMoreElements() ) {
|
while ( st.hasMoreElements() ) {
|
||||||
String element = (String) st.nextElement();
|
String element = (String) st.nextElement();
|
||||||
|
@ -311,7 +338,9 @@ class TypeSafeActivator {
|
||||||
property = associatedClass.getIdentifierMapper().getProperty( element );
|
property = associatedClass.getIdentifierMapper().getProperty( element );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ( ! property.isComposite() ) return null;
|
if ( !property.isComposite() ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
property = ( (Component) property.getValue() ).getProperty( element );
|
property = ( (Component) property.getValue() ).getProperty( element );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -332,8 +361,10 @@ class TypeSafeActivator {
|
||||||
factory = ValidatorFactory.class.cast( unsafeProperty );
|
factory = ValidatorFactory.class.cast( unsafeProperty );
|
||||||
}
|
}
|
||||||
catch ( ClassCastException e ) {
|
catch ( ClassCastException e ) {
|
||||||
throw new HibernateException( "Property " + FACTORY_PROPERTY
|
throw new HibernateException(
|
||||||
+ " should contain an object of type " + ValidatorFactory.class.getName() );
|
"Property " + FACTORY_PROPERTY
|
||||||
|
+ " should contain an object of type " + ValidatorFactory.class.getName()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,31 +38,29 @@ import org.hibernate.testing.junit.RequiresDialectFeature;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vladimir Klyushnikov
|
* @author Vladimir Klyushnikov
|
||||||
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public class DDLWithoutCallbackTest extends TestCase {
|
public class DDLWithoutCallbackTest extends TestCase {
|
||||||
@RequiresDialectFeature(value = DialectChecks.SupportsColumnCheck.class,
|
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
|
||||||
comment = "Not all databases support column checks")
|
|
||||||
public void testListeners() {
|
public void testListeners() {
|
||||||
CupHolder ch = new CupHolder();
|
CupHolder ch = new CupHolder();
|
||||||
ch.setRadius( new BigDecimal( "12" ) );
|
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();
|
Session s = openSession();
|
||||||
Transaction tx = s.beginTransaction();
|
Transaction tx = s.beginTransaction();
|
||||||
try {
|
s.persist( minMax );
|
||||||
s.persist( ch );
|
|
||||||
s.flush();
|
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();
|
tx.rollback();
|
||||||
s.close();
|
s.close();
|
||||||
}
|
}
|
||||||
|
@ -82,7 +80,31 @@ public class DDLWithoutCallbackTest extends TestCase {
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
return new Class<?>[] {
|
return new Class<?>[] {
|
||||||
Address.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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue