10

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?

Marvin
  • 226
  • 1
  • 9

1 Answers1

9

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.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • Thx Both links are very helpful. I cant upvote your answer though :( But this is the "Solution" – Marvin Jun 02 '13 at 13:59
  • Thx man your helpful. ;) i'd like to participate more in the communnity ;) – Marvin Jun 03 '13 at 06:59
  • @Marvin How did you fix your problem? Didnt work for me even annotating the methods, which is what I have done originally. BTW, mine is a `OneToMany` relationship if it does matter. – Quincy Jul 10 '13 at 05:40
  • @Quincy i Solved the Problem by unproxying my Proxy before validation. I dont have the Code infront of me now. Im laying in the Hospital. You can check if your "entity" is a Proxy ans then unproxy ( Force initialize) it. Hole that helps. If Not i get out of the hospital in a few days. I can Post my Code then. – Marvin Jul 11 '13 at 09:23
  • @Marvin Thanks for the reply. So it means the suggestions in the issues doesnt work. I have workaround it by creating a wrapping getter and add validation contraints to it. It initialise the collection (by calling `size()` and then call the original getter. BTW, get well soon! – Quincy Jul 12 '13 at 07:32
  • @Quincy. Thanks. I am mostly painfree which is good. You are correct that the suggestions in the issues do not work properly but they give you a Nice hint on how. Your Solution is valid but Not so elegant. You should check if your Relation is connected via a Proxy ans only then initialize it. (but thats only ne since its cleanr). Also you have the Option to create validation groups and only validate relevant constraints to save Performance. (Initializing a Proxy can be managed by using Hibernate.initialize(); no need to call size() – Marvin Jul 12 '13 at 12:23