-->
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 1, 2  Next
Author Message
 Post subject: Three questions about Hibernate Validator
PostPosted: Sat Sep 19, 2009 9:43 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
1) Is there a URL Pattern in 3.1.0 GA? I don't think so. Is there one planned? Just use my own regular expression?

2) How easy is it to connect to a Spring DAO (using hibernate) within a custom validation constraint? I want to check the database to make sure nobody has the same email except for that person. Any direction on how to do this easily? The easiest thing I could think of is use @assertTrue() on a isEmailUnique() method, and then use aop to give the domain object a reference to the dao to verify this. I think I'd rather just specify a @Unique constraint though and hope the magic happens for me as this is a very common use case that takes time to setup over and over to prevent duplicates.

3) How can I get InvalidStateException to show the actual error messages in the string that it generates at the highest level? I know I can query the exception for details... but when testing, I really just want to see the errors when the exception is unexpected thrown. Given how it is... it's too much manual effort to figure this out. I want to see this information instantly so if I'm creating objects part of a database test and they are wrong, I can easily correct them.

Thanks!


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Sat Sep 19, 2009 9:56 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
See, right now I have tested this:

Code:
   @AssertTrue
   public boolean isEmailUnique() {
      return !( emailAddress.equals( "my.email@gmail.com" ) && id != 1 );
   }


Since my.email@gmail.com with id 1 is already in the database, this would be invalid. Let's pretend this line checks the database. If it did, then this constraint would be okay, right?

So I guess the challenge is getting spring to wire in the Dao it needs to this Domain Object (which is something we shouldn't do... isn't it?)

Is there any other way to do this more cleanly? Using AOP to weave non-spring beans is... shall we say... messy? I know that testing becomes a problem because if the IDE compiles the tests and does not weave them, tests fail. I also know that Spring 3.0.0M4 will not weave properly using sunfire plugin within maven either. So I'd rather not use this approach.


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Tue Sep 22, 2009 7:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
1) Is there a URL Pattern in 3.1.0 GA? I don't think so. Is there one planned? Just use my own regular expression?

No, not at the moment. I guess it would make sense to add it to Validator 4.x. Whether we will backport it into the legacy code (3.x) - not sure!?
http://opensource.atlassian.com/project ... wse/HV-229

Quote:
2) How easy is it to connect to a Spring DAO (using hibernate) within a custom validation constraint?

In the 4.x version of Validator you would use a custom ConstraintValidatorFactory. This would for example allow you to hook into Springs IOC.

Quote:
3) How can I get InvalidStateException to show the actual error messages in the string that it generates at the highest level? I know I can query the exception for details... but when testing, I really just want to see the errors when the exception is unexpected thrown. Given how it is... it's too much manual effort to figure this out. I want to see this information instantly so if I'm creating objects part of a database test and they are wrong, I can easily correct them.

If you just need it for developing your tests you might be able to configure a custom view for InvalidStateException. Most IDEs have a feature like this. Otherwise you could just call ClassValidator.getInvalidValues() which returns an array of InvalidValue. Of course that might not work if you need Validator as part of another framework.

--Hardy


Top
 Profile  
 
 Post subject: Re: Three questions about Hibernate Validator
PostPosted: Tue Sep 22, 2009 8:29 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Is version 4.x stable enough for production use?

As for the exception output, it's really important that I am not adding temporary test code, even if it is just calling into ClassValidator.blah blah. This is a waste of time, frankly.

Try making some tests with a big database where all the objects are using the validation framework, and just make some test objects. You'll see that it's impossible for a human to perfectly construct valid objects on the first go... unless they come from dbunit (and even in that case, construction them the first time can still be wrong). Time and again, one must put up with these exceptions that give no information.

Basically, writing any code or using a debugger to see the output is inexcusable. This approach wasn't made with development in mind, agile or not. The string output is very necessary I would think to fast test/code cycles.

As for hooking that into my IDE? I don't know. But that also means everyone working on the project must do the same workaround. It really would be simpler to just have it come out of the box ;)


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

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Is version 4.x stable enough for production use?

The latest version is 4.0 CR and the official GA release is only a couple of weeks away. In my opinion it is best to switch to 4.x, especially if you are developing new code. 4.x is based on a standard (JSR 303) and while there are a lot of similarities it just makes sense to use the new API.

