-->
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.  [ 7 posts ] 
Author Message
 Post subject: adding validation rules to embedded object
PostPosted: Mon May 21, 2012 10:21 pm 
Newbie

Joined: Sun Aug 28, 2005 1:29 pm
Posts: 12
I have an entity class with an embedded object in it. The class of the embedded object has validation associated with it and is marked for validation:

@Embedded
@Valid
private Info info;

The embedded class, Info, is used in my places. In a couple of places, I would like to add an additional validation rule for the embedded object. How can I do this?

Thanks
Andrew


Top
 Profile  
 
 Post subject: Re: adding validation rules to embedded object
PostPosted: Tue May 22, 2012 5:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Andrew,

your description is a little vague. It would help to see the code for Info and what constraints you want or do not want to be validated.
I would expect, however, that you will have to look at groups. Depending on the entity Info is included you would validate a different group.

--Hardy


Top
 Profile  
 
 Post subject: Re: adding validation rules to embedded object
PostPosted: Tue May 22, 2012 8:52 am 
Newbie

Joined: Sun Aug 28, 2005 1:29 pm
Posts: 12
Hi, Hardy

Thanks for the reply. The Info class has a begin date and an end date in it, and the class has an @AssertTrue annotation which requires the begin date to be before the end date (if the two dates are not null).

In class A, I use it like this:

Code:
@Entity
public class A {
...
@Embedded
@Valid
private Info info;
...
}


In class B, I also use Info as an embedded object and I want to add a constraint: that the begin date is not null. My question is: can I add this constraint to class B?

I think you're suggesting that in the Info class itself (rather than in class B), I add a @NotNull constraint to the begin date field, but tag it with a non-null group name so I turn the not-null validation on and off?

Does that sound right?


Top
 Profile  
 
 Post subject: Re: adding validation rules to embedded object
PostPosted: Tue May 22, 2012 9:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
ageery wrote:

My question is: can I add this constraint to class B?

No

ageery wrote:
I think you're suggesting that in the Info class itself (rather than in class B), I add a @NotNull constraint to the begin date field, but tag it with a non-null group name so I turn the not-null validation on and off?
Does that sound right?

Something like this, yes.


Top
 Profile  
 
 Post subject: Re: adding validation rules to embedded object
PostPosted: Tue May 22, 2012 5:40 pm 
Newbie

Joined: Sun Aug 28, 2005 1:29 pm
Posts: 12
Would this work (from http://docs.jboss.org/hibernate/validat ... roup-class)?

Code:
public interface NotNullDate {
}

public class Info {
...
@NotNull(group = NotNullDate.class)
private Date begin;
...
private Date end;
...
}

@GroupSequence({Default.class, NotNullDate.class})
public class B {
...
@Embedded
@Valid
private Info info;
...
}


I would expect that redefining the default group on class B would have the effect of validating the @NotNull constraint in the Info class. On the other hand, a class, say A, without the @GroupSequence would not validate the @NotNull constraint in the Info class.

Does that sound right?

Thanks
Andrew


Top
 Profile  
 
 Post subject: Re: adding validation rules to embedded object
PostPosted: Wed May 23, 2012 6:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Good thinking, but this is not how Bean Validation works. As you can also read in the online documentation:

Quote:
The Default group sequence overriding is local to the class it is defined on and is not propagated to the associated objects. This means in particular that adding DriverChecks to the default group sequence of RentalCar would not have any effects. Only the group Default will be propagated to the driver association when validation a rental car instance.


If you are working with groups you will have to call the right groups explicitly.

I think what would help you is a solution to BVAL-208.

--Hardy


Top
 Profile  
 
 Post subject: Re: adding validation rules to embedded object
PostPosted: Tue May 29, 2012 2:39 pm 
Newbie

Joined: Sun Aug 28, 2005 1:29 pm
Posts: 12
I guess there are two basic solutions:

(1) Write a method on the parent/enclosing class to check the fields in the embedded class and annotated it with an @AssertTrue annotation; or
(2) Specify a group on the embedded class for the check and turn it on at validation time

The advantage to #1 is that the validation is wholly self-contained. The disadvantage is that the validation is really at a field level, but is being specified by a class level validator.

The advantage to #2 is that the validation is where it should be: on the field being checked. The disadvantage is that the caller has to know to enable the necessary group.

To me, #1 sounds like the best solution because it is encapsulated.

Andrew


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.