-->
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.  [ 29 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 9:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Email will be ported to the 4.0 before it goes GA.


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 9:55 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
@Url should be added as well =)


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 10:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Well, the Jira issue already exists. Whether it makes it into 4.0 or 4.1 I am not sure yet - http://opensource.atlassian.com/project ... wse/HV-229


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 10:16 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Okay, I rewrote all my tests to use the new way to validate things, instead of using a ClassValidator. I changed all the references to old packages and everything else. I even updated my custom contraints to use the new api... much cleaner btw. Good job on that ;)

Now, the last thing to do is hook this baby into Hibernate. The event.* package is gone, so I'm assuming they are somewhere else? I checked the documentation, but there isn't a word about it.

I did a search for BeanValidationEventListener, and I can't find it at all inside of my IDE. I guess I don't have the jar?

Here is the spot in my spring config where I need to add the listeners:
Code:
        <property name="eventListeners">
            <map>
                <entry key="pre-insert">
                    <bean class=""/>
                </entry>
                <entry key="pre-update">
                    <bean class=""/>
                </entry>
            </map>
        </property>


I think Spring can use a constructor with arguments, so that won't be a problem. I think I just need to know where the listeners are?

Thanks!


Last edited by mystic on Wed Sep 23, 2009 10:21 am, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 10:17 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
hardy.ferentschik wrote:
Well, the Jira issue already exists. Whether it makes it into 4.0 or 4.1 I am not sure yet - http://opensource.atlassian.com/project ... wse/HV-229


I could probably write that class and email it to you if you want. I've written custom validators before.


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 10:34 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I've done some digging via google and stuff... and it says that this class is supposed to be in the cfg package of the core hibernate project. Well, I have hibernate 3.3.2ga - the latest version... and there is no such class file :(

Do I need to use an experimental version of hibernate as well? And via maven, is there any special trick to getting it? I don't see any versions past 3.3.2ga

I just upgraded to 3.5.0.beta-1 and the class file is still not there. Now I'm totally and absolutely stumped.


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 10:51 am 
Hibernate Team
Hibernate Team

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

I am not sure where and what you found on the web, but there is no URL constraint in Hibernate Validator 3.x.
At least not in the current release.

Regarding the BeanValidationEventListener. It is part of the latest (unreleased) version of Hibernate Annotations. What I was saying before is that you need your own (modified) version of this class as part of your code. You can view the code on fisheye as well - http://fisheye.jboss.org/browse/Hiberna ... validation

--Hardy


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 11:01 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Oh, I know that Url was not part of 3.0... I'm just saying if you guys are going to port Email over, you might as well create a Url to pair with it. It makes perfect sense to provide this.

I managed to get that class. I eventually figured out that I need to 3.5.0-Beta-1 of annotations. I won't even ask what it's doing in there... I'm sure there's a reason, but I would not have thought of looking there. Especially since it was in the cfg package (core came to mind). Oh well, that's a problem for another day.

The only thing I have left in order for my project to run is to setup a validation factory. So once I get that setup (I hope I figure it out how to do it correctly in spring), I should be all set ;)


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 11:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Oh, I know that Url was not part of 3.0... I'm just saying if you guys are going to port Email over, you might as well create a Url to pair with it. It makes perfect sense to provide this.

It's more a question of the design of this constraints. Just plain @Url or will the constraint have parameters like protocol, etc? Or as you mentioned, will it handle mailto. So even so it makes sense to have a Url constraint, it does not make sense to rush an implementation. Given that we want to release GA quite soon it might make sense to wait for 4.1.

Quote:
I managed to get that class. I eventually figured out that I need to 3.5.0-Beta-1 of annotations. I won't even ask what it's doing in there... I'm sure there's a reason, but I would not have thought of looking there. Especially since it was in the cfg package (core came to mind). Oh well, that's a problem for another day.

If you look at the other classes in there we are implementing auto-registration. In many scenarios you will have Hibernate Annotations in your classpath. If so, we check at bootstrapping time whether for example Hibernate Validator or Hibernate Search are in the classpath and activate it for you (of course it can also be turned off). It there to make the life easier for the users.

Quote:
The only thing I have left in order for my project to run is to setup a validation factory. So once I get that setup (I hope I figure it out how to do it correctly in spring), I should be all set ;)

Validation.buildDefaultValidatorFactory() is the easiest way of doing this. How you do this in Spring is of course a different matter.

--Hardy


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 11:34 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I got it to be "sort-of" working with the following spring beans configuration, if anyone else needs to know as well - saves them some time.

Code:
    <bean id="validationFactory" class="javax.validation.Validation"
          factory-method="buildDefaultValidatorFactory" />
   
    <bean id="beanValidationEventListener" class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener">
        <constructor-arg index="0" ref="validationFactory" />
        <constructor-arg index="1">
<!-- I dunno what the heck goes here -->
            <props></props>
        </constructor-arg>
    </bean>


Wiring in the listener is the same as before:
Code:
        <property name="eventListeners">
            <map>
                <entry key="pre-insert" value-ref="beanValidationEventListener" />
                <entry key="pre-update" value-ref="beanValidationEventListener" />
            </map>
        </property>


