HHH-6737 column names in Constraint is not quoted even column name is quoted in mapping

This commit is contained in:
Strong Liu 2011-10-17 16:34:35 +08:00
parent 1acc35ca4a
commit fcf402c4af
4 changed files with 32 additions and 19 deletions

View File

@ -33,6 +33,8 @@
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.HibernateException; 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.engine.spi.SessionFactoryImplementor;
import org.hibernate.integrator.spi.Integrator; import org.hibernate.integrator.spi.Integrator;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
@ -106,7 +108,7 @@ public void integrate(
final Set<ValidationMode> modes = ValidationMode.getModes( configuration.getProperties().get( MODE_PROPERTY ) ); final Set<ValidationMode> modes = ValidationMode.getModes( configuration.getProperties().get( MODE_PROPERTY ) );
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); 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 // try to locate a BV class to see if it is available on the classpath
boolean isBeanValidationAvailable; boolean isBeanValidationAvailable;
try { try {
@ -121,12 +123,13 @@ public void integrate(
final Class typeSafeActivatorClass = loadTypeSafeActivatorClass( serviceRegistry ); final Class typeSafeActivatorClass = loadTypeSafeActivatorClass( serviceRegistry );
// todo : if this works out, probably better to simply alter TypeSafeActivator into a single method... // todo : if this works out, probably better to simply alter TypeSafeActivator into a single method...
applyRelationalConstraints( applyRelationalConstraints(
modes, modes,
isBeanValidationAvailable, isBeanValidationAvailable,
typeSafeActivatorClass, typeSafeActivatorClass,
configuration configuration,
dialect
); );
applyHibernateListeners( applyHibernateListeners(
modes, modes,
@ -178,7 +181,8 @@ private void applyRelationalConstraints(
Set<ValidationMode> modes, Set<ValidationMode> modes,
boolean beanValidationAvailable, boolean beanValidationAvailable,
Class typeSafeActivatorClass, Class typeSafeActivatorClass,
Configuration configuration) { Configuration configuration,
Dialect dialect) {
if ( ! ConfigurationHelper.getBoolean( APPLY_CONSTRAINTS, configuration.getProperties(), true ) ){ if ( ! ConfigurationHelper.getBoolean( APPLY_CONSTRAINTS, configuration.getProperties(), true ) ){
LOG.debug( "Skipping application of relational constraints from legacy Hibernate Validator" ); LOG.debug( "Skipping application of relational constraints from legacy Hibernate Validator" );
return; return;
@ -199,12 +203,13 @@ else if (modes.contains( ValidationMode.AUTO ) ) {
} }
try { try {
Method applyDDLMethod = typeSafeActivatorClass.getMethod( DDL_METHOD, Collection.class, Properties.class ); Method applyDDLMethod = typeSafeActivatorClass.getMethod( DDL_METHOD, Collection.class, Properties.class, Dialect.class );
try { try {
applyDDLMethod.invoke( applyDDLMethod.invoke(
null, null,
configuration.createMappings().getClasses().values(), configuration.createMappings().getClasses().values(),
configuration.getProperties() configuration.getProperties(),
dialect
); );
} }
catch (HibernateException e) { catch (HibernateException e) {

View File

@ -46,6 +46,7 @@
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.event.service.spi.EventListenerRegistry; import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType; import org.hibernate.event.spi.EventType;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
@ -112,7 +113,7 @@ public static void activateBeanValidation(EventListenerRegistry listenerRegistry
// } // }
@SuppressWarnings( {"UnusedDeclaration"}) @SuppressWarnings( {"UnusedDeclaration"})
public static void applyDDL(Collection<PersistentClass> persistentClasses, Properties properties) { public static void applyDDL(Collection<PersistentClass> persistentClasses, Properties properties, Dialect dialect) {
ValidatorFactory factory = getValidatorFactory( properties ); ValidatorFactory factory = getValidatorFactory( properties );
Class<?>[] groupsArray = new GroupsPerOperation( properties ).get( GroupsPerOperation.Operation.DDL ); Class<?>[] groupsArray = new GroupsPerOperation( properties ).get( GroupsPerOperation.Operation.DDL );
Set<Class<?>> groups = new HashSet<Class<?>>( Arrays.asList( groupsArray ) ); Set<Class<?>> groups = new HashSet<Class<?>>( Arrays.asList( groupsArray ) );
@ -132,7 +133,7 @@ public static void applyDDL(Collection<PersistentClass> persistentClasses, Prope
} }
try { try {
applyDDL( "", persistentClass, clazz, factory, groups, true ); applyDDL( "", persistentClass, clazz, factory, groups, true, dialect );
} }
catch (Exception e) { catch (Exception e) {
LOG.unableToApplyConstraints(className, e); LOG.unableToApplyConstraints(className, e);
@ -164,7 +165,8 @@ private static void applyDDL(String prefix,
Class<?> clazz, Class<?> clazz,
ValidatorFactory factory, ValidatorFactory factory,
Set<Class<?>> groups, Set<Class<?>> groups,
boolean activateNotNull) { boolean activateNotNull,
Dialect dialect) {
final BeanDescriptor descriptor = factory.getValidator().getConstraintsForClass( clazz ); final BeanDescriptor descriptor = factory.getValidator().getConstraintsForClass( clazz );
//no bean level constraints can be applied, go to the properties //no bean level constraints can be applied, go to the properties
@ -173,7 +175,7 @@ private static void applyDDL(String prefix,
boolean hasNotNull; boolean hasNotNull;
if ( property != null ) { if ( property != null ) {
hasNotNull = applyConstraints( hasNotNull = applyConstraints(
propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull, dialect
); );
if ( property.isComposite() && propertyDesc.isCascaded() ) { if ( property.isComposite() && propertyDesc.isCascaded() ) {
Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass(); Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass();
@ -187,7 +189,8 @@ private static void applyDDL(String prefix,
applyDDL( applyDDL(
prefix + propertyDesc.getPropertyName() + ".", prefix + propertyDesc.getPropertyName() + ".",
persistentClass, componentClass, factory, groups, persistentClass, componentClass, factory, groups,
canSetNotNullOnColumns canSetNotNullOnColumns,
dialect
); );
} }
//FIXME add collection of components //FIXME add collection of components
@ -216,7 +219,8 @@ private static boolean applyConstraints(Set<ConstraintDescriptor<?>> constraintD
Property property, Property property,
PropertyDescriptor propertyDesc, PropertyDescriptor propertyDesc,
Set<Class<?>> groups, Set<Class<?>> groups,
boolean canApplyNotNull boolean canApplyNotNull,
Dialect dialect
) { ) {
boolean hasNotNull = false; boolean hasNotNull = false;
for ( ConstraintDescriptor<?> descriptor : constraintDescriptors ) { for ( ConstraintDescriptor<?> descriptor : constraintDescriptors ) {
@ -231,8 +235,8 @@ private static boolean applyConstraints(Set<ConstraintDescriptor<?>> constraintD
// apply bean validation specific constraints // 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, dialect );
applyMax( property, descriptor ); applyMax( property, descriptor, dialect );
// apply hibernate validator specific constraints - we cannot import any HV specific classes though! // 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 // 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<ConstraintDescriptor<?>> constraintD
hasNotNull = hasNotNull || applyConstraints( hasNotNull = hasNotNull || applyConstraints(
descriptor.getComposingConstraints(), descriptor.getComposingConstraints(),
property, propertyDesc, null, property, propertyDesc, null,
canApplyNotNull canApplyNotNull,
dialect
); );
} }
return hasNotNull; return hasNotNull;
@ -280,25 +285,25 @@ private static boolean applyConstraints(Set<ConstraintDescriptor<?>> constraintD
// return hasNotNull; // 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() ) ) { if ( Min.class.equals( descriptor.getAnnotation().annotationType() ) ) {
@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();
String checkConstraint = col.getName() + ">=" + min; String checkConstraint = col.getQuotedName(dialect) + ">=" + min;
applySQLCheck( col, checkConstraint ); 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() ) ) { if ( Max.class.equals( descriptor.getAnnotation().annotationType() ) ) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
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();
String checkConstraint = col.getName() + "<=" + max; String checkConstraint = col.getQuotedName(dialect) + "<=" + max;
applySQLCheck( col, checkConstraint ); applySQLCheck( col, checkConstraint );
} }
} }

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.test.annotations.beanvalidation; package org.hibernate.test.annotations.beanvalidation;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -38,6 +39,7 @@ public class Range {
private Long id; private Long id;
@org.hibernate.validator.constraints.Range(min = 2, max = 10) @org.hibernate.validator.constraints.Range(min = 2, max = 10)
@Column(name = "`value`")
private Integer value; private Integer value;
private Range() { private Range() {

View File

@ -147,6 +147,7 @@ public void setFavoriteToys(Set<Toy> favoriteToys) {
@ElementCollection @ElementCollection
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
@Column(name = "`characters`")
public Set<Character> getCharacters() { public Set<Character> getCharacters() {
return characters; return characters;
} }