Bean Validation Fails on Hibernate Proxy? Expected Behavior?
Asked Answered
P

1

11

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?

Pelargonium answered 2/6, 2013 at 9:34 Comment(0)
S
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.

Shoat answered 2/6, 2013 at 9:45 Comment(6)
Thx Both links are very helpful. I cant upvote your answer though :( But this is the "Solution"Pelargonium
Thx man your helpful. ;) i'd like to participate more in the communnity ;)Pelargonium
@Pelargonium 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.Hexahedron
@Hexahedron 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.Pelargonium
@Pelargonium 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!Hexahedron
@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()Pelargonium

© 2022 - 2024 — McMap. All rights reserved.