-->
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: Field marked @NotNull @Transient validated upon persist()
PostPosted: Wed Feb 16, 2011 8:29 pm 
Newbie

Joined: Sun Jul 11, 2010 2:48 pm
Posts: 5
Fairly new to Hibernate Validator, but was zooming right along until I ran into a bit of a snag that I can't seem to find any info on. Neither the JSR-303 spec nor the Hibernate Validator docs mention validation behavior regarding @Transient fields. Can't find anything in the community Wiki or Jira either. Consider this:

Code:
@Entity
class SomeObject {
   ...
   @Transient
   @NotNull
   private String creditCardNumber;
   ...
}


The use case here is that the validator is really handy when validating a form submission that binds to an object that has a creditCardNumber property. However, the creditCardNumber should never be persisted due to client security concerns with credit card storage. Unfortunately, it appears that when Hibernate persists this object, the validator is enforcing the @NotNull constraint when it seems it logically should ignore it. Does that make sense? So to rephrase, I want to use the validator to validate this bean and it's creditCardNumber only on a form submission but never when persist() is called because, well, it doesn't matter if the creditCardNumber is valid at persist() because it's not being persisted. Anyone else run into this? Thanks in advance.

-Matt


Top
 Profile  
 
 Post subject: Re: Field marked @NotNull @Transient validated upon persist()
PostPosted: Thu Feb 17, 2011 7:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
It seems you just want to disable the event based validation. Setting the property javax.persistence.validation.mode to none (in persistence.xml) should do the trick. Afaik this should be in the online docs.
Hope this helps.

--Hardy


Top
 Profile  
 
 Post subject: Re: Field marked @NotNull @Transient validated upon persist()
PostPosted: Thu Feb 17, 2011 10:42 am 
Newbie

Joined: Sun Jul 11, 2010 2:48 pm
Posts: 5
Well, no, it was my understanding that javax.persistence.validation.mode would turn off ALL validation during persistence operations. I still want to validate the bean as it has a number of other non-@Transient fields that need to be validated before they are persisted. Although, now that you mention it, I wonder if it's overkill for me to be re-validating at persist since I'm validating earlier in the flow anyway (i.e. at the form submission.) Decisions, decisions... Thanks for the reply!


Top
 Profile  
 
 Post subject: Re: Field marked @NotNull @Transient validated upon persist()
PostPosted: Thu Feb 17, 2011 10:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Seems to me that re-validating is not needed. There is also no option to no validate transient fields. So if you validate the value needs to be not null.


Top
 Profile  
 
 Post subject: Re: Field marked @NotNull @Transient validated upon persist()
PostPosted: Thu Feb 17, 2011 11:01 am 
Newbie

Joined: Sun Jul 11, 2010 2:48 pm
Posts: 5
Although, the docs do say that when using JPA2, it requires validation to be turned on at persist. At any rate, this was more about the strange native nature of validating @Transient fields than needing help to solve a particular problem. I mean, why validate @Transient fields at persist? =)


Top
 Profile  
 
 Post subject: Re: Field marked @NotNull @Transient validated upon persist()
PostPosted: Thu Feb 17, 2011 11:13 am 
Newbie

Joined: Sun Jul 11, 2010 2:48 pm
Posts: 5
Well, but then the JPA2 spec says javax.persistence.validation.mode can certainly be set to none and should be honored. Oh well...


Top
 Profile  
 
 Post subject: Re: Field marked @NotNull @Transient validated upon persist()
PostPosted: Thu Feb 17, 2011 4:17 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
You might consider putting the constraints at you transient fields into a separate validation group and validating another group when persisting your entities. The groups to validate upon insert/update can be specified in persistence.xml with the properties

javax.persistence.validation.group.pre-persist
javax.persistence.validation.group.pre-update

Hth, Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


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.