Using bean Validation in hibernate

I was wondering if it was possible to use Java Bean Validation in hibernate, and how they integrate with each other.

I have a stack that consists of a Jax-rs API, and JPA in my data layer.

I was wondering if I it could use Java Bean validation to validate my Hibernate Entities, without using Spring.

Could I use the annotations from hibernate along with the ones from the javax.validation.contraints together

for example:

@Column(nullable = false)
@Size(min =8, max = 12)
@NotNull(message = "Phone Number must be entered")
private String phoneNumber;

here I specify that I the column, can't be null through hibernate and the bean validation.

Is this a good practice?

Or is there an alternative to validating data in hibernate, without bean validation like such?

Upvotes: 2

Views: 2815

Answers (3)

Jashu Kumar
Jashu Kumar

Reputation: 1

Since Hibernate also have their own validation annotations, for example @NotBlank, I don't think it's a bad practice to use javax.validation.constraint here. As far as I know, Hibernate even tries to consider all of these annotations.

So, for example, a field annotated with @NotNull will not be nullable in the generated table (so adding nullable = false is redundant), a String field annotated with @Size(max=2047) will be a varchar(2047) in MySQL instead of the default varchar(255).

Upvotes: 0

logi0517
logi0517

Reputation: 845

Since Hibernate also have their own validation annotations, for example @NotBlank, I don't think it's a bad practice to use javax.validation.constraint here. As far as I know, Hibernate even tries to consider all of these annotations.

So for example a field annotated with @NotNull will not be nullable in the generated table (so adding nullable = false is redundant), a String field annotated with @Size(max=2047) will be a varchar(2047) in MySQL instead of the default varchar(255).

This can be useful to read: http://hibernate.org/validator/

The whole reference docs for the project: https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#preface

EDIT: Based on Thorben Janssen's answer, the rest of my original answer below this can be discarded :)

I'm unsure if some more complicated constraints (for example a regular expression for phone numbers) are automatically enforced at the data layer or not. For example if you have a @Pattern for your phoneNumber field, that will work when your input is deserialized into your object. But if your setter methods dont have the same validation constraints, you might have an object in memory from some source with an incorrectly formatted phoneNumber that could be saved to the database. The safest way to use these constraints would probably include using programmatic validation with Validator.validate() before your database saves and updates.

Upvotes: 1

Thorben Janssen
Thorben Janssen

Reputation: 3275

The Bean Validation specification integrates with Hibernate and all other implementations of the JPA 2.x specification. I explained that in great detail in my article How to automatically validate entities with Hibernate Validator.

Let me give a quick summary:

If you add an implementation of the Bean Validation specification, e.g., Hibernate Validator, to your project, Hibernate automatically triggers the validation before inserting and updating an entity. You can customize that and also trigger validation before removing an entity. I explained that in more details in my article.

You can use all features of the Bean Validation specification, including custom validation rules, to validate your entity attributes. I used that here to validate that the value of an entity attribute is within a defined range and here to check that only one out of 2 associations is set.

The example mapping that you posted in your question is a very good practice! The @Column(nullable = false) part is not strictly necessary because the validation rule already ensures that the attribute can't be null.

Upvotes: 7

Related Questions