-->
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.  [ 4 posts ] 
Author Message
 Post subject: Validator annotation on setter
PostPosted: Wed Jul 30, 2008 11:33 am 
Newbie

Joined: Mon Dec 10, 2007 7:03 am
Posts: 14
I wanted to place Hibernate Validator annotations on setters because it's when I set something that I wan't it to be validated but it doesn't work.

Sample setter
Code:
@NotNull
public void setLocale(Locale locale) { ... }


An exception is raised:
Code:
java.lang.IllegalStateException: Could not get property value
   at org.hibernate.validator.ClassValidator.getMemberValue(ClassValidator.java:537)
   at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:383)
   at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:351)
   at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:139)
   at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)
   at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:160)
   at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:53)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   ...
Caused by: java.lang.IllegalArgumentException: Invoking setLocale with wrong parameters
   at org.hibernate.annotations.common.reflection.java.JavaXMethod.invoke(JavaXMethod.java:39)
   at org.hibernate.validator.ClassValidator.getMemberValue(ClassValidator.java:534)
   ... 45 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.annotations.common.reflection.java.JavaXMethod.invoke(JavaXMethod.java:33)
   ... 46 more


I would have expected associated getter or attribute to be used.

Using Hibernate Validator 3.0.0 with Hibernate Core 3.2.6.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 6:53 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
The annotations needs to be on the getter (or the field).

The validation does NOT occur when you get or set it,
instead it occurs when you explicititly give an instance of that class to a class validator.

However, hibernate automatically detects validation annotations and adds a hibernate lifecycle event to validate them with a class validator when it's written to the database.

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 7:06 am 
Newbie

Joined: Mon Dec 10, 2007 7:03 am
Posts: 14
I knew that ant I moved all annotation to the getters. But I would have expected:
1) A fallback solution: use associated getter or attribute like other Hibernate annotations
2) A clear error message


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 10:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If someone provides a patch for 2), we will apply it.

_________________
Emmanuel


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