-->
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: Constraint for filling in at least one field
PostPosted: Wed Sep 22, 2010 8:35 pm 
Newbie

Joined: Thu Feb 26, 2004 3:27 pm
Posts: 1
I am trying to set up a constraint but cannot figure out how to do it.

My class looks something like this:

public class Entity {
@Column
private String firstName;

@Column
private String lastName;

@Column
private String companyName;

//getters and setters ...
}

I wanted to set up a constraint that would allow me to check that at least one of the fields in this class is not null, that way we don't persist a record with all the fields null.

I can't use @NotNull because there is no conditional that checks if the other field was filled in or not.

What is the best way to implement this? I tried looking in the forum but didn't have any luck.

Thanks in advance,

Omar


Top
 Profile  
 
 Post subject: Re: Constraint for filling in at least one field
PostPosted: Thu Sep 23, 2010 4:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
A custom class level constraint would be easiest. Check the online documentation for help. An alternative would be to use the Hibernate Validator ScriptAssert constraint. Again, have a look at the online manual.

--Hardy


Top
 Profile  
 
 Post subject: Re: Constraint for filling in at least one field
PostPosted: Thu Sep 23, 2010 7:10 am 
Newbie

Joined: Thu Sep 23, 2010 4:43 am
Posts: 10
Hi,

You can use scriptAssert annotation but this will be available with Java6, if you are using 5, or earlier you should put JSR 223 RI to classpath.

@ScriptAssert(lang = "javascript", message= "first name,last name, and company name cannot be null at the same time.", script = "_this.firstName == null || _this.lastName == null || _this.companyName == null"

no hibernate annotation at class level.


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.