[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5786) TypeSafeActivator.applyDDL doesn't process comprising constraints when @NotNull goes first

Enrico Pizzorno (JIRA) noreply at atlassian.com
Mon Dec 13 06:11:13 EST 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5786?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39348#action_39348 ] 

Enrico Pizzorno commented on HHH-5786:
--------------------------------------

I really am sorry I can't prepare a unit test right now, I'm a bit busy and I think it's not so easy to reproduce the problem.

If you look at TypeSafeActivator.java from hibernate-core (package org.hibernate.cfg.beanvalidation), line 200 you'll see the call to "applyConstraints" won't be done if hasNotNull is already true. 

Correct me if I'm wrong, method applyConstraints (same source, line 172) receives a set of constraints, process them so that @Size, @Digits, @Min, @Max, @NotNull are recognized and sets those values on the column definition. At line 200 there's a recursive call to applyConstraints on "composing constraints" of the constraint we're currently processing. This way the composing constraints will be applied. *But* if we already processed a @NotNull constraint then hasNotNull is already true so when we do hasNotNull = hasNotNull || applyConstraints(....) (at line 200) the second condition won't be evaluated. Then the composing constraints won't be applied.

In my example I have a constraint @Name with a composing constraint @Size(max = 60). If I have an entity field annotated with @NotNull and @Name the @Size(max = 60) is not always applied. Sometimes yes, sometimes no.

Let me know if it's clearer now.

> TypeSafeActivator.applyDDL doesn't process comprising constraints when @NotNull goes first
> ------------------------------------------------------------------------------------------
>
>                 Key: HHH-5786
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5786
>             Project: Hibernate Core
>          Issue Type: Bug
>    Affects Versions: 3.6.0
>            Reporter: Enrico Pizzorno
>            Priority: Minor
>
> We have a custom constraint defined as:
> {quote}
> @Size(max=60)
> ...
> public @interface Name {
>     String message() default "";
>     Class<?>[] groups() default {};
>   
>     Class<? extends Payload>[] payload() default {};	
> }
> {quote}
> Then we can annotate some entities like this:
> {quote}
> public class MyEntity {
>     ...
>     
>     @NotNull
>     @Name
>     private String name;
>     ...
> }
> {quote}
> If @NotNull goes before @Name then when contraints are applied to column definitions by calling TypeSafeActivator.applyDDL the constraint @Size(max=60) is ignored. 
> I think the problem is at line 200 from *org.hibernate.cfg.beanvalidation.TypeSafeActivator*, method *private static boolean applyConstraints(Set<ConstraintDescriptor<?>> constraintDescriptors, Property property, PropertyDescriptor propertyDesc, Set<Class<?>> groups, boolean canApplyNotNull)*:
> {quote}
> // pass an empty set as composing constraints inherit the main constraint and thus are matching already
> hasNotNull = hasNotNull || applyConstraints(
> 		descriptor.getComposingConstraints(),
> 		property, propertyDesc, null,
> 		canApplyNotNull );
> {quote}
> If @NotNull is processed first then hasNotNull is set to true so the call to applyConstraints at line 200 is never made. 
> Changing the order of the "or" operands will solve the problem:
> {quote}
> // pass an empty set as composing constraints inherit the main constraint and thus are matching already
> hasNotNull = applyConstraints(
> 		descriptor.getComposingConstraints(),
> 		property, propertyDesc, null,
> 		canApplyNotNull ) || hasNotNull;
> {quote}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list