[1.2.x] Validation of Hibernate lazy loaded members

177 views
Skip to first unread message

jsh

unread,
Apr 27, 2012, 7:49:57 AM4/27/12
to play-fr...@googlegroups.com
Hi all,

We are using Play 1.2.3 with Hibernate models.
We currently have a problem when wanting to validate a whole objects hierarchy. Some members of the root object of the hierarchy are Lazy loaded (for performance reason). When using the "validation.valid(object.subObject)", the subObject is in fact an hibernate proxy and the validation always fails. I think this is because the validator does not accesses internally the fields but uses reflection so not provoking the load of the object from the database.
Concretely some code example:
RootObject rObject = new RootObject();
// Fill the rObject fields...
SubObject sObject = SubObject.find("whatever").first();
rObject.subObject = sObject;
...
if (rObject.subObject != null) {
  validation.valid(rObject.subObject); // Will fail if subObject member is declared with "fetch = FetchType.LAZY"
  ...
}

To be noted that if we instanciate a new SubObject (not retrieved from DB) this works perfectly.
For the moment I solved the problem by doing an horrible getter for the subObject field like that:
public SubObject getSubObject() {
    if (this.subObject != null && this.subObject instanceof HibernateProxy) {
      return (SubObject) ((HibernateProxy) this.subObject).getHibernateLazyInitializer().getImplementation();
    }
    return this.subObject;
}

Does anybody have an idea to solve this?

Thanks.
Jérôme

Nicolas Leroux

unread,
Apr 27, 2012, 7:51:56 AM4/27/12
to play-fr...@googlegroups.com
Does this problem also occur with the yourentity.validateAndSave method?


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/DKk1s0z3Y48J.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

jsh

unread,
Apr 27, 2012, 8:13:37 AM4/27/12
to play-fr...@googlegroups.com
Le vendredi 27 avril 2012 13:51:56 UTC+2, Nicolas Leroux - committer a écrit :
Does this problem also occur with the yourentity.validateAndSave method?

Hmm, using this method does not seem to trigger a validation error nevertheless does it do recursive validation of all submembers of the root object?

Thanks.
Jérôme.
Reply all
Reply to author
Forward
0 new messages