-->
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.  [ 2 posts ] 
Author Message
 Post subject: [Hiernate Validator] i18n and ClassValidator
PostPosted: Fri May 30, 2008 2:48 pm 
Newbie

Joined: Thu Sep 23, 2004 11:53 am
Posts: 8
Location: Maracaibo, Venezuela
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: Core (3.2.5), Annotations (3.3.1), Validator (3.0.0)
Name and version of the database you are using: Postgres SQL 8.3


[b]Some background information and Issue #1 [b]:
According to Hibernate Validator documentation you can create your own ValidatorMessages.properties and any localized version and those one defined by you shuold overwrite the one contained inside HV jar, unable to make this aprroach to work I resorted to passing a Resource Bundle to the ClassValidator object.

Mandante mandante = new Mandante();

mandante.setXXX(...);
...


ClassValidator<Mandante> classValidator = new ClassValidator<Mandante>(
Mandante.class, ResourceBundle.getBundle( "com/procedatos/plataforma/modelo/recursos/ValidatorMessages", new Locale("es") ) );

InvalidValue[] validationMessages = classValidator.getInvalidValues( mandante);

Where Mandante class has all validations rules as well and JPA and Hibernate Annotations as needed by my object model.

This approach worked as advertised by the framework.

Now, I as a good practice I always try to program to Interfaces, so Mandante class has a corresponding IMandante interface, first surprise was in order to use the ClassValidator I had to change my code to


IMandante mandante = new Mandante();

mandante.setXXX(...);
...


ClassValidator<IMandante> classValidator = new ClassValidator<IMandante>(
IMandante.class, ResourceBundle.getBundle( "com/procedatos/plataforma/modelo/recursos/ValidatorMessages", new Locale("es") ) );

InvalidValue[] validationMessages = classValidator.getInvalidValues( mandante);

but had to move all validation annotations from Mandante class to IMandante class, leaving all JPA and Hibernate Core annotations in Mandante class.


[b]Question #1 [b]: Is there any way to inform HV to use the interface but applied validations to the concrete class, I was thinking something among the lines of Hibernate Core annotation usage of the targetEntity property:

@ManyToOne(targetEntity=Mandante.class, fetch = FetchType.LAZY)

I don't have a problem wit moving all my validations rules to my interfaces, but I was curious if this Interface/Class could approach be used with ClassValidator without making any changes to the API or it's a restricction of using Generics, perhaps any other technnical limitation used?

[b]Issue #2[b]:

Now let's supposed I have a ISociedad interface and Sociedad class with the followig structure

public interface ISociedad extends IDataTracking
{

/**
* Getter of the property <tt>mandante</tt>
* @return Returns the mandante.
*
*/
@Valid
public IMandante getMandante();

}


public final class Sociedad extends DataMaestra implements ISociedad
{

/**
* Mandante
*
*/
private IMandante mandante;


/**
* Getter of the property <tt>mandante</tt>
* @return Returns the mandante.
*
*/
@ManyToOne(targetEntity=Mandante.class, fetch = FetchType.LAZY)
@JoinColumn(name = "co_mandt", nullable = false, insertable = false, updatable = false)
public IMandante getMandante()
{
return this.mandante;
}


... String properties of Sociedad with getter and setters
}


So basically what I want to do is validated the String properties of Sociedad and at the same time recursively validate an associated object
in this case mandante.


But when I issue this unit test

ClassValidator<ISociedad> classValidator = new ClassValidator<ISociedad>(
ISociedad.class, ResourceBundle.getBundle( "com/procedatos/plataforma/modelo/recursos/ValidatorMessages", new Locale("es") )
);

InvalidValue[] validationMessages = classValidator.getInvalidValues( sociedad);

for (InvalidValue validationMessage : validationMessages)
{
System.out.println(validationMessage.getPropertyName() +" > "+validationMessage.getMessage());
}


On the ouput console of Eclipse I received my custom
validation message from the strings properties correctly
with their loclaized message and I received the key from
the validation of the class Mandante, rso I suspect reading the source
the code of HV the resource bundle pass by paramter to ClassValidator
it is not passed along to any child class validator.

[b]Console Output[b]
nombreCorto > Nombre Corto de la Sociedad no puede ser vaciĆ³ (NULL).
rif > RIF de la Sociedad no puede ser vaciĆ³ (NULL).
mandt > {modelo.organizacion.sociedad.mandt.not.null}
codigo > {modelo.organizacion.sociedad.codigo.not.null}


I think once validated Sociedad's string properties it tried to validate Mandante, i correctly detects the validation violations but since it cound't find any key in the original ValidatorMessages.properties it simples returns the key as the result for the message property.

Are there any tips of work arounds to fix this solution.



Thanks in advance,
Pedro Taborda

_________________
Pedro Taborda
Procedatos,S.A.
Phone: (58414)6124848


Top
 Profile  
 
 Post subject: Additionally to my original post
PostPosted: Mon Jun 02, 2008 10:56 am 
Newbie

Joined: Thu Sep 23, 2004 11:53 am
Posts: 8
Location: Maracaibo, Venezuela
Hi people, I would additionally ask how I could set-up my application (J2EE) in order to command HV to use my custom ValidatorMessages. properties, what I mean is there any way to tell HV to use a series of properties files in a global configuration form, what I would like to avoid is to tell every time I create a ClasValidator to pass a custom ResourceBundle. And how to applied in a recursive way when running @Valid on associated objects

TIA,
Pedro

_________________
Pedro Taborda
Procedatos,S.A.
Phone: (58414)6124848


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