Made sure hibernate.validator.apply_to_ddl property is honored
Also changed BeanValidationGroupsTest to work against other databases than HSQLDB

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17818 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2009-10-22 11:10:31 +00:00
parent 85792a42d2
commit 11a87b6965
2 changed files with 191 additions and 135 deletions

View File

@ -150,7 +150,9 @@ protected List<XClass> orderAndFillHierarchy(List<XClass> original) {
//for each class, copy all the relevant hierarchy
for ( XClass clazz : original ) {
XClass superClass = clazz.getSuperclass();
while ( superClass != null && !reflectionManager.equals( superClass, Object.class ) && !copy.contains( superClass ) ) {
while ( superClass != null && !reflectionManager.equals( superClass, Object.class ) && !copy.contains(
superClass
) ) {
if ( superClass.isAnnotationPresent( Entity.class )
|| superClass.isAnnotationPresent( MappedSuperclass.class ) ) {
copy.add( superClass );
@ -168,7 +170,9 @@ protected List<XClass> orderAndFillHierarchy(List<XClass> original) {
}
private void orderHierarchy(List<XClass> copy, List<XClass> newList, List<XClass> original, XClass clazz) {
if ( clazz == null || reflectionManager.equals( clazz, Object.class ) ) return;
if ( clazz == null || reflectionManager.equals( clazz, Object.class ) ) {
return;
}
//process superclass first
orderHierarchy( copy, newList, original, clazz.getSuperclass() );
if ( original.contains( clazz ) ) {
@ -183,6 +187,7 @@ private void orderHierarchy(List<XClass> copy, List<XClass> newList, List<XClass
* Read a mapping from the class annotation metadata (JSR 175).
*
* @param persistentClass the mapped class
*
* @return the configuration object
*/
public AnnotationConfiguration addAnnotatedClass(Class persistentClass) throws MappingException {
@ -201,6 +206,7 @@ public AnnotationConfiguration addAnnotatedClass(Class persistentClass) throws M
* Read package level metadata
*
* @param packageName java package name
*
* @return the configuration object
*/
public AnnotationConfiguration addPackage(String packageName) throws MappingException {
@ -285,8 +291,12 @@ else if ( clazz.isAnnotationPresent( MappedSuperclass.class ) ) {
}
//process entities
if ( precedence == null ) precedence = getProperties().getProperty( ARTEFACT );
if ( precedence == null ) precedence = DEFAULT_PRECEDENCE;
if ( precedence == null ) {
precedence = getProperties().getProperty( ARTEFACT );
}
if ( precedence == null ) {
precedence = DEFAULT_PRECEDENCE;
}
StringTokenizer precedences = new StringTokenizer( precedence, ",; ", false );
if ( !precedences.hasMoreElements() ) {
throw new MappingException( ARTEFACT + " cannot be empty: " + precedence );
@ -363,17 +373,34 @@ else if ( clazz.isAnnotationPresent( MappedSuperclass.class ) ) {
buildUniqueKeyFromColumnNames( columnNames, table, keyName );
}
}
applyConstraintsToDDL();
}
private void applyConstraintsToDDL() {
boolean applyOnDdl = getProperties().getProperty(
"hibernate.validator.apply_to_ddl", //org.hibernate.validator.Environment.APPLY_TO_DDL
"true" )
"hibernate.validator.apply_to_ddl",
"true"
)
.equalsIgnoreCase( "true" );
if ( !applyOnDdl ) {
return; // nothing to do in this case
}
applyHibernateValidatorLegacyConstraintsOnDDL();
applyBeanValidationConstraintsOnDDL();
}
private void applyHibernateValidatorLegacyConstraintsOnDDL() {
//TODO search for the method only once and cache it?
Constructor validatorCtr = null;
Method applyMethod = null;
try {
Class classValidator = ReflectHelper.classForName( "org.hibernate.validator.ClassValidator", this.getClass() );
Class messageInterpolator = ReflectHelper.classForName( "org.hibernate.validator.MessageInterpolator", this.getClass() );
Class classValidator = ReflectHelper.classForName(
"org.hibernate.validator.ClassValidator", this.getClass()
);
Class messageInterpolator = ReflectHelper.classForName(
"org.hibernate.validator.MessageInterpolator", this.getClass()
);
validatorCtr = classValidator.getDeclaredConstructor(
Class.class, ResourceBundle.class, messageInterpolator, Map.class, ReflectionManager.class
);
@ -388,7 +415,7 @@ else if ( clazz.isAnnotationPresent( MappedSuperclass.class ) ) {
catch ( NoSuchMethodException e ) {
throw new AnnotationException( e );
}
if ( applyMethod != null && applyOnDdl ) {
if ( applyMethod != null ) {
for ( PersistentClass persistentClazz : ( Collection<PersistentClass> ) classes.values() ) {
//integrate the validate framework
String className = persistentClazz.getClassName();
@ -405,11 +432,10 @@ else if ( clazz.isAnnotationPresent( MappedSuperclass.class ) ) {
}
}
}
applyDDLOnBeanValidation( (Collection<PersistentClass>) classes.values(), getProperties() );
}
private void applyDDLOnBeanValidation(Collection<PersistentClass> persistentClasses, Properties properties) {
BeanValidationActivator.applyDDL( persistentClasses, properties );
private void applyBeanValidationConstraintsOnDDL() {
BeanValidationActivator.applyDDL( ( Collection<PersistentClass> ) classes.values(), getProperties() );
}
/**
@ -477,7 +503,9 @@ private void processEndOfQueue(List endOfQueueFkSecondPasses) {
}
catch ( RecoverableException e ) {
failingSecondPasses.add( pass );
if (originalException == null) originalException = (RuntimeException) e.getCause();
if ( originalException == null ) {
originalException = ( RuntimeException ) e.getCause();
}
}
}
stopProcess = failingSecondPasses.size() == 0 || failingSecondPasses.size() == endOfQueueFkSecondPasses.size();
@ -534,7 +562,8 @@ private void buildRecursiveOrderedFkSecondPasses(
String dependentTable = sp.getValue().getTable().getQuotedName();
if ( dependentTable.compareTo( startTable ) == 0 ) {
StringBuilder sb = new StringBuilder(
"Foreign key circularity dependency involving the following tables: ");
"Foreign key circularity dependency involving the following tables: "
);
throw new AnnotationException( sb.toString() );
}
buildRecursiveOrderedFkSecondPasses( orderedFkSecondPasses, isADependencyOf, startTable, dependentTable );
@ -691,7 +720,9 @@ protected void add(org.dom4j.Document doc) throws MappingException {
boolean ejb3Xml = "entity-mappings".equals( doc.getRootElement().getName() );
if ( inSecondPass ) {
//if in second pass bypass the queueing, getExtendedQueue reuse this method
if ( !ejb3Xml ) super.add( doc );
if ( !ejb3Xml ) {
super.add( doc );
}
}
else {
if ( !ejb3Xml ) {
@ -739,16 +770,22 @@ private static void findClassNames(
while ( classIterator.hasNext() ) {
Element element = ( Element ) classIterator.next();
String entityName = element.attributeValue( "entity-name" );
if ( entityName == null ) entityName = getClassName( element.attribute( "name" ), defaultPackage );
if ( entityName == null ) {
entityName = getClassName( element.attribute( "name" ), defaultPackage );
}
names.add( entityName );
findClassNames( defaultPackage, element, names );
}
}
private static String getClassName(Attribute name, String defaultPackage) {
if ( name == null ) return null;
if ( name == null ) {
return null;
}
String unqualifiedName = name.getValue();
if ( unqualifiedName == null ) return null;
if ( unqualifiedName == null ) {
return null;
}
if ( unqualifiedName.indexOf( '.' ) < 0 && defaultPackage != null ) {
return defaultPackage + '.' + unqualifiedName;
}
@ -823,12 +860,17 @@ public SessionFactory buildSessionFactory() throws HibernateException {
private void enableLegacyHibernateValidator() {
//add validator events if the jar is available
boolean enableValidatorListeners = !"false".equalsIgnoreCase( getProperty( "hibernate.validator.autoregister_listeners" ) );
boolean enableValidatorListeners = !"false".equalsIgnoreCase(
getProperty(
"hibernate.validator.autoregister_listeners"
)
);
Class validateEventListenerClass = null;
try {
validateEventListenerClass = ReflectHelper.classForName(
"org.hibernate.validator.event.ValidateEventListener",
AnnotationConfiguration.class );
AnnotationConfiguration.class
);
}
catch ( ClassNotFoundException e ) {
//validator is not present
@ -905,12 +947,16 @@ private void enableHibernateSearch() {
Class searchStartupClass;
try {
searchStartupClass = ReflectHelper.classForName( SEARCH_STARTUP_CLASS, AnnotationConfiguration.class );
} catch ( ClassNotFoundException e ) {
}
catch ( ClassNotFoundException e ) {
// TODO remove this together with SearchConfiguration after 3.1.0 release of Search
// try loading deprecated HibernateSearchEventListenerRegister
try {
searchStartupClass = ReflectHelper.classForName("org.hibernate.cfg.search.HibernateSearchEventListenerRegister", AnnotationConfiguration.class);
} catch ( ClassNotFoundException cnfe ) {
searchStartupClass = ReflectHelper.classForName(
"org.hibernate.cfg.search.HibernateSearchEventListenerRegister", AnnotationConfiguration.class
);
}
catch ( ClassNotFoundException cnfe ) {
log.debug( "Search not present in classpath, ignoring event listener registration." );
return;
}
@ -919,16 +965,22 @@ private void enableHibernateSearch() {
// call the method for registering the listeners
try {
Object searchStartupInstance = searchStartupClass.newInstance();
Method enableSearchMethod = searchStartupClass.getDeclaredMethod(SEARCH_STARTUP_METHOD,
EventListeners.class, Properties.class);
Method enableSearchMethod = searchStartupClass.getDeclaredMethod(
SEARCH_STARTUP_METHOD,
EventListeners.class, Properties.class
);
enableSearchMethod.invoke( searchStartupInstance, getEventListeners(), getProperties() );
} catch ( InstantiationException e ) {
}
catch ( InstantiationException e ) {
log.debug( "Unable to instantiate {}, ignoring event listener registration.", SEARCH_STARTUP_CLASS );
} catch ( IllegalAccessException e ) {
}
catch ( IllegalAccessException e ) {
log.debug( "Unable to instantiate {}, ignoring event listener registration.", SEARCH_STARTUP_CLASS );
} catch ( NoSuchMethodException e ) {
}
catch ( NoSuchMethodException e ) {
log.debug( "Method enableHibernateSearch() not found in {}.", SEARCH_STARTUP_CLASS );
} catch ( InvocationTargetException e ) {
}
catch ( InvocationTargetException e ) {
log.debug( "Unable to execute {}, ignoring event listener registration.", SEARCH_STARTUP_METHOD );
}
}
@ -1078,13 +1130,15 @@ protected AnnotationConfiguration doConfigure(Document doc) throws HibernateExce
}
@Override
public AnnotationConfiguration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy) throws MappingException {
public AnnotationConfiguration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy)
throws MappingException {
super.setCacheConcurrencyStrategy( clazz, concurrencyStrategy );
return this;
}
@Override
public AnnotationConfiguration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) throws MappingException {
public AnnotationConfiguration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy)
throws MappingException {
super.setCollectionCacheConcurrencyStrategy( collectionRole, concurrencyStrategy );
return this;
}
@ -1276,9 +1330,10 @@ public void addQuery(String name, NamedQueryDefinition query) throws DuplicateMa
@Override
public void addResultSetMapping(ResultSetMappingDefinition definition) throws DuplicateMappingException {
if ( !defaultSqlResulSetMappingNames.contains( definition.getName() ) )
if ( !defaultSqlResulSetMappingNames.contains( definition.getName() ) ) {
super.addResultSetMapping( definition );
}
}
@Override
public void addSQLQuery(String name, NamedSQLQueryDefinition query) throws DuplicateMappingException {

View File

@ -71,6 +71,7 @@ protected void configure(Configuration cfg) {
"javax.persistence.validation.group.pre-remove",
Default.class.getName() + ", " + Strict.class.getName()
);
cfg.setProperty( "hibernate.validator.apply_to_ddl", "false" );
}
protected Class<?>[] getMappings() {