[hibernate-issues] [Hibernate-JIRA] Commented: (HV-433) Cascaded validation on superclass fields does not work if only @Valid is used

Christian Quanz (JIRA) noreply at atlassian.com
Sun Feb 20 14:44:05 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HV-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=40030#action_40030 ] 

Christian Quanz commented on HV-433:
------------------------------------

The following test case fails for me:

{noformat}public class Hv433 {

	private static class Bar {

		private String string;
	}

	private static class Foo {

		protected Bar bar;

	}

	private static class Baz extends Foo {

	}

	@Test
	public void hv433() {

		Baz baz = new Baz();
		baz.bar = new Bar();

		HibernateValidatorConfiguration config = Validation
				.byProvider(HibernateValidator.class).configure();
		
		ConstraintMapping newMapping = new ConstraintMapping();
		newMapping
		.type(Bar.class)
			.property("string", ElementType.FIELD)
				.constraint(NotNullDef.class)
		.type(Foo.class)
				.valid("bar", ElementType.FIELD);
		config.addMapping(newMapping);
		
		ValidatorFactory factory = config.buildValidatorFactory();
		Set<ConstraintViolation<Baz>> violations = factory.getValidator()
				.validate(baz);

		assertEquals(violations.size(), 1);
		assertEquals(violations.iterator().next().getMessage(),
				"may not be null");
		assertEquals(violations.iterator().next().getPropertyPath().toString(),
				"bar.string");
	}
}{noformat}

The code I pointed to in BeanMetaDataImpl only checks for @Valid annotations being present.

> Cascaded validation on superclass fields does not work if only @Valid is used
> -----------------------------------------------------------------------------
>
>                 Key: HV-433
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-433
>             Project: Hibernate Validator
>          Issue Type: Bug
>          Components: engine
>    Affects Versions: 4.1.0.Final
>         Environment: Java 1.5.0 (IBM J9 2.3); Hibernate Validator 4.1.0.FINAL
>            Reporter: Christian Quanz
>
> Cascaded fields are not validated if the following applies:
> - Only @valid is specified for a particular field, no further constraints
> - Validation takes place on an object of a sublcass of the class where the field to be validated is defined
> Simply no validation will take place on the object assigned to the field. Eyerything works fine if I add another constraint definition, e.g., @NotNull to the field, as in the examples in the documentation. If I validate an object of the class where the field is defined, it also works. I doubt that it matters, but the field's visibility is "protected".
> I tested it for XML constraint definitions and programmatic constraint definitions.
> I could trace the error back to BeanMetaDataImpl->initFieldConstraints(...), where the flag cachedFieldIsCascaded will be false. Thus, addCascadedMember(...) will not be called for the field.
> In case of any additional constraint being defined, the flag cachedFieldIsCascaded will be true.
> Hope that helps.

-- 
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