In which environment are you using Validator? If you are using it with JPA 1 you will have to add a custom event listener to your code and register the listener in your configuration. The event listener looks something like this - http://fisheye.jboss.org/browse/Hiberna ... tener.java. However, in its current version it won't work to configure the listener via the hibernate config file (no no-arg constructor). There is a Jira for that (http://opensource.atlassian.com/project ... wse/HV-232) and I will change the code shortly. If you want you can try it in your environment

Quote:
As for the exception output, it's really important that I am not adding temporary test code, even if it is just calling into ClassValidator.blah blah. This is a waste of time, frankly.

Try making some tests with a big database where all the objects are using the validation framework, and just make some test objects. You'll see that it's impossible for a human to perfectly construct valid objects on the first go... unless they come from dbunit (and even in that case, construction them the first time can still be wrong). Time and again, one must put up with these exceptions that give no information.


I still don't see your problem. Add a helper class to your unit test framework which dumps the actual error messages when a constraint violation exception is thrown. This is for example the approach we take in the TestUtil class in the 4.x codebase. Maybe you find that once you switch to 4.x your problems disappear anyways.


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I guess I will upgrade to 4.0 and check it out. I haven't programmed too much validation stuff, so the impact should be low right now.

I have a question though... what is the maven repository for this release? Maven is not picking it up when try version "4.0.0 CR1". I've also googled and tried this:

Code:
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-legacy</artifactId>
            <version>4.0.0 CR1</version>


Which is not working. IntelliJ IDEA is usually pretty good about refreshing the indices, so it can't find it. I also added a specific repository to hand hold maven, but that hasn't helped.

Code:
        <repository>
            <id>Hibernate Repository</id>
            <url>http://repository.jboss.org/maven2</url>
        </repository>
        <repository>
            <id>JBoss Repository</id>
            <url>http://repository.jboss.org/maven2/org/hibernate/hibernate-validator-legacy/</url>
        </repository>


Could you let me which repository I should be using?


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Oh, I'm also not using JPA. I hate JPA, so I stick with regular hibernate mappings. Works fine for me.


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Oh, and as for the exception message - this is really important.

Let's say I have an object called Employer, and I'm writing a test and I forget to set it's name. It's *ME* that made the mistake... not some code or something else. Employer also has about 40 other properties on it.

So, I go to the run the test, and then I get this:
Code:
org.hibernate.validator.InvalidStateException: validation failed for: jawbs.domain.employer.Employer
   at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:148)
   at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)
   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)


Wow... what a fantastic exception. It tells me nothing other than that employer has an error in it. How many? I dunno. Which properties? Who knows?

Seriously, InvalidStateException should throw something more useful. Imagine if it actually iterated through InvalidValues[] array and just told me what it was? If it did that, I could just add employer.setName( "abc" );... and presto! - My test would pass!

This gets worse when multiple properties are not set. The programmer is in the freaking dark.

I think this definitely warrants a change - this is not productive in the least. Every other framework in existence usually has better exception messages than this, unless it's some parser error that's delegating to a library.

The point is that *I* as a developer shouldn't have to call into anything else to get this information. I really just want to read it, make the change and rerun the test. Simple.


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

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
I guess I will upgrade to 4.0 and check it out. I haven't programmed too much validation stuff, so the impact should be low right now.

I have a question though... what is the maven repository for this release? Maven is not picking it up when try version "4.0.0 CR1". I've also googled and tried this:


It is the JBoss repo, but I think you got the version wrong. It's 4.0.0.CR1 - http://repository.jboss.com/maven2/org/ ... /4.0.0.CR1

--Hardy


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

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Wow... what a fantastic exception. It tells me nothing other than that employer has an error in it. How many? I dunno. Which properties? Who knows?


True to a certain degree. Anyways, even when not using JPA, but rather plain Hibernate you will need a custom event listener similar to the BeanValidationEventListener. I will make some modifications to the existing one shortly so that it will be usable via the Hibernate configuration file in plain Hibernate as well as JPA 1. Or maybe you want to provide a patch ;-)

If you look at the way the exception is created when a constraint violation occurs you can see that it is quite trivial to change the error message to include all error messages.
Code:
if (constraintViolations.size() > 0 ) {
   throw new ConstraintViolationException(
      "Invalid object at " + operation.getName() + " time for groups " + toString( groups ),
      (Set<ConstraintViolation<?>>) unsafeViolations);
}