I do get one exception though, and of course, I have no idea what the heck it means as Groups are something that was added:
Code:
javax.validation.ConstraintViolationException: Invalid object at persist time for groups [javax.validation.groups.Default, ]
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:83)
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:51)
   at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:142)
   at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:538)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:530)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:526)
   at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:740)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)


I'm reasonably confident everything else I did is accurate. Even going through the messages names (validator.notEmpty and so on) and changing the ValidatorMessages to ValidationMessages.properties... Man, there were so many changes to be made to upgrade ;)

I thought this might be due to my customized @QueryConstraint, but nope... says the same exception. Turns out the problem has to do with this:
@Pattern( regexp = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$",
message = "{userAccount.email.valid}" )

LOL. Oh, the irony. The 1 thing I change because @Email wasn't supported... and I get it wrong :) Of course, I dunno what's wrong exactly about it because the exception says nothing to me personally.

I'll keep plugging...


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Wed Sep 23, 2009 12:09 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I got from getting something to eat ;)

Well, it would appear that the regular expression is wrong.

It would appear that the exception message problem is exactly the same it was in 3.1.0 GA Harry.

As a result, I have to do stuff like this:
Code:
      try {
         userAccountDao.save( userAccount );
         clear();
      } catch( ConstraintViolationException e ) {
         for( ConstraintViolation constraintViolation : e.getConstraintViolations() ) {
            System.out.println( constraintViolation.getMessage() );
            System.out.println( constraintViolation.getInvalidValue() );
            System.out.println( constraintViolation.getLeafBean() );
            System.out.println( constraintViolation.getMessageTemplate() );
            System.out.println( constraintViolation.getConstraintDescriptor() );
            System.out.println( constraintViolation.getPropertyPath() );
            System.out.println( constraintViolation.getRootBeanClass() );
         }
      }


I know this is bad, but you see why I'm doing it right? I don't even know which object it's on - which is actually worse than it was in 3.1. At least in 3.1, it said there was a problem with UserAccount. Now, it just says some crap about groups :(

I think a much, much better error message would be this Harry:
Code:
javax.validation.ConstraintViolationException: object of class jawbs.domain.user.UserAccount has constraint violations [ ( property: "emailAddress", message: "Must be a valid email address" ) ]
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:83)
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:51)
   at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:142)
   at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
....


This would be SOOOOOOO Helpful. Like, a million times better. Please pitch this ;(


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Thu Sep 24, 2009 5:55 am 
Hibernate Team
Hibernate Team

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

I still argue that from a proper programmatic handling for an application you will have to inspect the ConstraintViolation anyways.

I also noticed that there is no custom implementation of toString() in ConstraintViolationImpl. That I will add. I will also add the class name to the ConstraintViolationException. Makes sense.

The problem with adding all violation exception messages to the ConstraintViolationException message is that this could get arbitrary long. I don't think that it is good practice to have long messages in an Exception. Also think about that the constraint violations are contained in a set which means that the order in which the messages would appear in the exception will not be defined.

That said, I think I will add a debug/trace message to the BeanValidationEventListener. If the corresponding log level gets enabled for the listener toString() on all ConstraintViolations will be logged. WDYT?

Last, but not least - since you have a "own" version of BeanValidationEventListener now, it is really up to you to change the message. Just change the code where the ConstraintViolationException gets created. ;-)

--Hardy


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Thu Sep 24, 2009 6:10 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I'm so happy to hear all of this! YAY!

I think adding a lot of this stuff would definitely decrease the burden to using the library.

I do not think though that having to inspect the exception is necessary when testing. Sure, if we are writing code to check constraints programmatically, then absolutely - I am totally 100% behind that. For testing though, it is much faster for the application developer to just get instant feedback, fix it and move on.

I can see your point about things getting arbitrarily long. You could take the approach that Hibernate uses for sql generation - the ability to turn it on and off, and also have an option to do "pretty formatting" or not.

Conceptually, testing sql statements and getting good feedback is actually very much in line which writing validation annotations, regular expressions, etc. This is application developer feedback, and we want to maximize and configure this as much as possible so developers can leverage as much or as little as they need to, just as if they were writing hql.

As a shallow compromise, even if the property names were given, that would go a LONG WAY to resolving this issue and making it a much lighter framework to test with.

For example, if I got back UserAccount and "emailAddress", I'm pretty sure I can figure it out which annotation is messed up if I only had 2 or 3 to pick from. For example, I could look and see that I wrote, "bob.newman@gmail" and see that I forgot the domain. I'd go, "Oh silly me" and fix it, rerun the test. This is not that bad - it's pretty good feedback. Having no property information makes things really tough because I feel blind and hopeless as to what is going on.

Thanks Harry for adding this stuff! Seriously, we all love you and now lots of people are going to benefit from this, which is awesome!


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Sat Sep 26, 2009 7:31 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Hi Hardy, where can I get the latest and greatest version of this through maven so I can get these features?

Also, did you include the property names in addition to the object?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 29 posts ]  Go to page Previous  1, 2

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.