-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Bean Validation + JPA 2 with conditional validation support?
PostPosted: Mon Mar 21, 2011 10:11 pm 
Beginner
Beginner

Joined: Wed Feb 16, 2005 1:22 am
Posts: 25
Location: Jakarta
I have a dummy JPA Entity to demonstrate my need:

Code:
@Entity
    public class User {
      private boolean referenced;
      private String referencedBy;
      private Date referenceTime;
      ...
    }


As i understand it, i can annotate those fields with bean validation annotations. Also, with BV integration with JPA, i can make groups that belong to one of the three lifecycle validation groups (pre-persist, pre-update and pre-remove).

In my above example, my need is :

1. If the referenced == true
2. Then referencedBy and referenceTime must not be null on pre-persist and pre-update

Obviously this example below wont work, as the validation would be done for referencedBy and referenceTime on pre-persist and pre-update :

Code:
    @Entity
    public class User {
      private boolean referenced;
   
      @NotNull
      private String referencedBy;
   
      @NotNull
      private Date referenceTime;
      ...
    }


Any idea on how to solve this ? Or this kind of validation should belong not in the bean validation, but as a code somewhere like in @PrePersist method ?

Thank you !

_________________
Greater in battle
than the man who would conquer
a thousand-thousand men,
is he who would conquer
just one —
himself.


Top
 Profile  
 
 Post subject: Re: Bean Validation + JPA 2 with conditional validation support?
PostPosted: Tue Mar 22, 2011 12:20 pm 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Hi,

I've looked at the JPA2 specification and, by default, on pre-persist and pre-update, the validated group is the Default one. I think you could adress your problem in different ways. First of all, you can define a custom validator for the User class (this approach is portable across BV implementations). Inside of your custom validator you could check that, if the referenced property is true, then the properties referencedBy and referenceTime are not null (remember to remove the @NotNull annotation on your fields).

You also could use specific Hibernate Validator solutions:

  • As of version 4.1 you could use @ScriptAssert annotation to validate the same thing (you have to remove the @NotNull annotation on your fields). See http://docs.jboss.org/hibernate/stable/ ... ssert.html.
  • As of version 4.2.0.Beta2 you could use a dynamic default group sequence provider. With that provider you can dynamically compose the Default group sequence of your bean (sequence used when the Default group is validated). So you can add a group (in which your @NotNull constraints belongs) depending on the state of the referenced property. See http://docs.jboss.org/hibernate/validat ... roup-class.

Hope this help.

--
Kevin


Top
 Profile  
 
 Post subject: Re: Bean Validation + JPA 2 with conditional validation support?
PostPosted: Wed Mar 23, 2011 12:13 am 
Beginner
Beginner

Joined: Wed Feb 16, 2005 1:22 am
Posts: 25
Location: Jakarta
Thank you for your research and suggestions .. Appreciate it ..! :-)

_________________
Greater in battle
than the man who would conquer
a thousand-thousand men,
is he who would conquer
just one —
himself.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.