HHH-4968 Do not activate Bean Validation listeners when only DDL is requested (Vladimir Klyushnikov).

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19267 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-04-21 13:37:39 +00:00
parent ccadc9a406
commit 52495816de
2 changed files with 61 additions and 1 deletions

View File

@ -3,8 +3,10 @@ package org.hibernate.test.annotations.beanvalidation;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.mapping.Column;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.test.annotations.TestCase; import org.hibernate.test.annotations.TestCase;
@ -28,6 +30,12 @@ public class BeanValidationDisabledTest extends TestCase {
s.close(); s.close();
} }
public void testDDLDisabled() {
PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() );
Column countryColumn = (Column) classMapping.getProperty( "country" ).getColumnIterator().next();
assertTrue("DDL constraints are applied", countryColumn.isNullable() );
}
@Override @Override
protected void configure(Configuration cfg) { protected void configure(Configuration cfg) {
super.configure( cfg ); super.configure( cfg );
@ -36,6 +44,7 @@ public class BeanValidationDisabledTest extends TestCase {
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { return new Class<?>[] {
Address.class,
CupHolder.class CupHolder.class
}; };
} }

View File

@ -0,0 +1,51 @@
package org.hibernate.test.annotations.beanvalidation;
import java.math.BigDecimal;
import javax.validation.ConstraintViolationException;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.mapping.Column;
import org.hibernate.cfg.Configuration;
import org.hibernate.test.annotations.TestCase;
/**
* @author Vladimir Klyushnikov
*/
public class DDLWithoutCallbackTest extends TestCase {
public void testListeners() {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) );
Session s = openSession( );
Transaction tx = s.beginTransaction();
try {
s.persist( ch );
s.flush();
}
catch ( ConstraintViolationException e ) {
fail("invalid object should not be validated");
}
tx.rollback();
s.close();
}
public void testDDLEnabled() {
PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() );
Column countryColumn = (Column) classMapping.getProperty( "country" ).getColumnIterator().next();
assertFalse("DDL constraints are not applied", countryColumn.isNullable() );
}
@Override
protected void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( "javax.persistence.validation.mode", "ddl" );
}
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Address.class,
CupHolder.class
};
}
}