HHH-9676 - TypeSafeActivator.applyDDL doesn't process composing constraints when @NotNull is present

This commit is contained in:
Timo Verhoeven 2015-03-23 16:06:08 +01:00 committed by Vlad Mihalcea
parent 40bb7d518c
commit 86e9efe788
5 changed files with 79 additions and 8 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@
/build /build
*/build */build
testdb testdb
lib/
# IntelliJ specific files/directories # IntelliJ specific files/directories
out out

View File

@ -264,12 +264,14 @@ class TypeSafeActivator {
applyLength( property, descriptor, propertyDesc ); applyLength( property, descriptor, propertyDesc );
// pass an empty set as composing constraints inherit the main constraint and thus are matching already // pass an empty set as composing constraints inherit the main constraint and thus are matching already
hasNotNull = hasNotNull || applyConstraints( boolean hasNotNullFromComposingConstraints = applyConstraints(
descriptor.getComposingConstraints(), descriptor.getComposingConstraints(),
property, propertyDesc, null, property, propertyDesc, null,
canApplyNotNull, canApplyNotNull,
dialect dialect
); );
hasNotNull = hasNotNull || hasNotNullFromComposingConstraints;
} }
return hasNotNull; return hasNotNull;
} }

View File

@ -11,8 +11,10 @@ import javax.persistence.EntityManager;
import javax.persistence.RollbackException; import javax.persistence.RollbackException;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -23,16 +25,18 @@ import static org.junit.Assert.fail;
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase { public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
@Test @Test
public void testBeanValidationIntegrationOnFlush() { public void testBeanValidationIntegrationOnFlush() {
CupHolder ch = new CupHolder(); CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) ); ch.setRadius( new BigDecimal( "12" ) );
ch.setTitle( "foo" );
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
try { try {
em.persist( ch ); em.persist( ch );
em.flush(); em.flush();
fail("invalid object should not be persisted"); fail( "invalid object should not be persisted" );
} }
catch ( ConstraintViolationException e ) { catch ( ConstraintViolationException e ) {
assertEquals( 1, e.getConstraintViolations().size() ); assertEquals( 1, e.getConstraintViolations().size() );
@ -49,6 +53,7 @@ public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
public void testBeanValidationIntegrationOnCommit() { public void testBeanValidationIntegrationOnCommit() {
CupHolder ch = new CupHolder(); CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "9" ) ); ch.setRadius( new BigDecimal( "9" ) );
ch.setTitle( "foo" );
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
em.persist( ch ); em.persist( ch );
@ -56,7 +61,7 @@ public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
try { try {
ch.setRadius( new BigDecimal( "12" ) ); ch.setRadius( new BigDecimal( "12" ) );
em.getTransaction().commit(); em.getTransaction().commit();
fail("invalid object should not be persisted"); fail( "invalid object should not be persisted" );
} }
catch ( RollbackException e ) { catch ( RollbackException e ) {
final Throwable cve = e.getCause(); final Throwable cve = e.getCause();
@ -66,10 +71,21 @@ public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
em.close(); em.close();
} }
@Test
@RequiresDialect(H2Dialect.class)
public void testTitleColumnHasExpectedLength() {
EntityManager em = getOrCreateEntityManager();
int len = (Integer) em.createNativeQuery(
"select CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS c where c.TABLE_NAME = 'CUPHOLDER' and c.COLUMN_NAME = 'TITLE'"
).getSingleResult();
assertEquals( 64, len );
}
@Override @Override
public Class[] getAnnotatedClasses() { public Class[] getAnnotatedClasses() {
return new Class[] { return new Class[] {
CupHolder.class CupHolder.class
}; };
} }
}
}

View File

@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.jpa.test.beanvalidation; package org.hibernate.jpa.test.beanvalidation;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -17,12 +18,15 @@ import javax.validation.constraints.NotNull;
*/ */
@Entity @Entity
public class CupHolder { public class CupHolder {
@Id @Id
@GeneratedValue @GeneratedValue
//@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id; private Integer id;
private BigDecimal radius; private BigDecimal radius;
private String title;
public Integer getId() { public Integer getId() {
return id; return id;
} }
@ -31,7 +35,7 @@ public class CupHolder {
this.id = id; this.id = id;
} }
@Max( value = 10, message = "Radius way out") @Max(value = 10, message = "Radius way out")
@NotNull @NotNull
public BigDecimal getRadius() { public BigDecimal getRadius() {
return radius; return radius;
@ -40,4 +44,14 @@ public class CupHolder {
public void setRadius(BigDecimal radius) { public void setRadius(BigDecimal radius) {
this.radius = radius; this.radius = radius;
} }
}
@NotNull
@ValidTitle
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -0,0 +1,38 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.beanvalidation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.Size;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* @author Vlad Mihalcea
*/
@Size(min = 3, max = 64)
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface ValidTitle {
String message() default "foo";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}