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