Bean Validation Fails on Hibernate Proxy? Expected Behavior?

随声附和 提交于 2020-01-03 13:35:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!