1

I am developing an application using Spring JPA 2.0 with Hibernate as a ORM provider. We have only read only access to database and will generate report. I would like do some validation while fetching data.

@Column(name = "LOGICAL_ID", nullable = false)
@NotNull
private Long logicalId;

I added Hibernate validator which implements JSR 303 specs. But while fetching it doesn't throw any runtime exception or ConstraintViolationException? Do i add something in the configuration or am i missing something? Please advice me.

1 Answers1

2

Like i've posted in Implementing cross-validation in java

You can use the following piece of code to validate an entity manually;

ValidatorFactory factory = Validation.byDefaultProvider().configure().traversableResolver(new CustomTraversableResolver() ).buildValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<YourBaseClass>> constraintViolations = Validator.validate(myEntityToValidate);

If you would like this to be done for you automatically, it might be that the 'javax.persistence.validation.mode' property is set to none (http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html).

I prefer the manual validation though, since then you have control and details, of which constraints are not passing.

Community
  • 1
  • 1
Marius
  • 3,043
  • 1
  • 15
  • 24
  • Hi Marius, Thanks for the response.Please forgive my ignorance: if i add bean validation mode = NONE in , then as per hibernate documentation NONE: Bean Validation is not used at all. Moreover in my case i would like to throw Runtime Exception. As per hibernate documentation : By default, Bean Validation (and Hibernate Validator) is activated. When an entity is created, updated (and optionally deleted), it is validated before being sent to the database. In my case i only read data from database and validate the data, if it doen't matches constraint i need throw Exception. – Sathish Murugesan Jul 17 '12 at 16:41