From fcf402c4af5b9983fb2d0b32b459dbb1efac6b76 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Mon, 17 Oct 2011 16:34:35 +0800 Subject: [PATCH] HHH-6737 column names in Constraint is not quoted even column name is quoted in mapping --- .../BeanValidationIntegrator.java | 17 ++++++---- .../cfg/beanvalidation/TypeSafeActivator.java | 31 +++++++++++-------- .../annotations/beanvalidation/Range.java | 2 ++ .../annotations/collectionelement/Boy.java | 1 + 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java index 6a3d5ef930..0b45f4c18b 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java @@ -33,6 +33,8 @@ import org.jboss.logging.Logger; import org.hibernate.HibernateException; +import org.hibernate.dialect.Dialect; +import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.integrator.spi.Integrator; import org.hibernate.internal.CoreMessageLogger; @@ -106,7 +108,7 @@ public void integrate( final Set modes = ValidationMode.getModes( configuration.getProperties().get( MODE_PROPERTY ) ); final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); - + Dialect dialect = serviceRegistry.getService( JdbcServices.class ).getDialect(); // try to locate a BV class to see if it is available on the classpath boolean isBeanValidationAvailable; try { @@ -121,12 +123,13 @@ public void integrate( final Class typeSafeActivatorClass = loadTypeSafeActivatorClass( serviceRegistry ); // todo : if this works out, probably better to simply alter TypeSafeActivator into a single method... - applyRelationalConstraints( modes, isBeanValidationAvailable, typeSafeActivatorClass, - configuration + configuration, + dialect + ); applyHibernateListeners( modes, @@ -178,7 +181,8 @@ private void applyRelationalConstraints( Set modes, boolean beanValidationAvailable, Class typeSafeActivatorClass, - Configuration configuration) { + Configuration configuration, + Dialect dialect) { if ( ! ConfigurationHelper.getBoolean( APPLY_CONSTRAINTS, configuration.getProperties(), true ) ){ LOG.debug( "Skipping application of relational constraints from legacy Hibernate Validator" ); return; @@ -199,12 +203,13 @@ else if (modes.contains( ValidationMode.AUTO ) ) { } try { - Method applyDDLMethod = typeSafeActivatorClass.getMethod( DDL_METHOD, Collection.class, Properties.class ); + Method applyDDLMethod = typeSafeActivatorClass.getMethod( DDL_METHOD, Collection.class, Properties.class, Dialect.class ); try { applyDDLMethod.invoke( null, configuration.createMappings().getClasses().values(), - configuration.getProperties() + configuration.getProperties(), + dialect ); } catch (HibernateException e) { diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java index f46a38b305..3c6e4be460 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java @@ -46,6 +46,7 @@ import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.cfg.Configuration; +import org.hibernate.dialect.Dialect; import org.hibernate.event.service.spi.EventListenerRegistry; import org.hibernate.event.spi.EventType; import org.hibernate.internal.CoreMessageLogger; @@ -112,7 +113,7 @@ public static void activateBeanValidation(EventListenerRegistry listenerRegistry // } @SuppressWarnings( {"UnusedDeclaration"}) - public static void applyDDL(Collection persistentClasses, Properties properties) { + public static void applyDDL(Collection persistentClasses, Properties properties, Dialect dialect) { ValidatorFactory factory = getValidatorFactory( properties ); Class[] groupsArray = new GroupsPerOperation( properties ).get( GroupsPerOperation.Operation.DDL ); Set> groups = new HashSet>( Arrays.asList( groupsArray ) ); @@ -132,7 +133,7 @@ public static void applyDDL(Collection persistentClasses, Prope } try { - applyDDL( "", persistentClass, clazz, factory, groups, true ); + applyDDL( "", persistentClass, clazz, factory, groups, true, dialect ); } catch (Exception e) { LOG.unableToApplyConstraints(className, e); @@ -164,7 +165,8 @@ private static void applyDDL(String prefix, Class clazz, ValidatorFactory factory, Set> groups, - boolean activateNotNull) { + boolean activateNotNull, + Dialect dialect) { final BeanDescriptor descriptor = factory.getValidator().getConstraintsForClass( clazz ); //no bean level constraints can be applied, go to the properties @@ -173,7 +175,7 @@ private static void applyDDL(String prefix, boolean hasNotNull; if ( property != null ) { hasNotNull = applyConstraints( - propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull + propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull, dialect ); if ( property.isComposite() && propertyDesc.isCascaded() ) { Class componentClass = ( (Component) property.getValue() ).getComponentClass(); @@ -187,7 +189,8 @@ private static void applyDDL(String prefix, applyDDL( prefix + propertyDesc.getPropertyName() + ".", persistentClass, componentClass, factory, groups, - canSetNotNullOnColumns + canSetNotNullOnColumns, + dialect ); } //FIXME add collection of components @@ -216,7 +219,8 @@ private static boolean applyConstraints(Set> constraintD Property property, PropertyDescriptor propertyDesc, Set> groups, - boolean canApplyNotNull + boolean canApplyNotNull, + Dialect dialect ) { boolean hasNotNull = false; for ( ConstraintDescriptor descriptor : constraintDescriptors ) { @@ -231,8 +235,8 @@ private static boolean applyConstraints(Set> constraintD // apply bean validation specific constraints applyDigits( property, descriptor ); applySize( property, descriptor, propertyDesc ); - applyMin( property, descriptor ); - applyMax( property, descriptor ); + applyMin( property, descriptor, dialect ); + applyMax( property, descriptor, dialect ); // apply hibernate validator specific constraints - we cannot import any HV specific classes though! // no need to check explicitly for @Range. @Range is a composed constraint using @Min and @Max which @@ -243,7 +247,8 @@ private static boolean applyConstraints(Set> constraintD hasNotNull = hasNotNull || applyConstraints( descriptor.getComposingConstraints(), property, propertyDesc, null, - canApplyNotNull + canApplyNotNull, + dialect ); } return hasNotNull; @@ -280,25 +285,25 @@ private static boolean applyConstraints(Set> constraintD // return hasNotNull; // } - private static void applyMin(Property property, ConstraintDescriptor descriptor) { + private static void applyMin(Property property, ConstraintDescriptor descriptor, Dialect dialect) { if ( Min.class.equals( descriptor.getAnnotation().annotationType() ) ) { @SuppressWarnings("unchecked") ConstraintDescriptor minConstraint = (ConstraintDescriptor) descriptor; long min = minConstraint.getAnnotation().value(); Column col = (Column) property.getColumnIterator().next(); - String checkConstraint = col.getName() + ">=" + min; + String checkConstraint = col.getQuotedName(dialect) + ">=" + min; applySQLCheck( col, checkConstraint ); } } - private static void applyMax(Property property, ConstraintDescriptor descriptor) { + private static void applyMax(Property property, ConstraintDescriptor descriptor, Dialect dialect) { if ( Max.class.equals( descriptor.getAnnotation().annotationType() ) ) { @SuppressWarnings("unchecked") ConstraintDescriptor maxConstraint = (ConstraintDescriptor) descriptor; long max = maxConstraint.getAnnotation().value(); Column col = (Column) property.getColumnIterator().next(); - String checkConstraint = col.getName() + "<=" + max; + String checkConstraint = col.getQuotedName(dialect) + "<=" + max; applySQLCheck( col, checkConstraint ); } } diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/beanvalidation/Range.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/beanvalidation/Range.java index 32b75e239e..5d2a3a0316 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/beanvalidation/Range.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/beanvalidation/Range.java @@ -23,6 +23,7 @@ */ package org.hibernate.test.annotations.beanvalidation; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @@ -38,6 +39,7 @@ public class Range { private Long id; @org.hibernate.validator.constraints.Range(min = 2, max = 10) + @Column(name = "`value`") private Integer value; private Range() { diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/collectionelement/Boy.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/collectionelement/Boy.java index 2fc04de53e..46bdbca0e5 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/collectionelement/Boy.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/collectionelement/Boy.java @@ -147,6 +147,7 @@ public void setFavoriteToys(Set favoriteToys) { @ElementCollection @Enumerated(EnumType.STRING) + @Column(name = "`characters`") public Set getCharacters() { return characters; }