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
testdb
lib/
# IntelliJ specific files/directories
out

View File

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

View File

@ -11,8 +11,10 @@ import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.validation.ConstraintViolationException;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -23,16 +25,18 @@ import static org.junit.Assert.fail;
* @author Emmanuel Bernard
*/
public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
@Test
public void testBeanValidationIntegrationOnFlush() {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) );
ch.setTitle( "foo" );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist( ch );
em.flush();
fail("invalid object should not be persisted");
fail( "invalid object should not be persisted" );
}
catch ( ConstraintViolationException e ) {
assertEquals( 1, e.getConstraintViolations().size() );
@ -49,6 +53,7 @@ public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
public void testBeanValidationIntegrationOnCommit() {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "9" ) );
ch.setTitle( "foo" );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist( ch );
@ -56,7 +61,7 @@ public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
try {
ch.setRadius( new BigDecimal( "12" ) );
em.getTransaction().commit();
fail("invalid object should not be persisted");
fail( "invalid object should not be persisted" );
}
catch ( RollbackException e ) {
final Throwable cve = e.getCause();
@ -66,10 +71,21 @@ public class BeanValidationTest extends BaseEntityManagerFunctionalTestCase {
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
public Class[] getAnnotatedClasses() {
return new 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>.
*/
package org.hibernate.jpa.test.beanvalidation;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -17,12 +18,15 @@ import javax.validation.constraints.NotNull;
*/
@Entity
public class CupHolder {
@Id
@GeneratedValue
//@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
private BigDecimal radius;
private String title;
public Integer getId() {
return id;
}
@ -31,7 +35,7 @@ public class CupHolder {
this.id = id;
}
@Max( value = 10, message = "Radius way out")
@Max(value = 10, message = "Radius way out")
@NotNull
public BigDecimal getRadius() {
return radius;
@ -40,4 +44,14 @@ public class CupHolder {
public void setRadius(BigDecimal 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 {};
}