Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bean Validation Fails on Hibernate Proxy? Expected Behavior?

Im using

  • hibernate-core-4.0.1.Final
  • hibernate-validator-4.2.0.Final

I have a lazy loadable Entity

@NotNull
@OneToOne(fetch = FetchType.LAZY,optional = false,cascade = CascadeType.PERSIST)
@JoinColumn(name="library_id")
private Library library;

public Library getLibray() {
    return library;
}

and a defaultValidator

 private final ValidatorFactory factory = Validation
           .buildDefaultValidatorFactory();
 private final Validator val = factory.getValidator();

When I am trying to validate unattached and attached Entitys annotated with @NotNull, @Size and so forth. Everything works fine. But when i load an Entity via Lazy Load and try to validate it. The Validator fails every time. This seems due to the fact that im getting a Hibernate Proxy Object.

I can get arround this "issue" easily by just unproxying it.(But this is not so favorable in my situation)

Is this the expected behavior? Do I get the same behavior in OpenJPA, EclipseLink....?

Have a nice Sunday guys ;) I hope i did make the Question clear?

like image 724
Marvin Avatar asked Jun 02 '13 09:06

Marvin


People also ask

What is Bean Validation in JPA?

Bean validation is an optional feature in JPA 2, supported with its persistence provider. The persistence provider enables Bean validation on all entities to which annotations are applied. If Hibernate 3.5 is used as the persistence provider, then JPA 2 will use Hibernate Validator.

What are the annotations available in Hibernate Validator?

Hibernate validator specific annotations In addition to the constraints defined by the Bean Validation API, Hibernate Validator provides several useful custom constraints which are listed below. Checks that the annotated character sequence passes the Luhn checksum test.

What are the constraints for Bean Validation in Spring Boot?

Therefore, when Spring Boot validates the class instance, the constrained fields must be not null and their trimmed length must be greater than zero. Additionally, Bean Validation provides many other handy constraints besides @NotBlank.

What are the required Hibernate Validator Maven dependencies?

Given below are the required hibernate validator maven dependencies. Bean validation allows expressions inside the error messages. To parse these expressions, we must add a dependency on both the expression language API and an implementation of that API.


1 Answers

Issue can be solved by annotating methods instead of fields.

This is further discussed in HVAL-13 issue and also in HV-535. If replacement of annotations is not feasible, solution suggested in bug report is using HibernateProxyValidator instead.

like image 192
Mikko Maunu Avatar answered Sep 28 '22 03:09

Mikko Maunu