-->
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.  [ 6 posts ] 
Author Message
 Post subject: Custom Constraint Validation Messages
PostPosted: Thu Jun 03, 2010 1:35 pm 
Newbie

Joined: Thu Jun 03, 2010 1:10 pm
Posts: 6
Hi,
i'm currenlty using Hibernate 3.3.2 GA, Hibernate Validator 3.1.0 GA & Hibernate Annotations 3.4.0 GA.

Now i'm trying to upgrade to Hibernate 3.5.2 & Hibernate Validator 4.0.2 GA.

After reviewing the documentation, i came to know that
1. Annoations are now part of Hibernate core
2. Validation Framework is using the Validation API for some of the validations

I've Custom Validation Class, which used to implement "Validator" in the earlier version.

Now with the upgrade versions, we need to impement ConstraintValidator<HasIdentifier, Object> interface.

Now my real problem lies in here.

In the earlier version, i'm using
org.hibernate.validator.event.ValidateEventListener
Now after upgrading, i'm using
org.hibernate.cfg.beanvalidation.BeanValidationEventListener

Now when the add/update operations are performing, and if the custom constraint is failed, i'm getting the below error message when executing test case

javax.validation.ConstraintViolationException: validation failed for classes [com.test.ContactInfo] during persist time for groups [javax.validation.groups.Default, ]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:71)
at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:177)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.springframework.orm.hibernate3.HibernateTemplate$27.doInHibernate(HibernateTemplate.java:811)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:809)
at fleetcycle.dbof.framework.communication.Test_Hibernate_ContactInfo.testAdd(Test_Hibernate_ContactInfo.java:32)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


but earlier, i used to get my local message set for the custom constraint.

I need help on the following
=====================
1. Can you pl. confirm if the above files selected are correct?
if not correct, please let me know the file/package names to be used
2. how to have the hibernate custom validator can be picked the internatilazation message
Providing an example would be great helpful to me


Thanks in advance
Venky


Top
 Profile  
 
 Post subject: Re: Custom Constraint Validation Messages
PostPosted: Fri Jun 04, 2010 5:10 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
The ConstraintViolationException contains a set of ConstraintViolations. Call
Code:
Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations();
for(ConstraintViolation<?> violation : violations) {
    String message = violation.getMessage();
     ...
}


I think you are on the right track though.

--Hardy


Top
 Profile  
 
 Post subject: Re: Custom Constraint Validation Messages
PostPosted: Mon Jun 07, 2010 11:41 am 
Newbie

Joined: Thu Jun 03, 2010 1:10 pm
Posts: 6
Hi Hardy

Sorry in the last message, i didn't asked one doubt.

How to use "MessageInterpolator" in conjunction with "BeanValidationEventListener" to internationalize the custom messages

Example would be of great help.

Thanks
Venky


Top
 Profile  
 
 Post subject: Re: Custom Constraint Validation Messages
PostPosted: Mon Jun 07, 2010 11:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I am not quite sure what you mean. Can you clarify? BeanValidationEventListener has nothing to do with internationalizing messages. All it does it registering and orchestrating the validation calls. The ConstraintViolation instances already contain the i18ned messages.
Per default the MessageInterpolator will use the default Locale of the JVM to select the appropriate message resource bundle.

Does this help? What's your usecase?

--Hardy


Top
 Profile  
 
 Post subject: Re: Custom Constraint Validation Messages
PostPosted: Mon Jun 07, 2010 1:31 pm 
Newbie

Joined: Thu Jun 03, 2010 1:10 pm
Posts: 6
Hi Hardy,

i've an Custom ConstraintValidator class which checks for the some of the Custom validation on the fileds in the domain, and in the annotation, i will provide only the Key which needs to retrieve the message from appropriate resource bundle.

Can you pl let me know how to achieve this functionality.

I hope you will be clear on this

Thanks
Venky


Top
 Profile  
 
 Post subject: Re: Custom Constraint Validation Messages
PostPosted: Tue Jun 08, 2010 4:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
You have to place your messages into ValidationMessages.properties and add this file to the root of your classpath. This is the file Bean Validation looks for per default. If your message key is {my.company.CustomConstraint.message}, then you need the following in your properties file:

Code:
my.company.CustomConstraint.message=i18ed message


Hibernate Validator provides default resource bundles for some languages in org.hibernate.validator. HV also provides a ResourceBundleLocator interface which allows you to implement a custom strategy to locate and load resource bundles. Check the online documentation for that.

Hope this helps.

--Hardy


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