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.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<ValidationMode> 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<ValidationMode> 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) {

View File

@ -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<PersistentClass> persistentClasses, Properties properties) {
public static void applyDDL(Collection<PersistentClass> persistentClasses, Properties properties, Dialect dialect) {
ValidatorFactory factory = getValidatorFactory( properties );
Class<?>[] groupsArray = new GroupsPerOperation( properties ).get( GroupsPerOperation.Operation.DDL );
Set<Class<?>> groups = new HashSet<Class<?>>( Arrays.asList( groupsArray ) );
@ -132,7 +133,7 @@ public static void applyDDL(Collection<PersistentClass> 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<Class<?>> 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<ConstraintDescriptor<?>> constraintD
Property property,
PropertyDescriptor propertyDesc,
Set<Class<?>> groups,
boolean canApplyNotNull
boolean canApplyNotNull,
Dialect dialect
) {
boolean hasNotNull = false;
for ( ConstraintDescriptor<?> descriptor : constraintDescriptors ) {
@ -231,8 +235,8 @@ private static boolean applyConstraints(Set<ConstraintDescriptor<?>> 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<ConstraintDescriptor<?>> constraintD
hasNotNull = hasNotNull || applyConstraints(
descriptor.getComposingConstraints(),
property, propertyDesc, null,
canApplyNotNull
canApplyNotNull,
dialect
);
}
return hasNotNull;
@ -280,25 +285,25 @@ private static boolean applyConstraints(Set<ConstraintDescriptor<?>> 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<Min> minConstraint = (ConstraintDescriptor<Min>) 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<Max> maxConstraint = (ConstraintDescriptor<Max>) 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 );
}
}

View File

@ -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() {

View File

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