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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.