问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/16881516/bean-validation-fails-on-hibernate-proxy-expected-behavior