--Hardy


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
hardy.ferentschik wrote:
Quote:
I guess I will upgrade to 4.0 and check it out. I haven't programmed too much validation stuff, so the impact should be low right now.

I have a question though... what is the maven repository for this release? Maven is not picking it up when try version "4.0.0 CR1". I've also googled and tried this:


It is the JBoss repo, but I think you got the version wrong. It's 4.0.0.CR1 - http://repository.jboss.com/maven2/org/ ... /4.0.0.CR1

--Hardy


Oh, the main problem was that both the legacy artifact id and the version were wrong. For just the basic hibernate-validator artifact id, only the version is not being picked up. I dunno why maven is not picking it up.

This still does not work:
Code:
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.0.0.RC1</version>
        </dependency>


You see, in IDEA, I can just press ctrl-space... and IDEA will tell me all the versions available. None of the 4.0 versions are coming up. When I manually put the version in, it's RED - so something is definitely wrong.

:(


Last edited by mystic on Wed Sep 23, 2009 8:08 am, edited 1 time in total.

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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
hardy.ferentschik wrote:
Quote:
Wow... what a fantastic exception. It tells me nothing other than that employer has an error in it. How many? I dunno. Which properties? Who knows?


True to a certain degree. Anyways, even when not using JPA, but rather plain Hibernate you will need a custom event listener similar to the BeanValidationEventListener. I will make some modifications to the existing one shortly so that it will be usable via the Hibernate configuration file in plain Hibernate as well as JPA 1. Or maybe you want to provide a patch ;-)

If you look at the way the exception is created when a constraint violation occurs you can see that it is quite trivial to change the error message to include all error messages.
Code:
if (constraintViolations.size() > 0 ) {
   throw new ConstraintViolationException(
      "Invalid object at " + operation.getName() + " time for groups " + toString( groups ),
      (Set<ConstraintViolation<?>>) unsafeViolations);
}


--Hardy


I don't think I would know about the library enough to provide a patch. I actually don't even know how to do that for an open source project.

I know that it would be trivial to change the text.. all I'm saying is that the users of the library shouldn't have to. I Mean, would anyone really object to this change? If I'm annoyed by it, I'm sure other people are. And maybe some people just don't question it... like they don't mind doing extra work? I dunno. If I had designed the framework, I would definitely output the textual information as part of the exception.

It's a shame really, because I like the framework - it's good stuff... but this stupid exception makes the framework a 9/10 and turns it into a 2/10. It's that big of deal for me ;/


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Code:
Downloading: http://repository.jboss.org/maven2/org/hibernate/hibernate-validator/4.0.0.RC1/hibernate-validator-4.0.0.RC1.pom
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:pom:4.0.0.RC1' in repository Hibernate Repository (http://repository.jboss.org/maven2)
Downloading: http://maven.springframework.org/milestone/org/hibernate/hibernate-validator/4.0.0.RC1/hibernate-validator-4.0.0.RC1.pom
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:pom:4.0.0.RC1' in repository Springframework milestone (http://maven.springframework.org/milestone)
Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate-validator/4.0.0.RC1/hibernate-validator-4.0.0.RC1.pom
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:pom:4.0.0.RC1' in repository central (http://repo1.maven.org/maven2)
Downloading: http://repository.jboss.org/maven2/org/hibernate/hibernate-validator/4.0.0.RC1/hibernate-validator-4.0.0.RC1.jar
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:jar:4.0.0.RC1' in repository Hibernate Repository (http://repository.jboss.org/maven2)
Downloading: http://maven.springframework.org/milestone/org/hibernate/hibernate-validator/4.0.0.RC1/hibernate-validator-4.0.0.RC1.jar
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:jar:4.0.0.RC1' in repository Springframework milestone (http://maven.springframework.org/milestone)
Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate-validator/4.0.0.RC1/hibernate-validator-4.0.0.RC1.jar
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:jar:4.0.0.RC1' in repository central (http://repo1.maven.org/maven2)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.

Missing:
----------
1) org.hibernate:hibernate-validator:jar:4.0.0.RC1


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I fixed it. Once I changed jboss.org to jboss.com, I could see it and I selected 4.0.0.CR1

I realized I had it RC1 before, but it didn't matter.. maven couldn't pick any of the 4.0 versions up. After comparing the url strings, I realized org was different than com... and that fixed it :/


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

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Am I to understand that @Email was removed? :(((((((((((((((((((((((((((((((((((


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 1, 2  Next

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.