diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java index 220e3c26b..e35533d64 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java @@ -22,7 +22,6 @@ import javax.persistence.Transient; import javax.persistence.Version; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; - import com.avaje.ebean.annotation.CreatedTimestamp; import com.avaje.ebean.annotation.EmbeddedColumns; import com.avaje.ebean.annotation.Encrypted; @@ -193,9 +192,10 @@ public class AnnotationFields extends AnnotationParser { if (validationAnnotations) { NotNull notNull = get(prop, NotNull.class); - if (notNull != null) { - // explicitly specify a version column - prop.setNullable(false); + if (notNull != null && isNotNullOnAllValidationGroups(notNull.groups())) { + // Not null on all validation groups so enable + // DDL generation of Not Null Constraint + prop.setNullable(false); } Size size = get(prop, Size.class); @@ -255,6 +255,20 @@ public class AnnotationFields extends AnnotationParser { } + /** + * Return true if the validation is on all validation groups and hence + * can be applied to DDL generation. + */ + private boolean isNotNullOnAllValidationGroups(Class[] groups) { + if (groups.length == 0) { + return true; + } + if (groups.length == 1 && javax.validation.groups.Default.class.isAssignableFrom(groups[0])) { + return true; + } + return false; + } + private void setEncryption(DeployBeanProperty prop, boolean dbEncString, int dbLen) { util.checkEncryptKeyManagerDefined(prop.getFullBeanName()); diff --git a/src/test/java/com/avaje/tests/model/basic/Customer.java b/src/test/java/com/avaje/tests/model/basic/Customer.java index 1acd0e2a6..831f062ce 100644 --- a/src/test/java/com/avaje/tests/model/basic/Customer.java +++ b/src/test/java/com/avaje/tests/model/basic/Customer.java @@ -53,6 +53,7 @@ public class Customer extends BasicDomain { @Size(max=100) String smallnote; + @NotNull(groups={ValidationGroupSomething.class}) Date anniversary; @ManyToOne(cascade=CascadeType.ALL) diff --git a/src/test/java/com/avaje/tests/model/basic/ValidationGroupSomething.java b/src/test/java/com/avaje/tests/model/basic/ValidationGroupSomething.java new file mode 100644 index 000000000..6219556cc --- /dev/null +++ b/src/test/java/com/avaje/tests/model/basic/ValidationGroupSomething.java @@ -0,0 +1,9 @@ +package com.avaje.tests.model.basic; + +/** + * A javax validation group used to test conditional @NotNull constraints + * which results in no DDL generation of the constraint - Issue 34. + */ +public interface ValidationGroupSomething { + +}