PDA

View Full Version : Validation with Spring and Hibernate



jeeper
Feb 2nd, 2011, 08:09 AM
Hello,

Ive got one question regarding Bean Validation in Spring (with Spring MVC and Hibernate)
I was now researching some hours to find the best way to implement validation.

I have the following example.
- A User wants to register
- There is a register formular for username, email, validate email, password, validate password, accept-agb
- In the database the users data is saved in the table users(user_id, username, password, enabled), users_information(users_id, email, ....) and some other tables (authorities, salt...)
- At the moment I first check the data with java script, then I submit it to the server (the controller, they get the data with @RequestParam)
- Then I call methods of my own class RegistrationCheck, where I check if the username is free, the email is valid, the password secure, the emails typed in equal and so on!
- When the checks are successful, the user is registered (I use Hibernate for it)

Now I read of using FormObjects and Validation. Im a little bit confused what to do now. I already have the Hibernate Classes, which contain the restrictions I have set in database with the create table commands.
For example username not null, etc ... in the Hibernate Class: Users.java, the String username is annotated with @NotNull then.

Im not sure where the begin. Shall I write my own Validation Classes like public class UserToRegister implements Validator and set all values which are given in through the register-formular there and check them?
Or shall I extend the existing Hibernate Classes and add some more Annotations there? Im not sure what to do.
I need to write some checking methods by myself, I guess, because I need to check if the username is free and so on.

I would be glad if someone could give me a hint! :-) Thank you!

jeeper
Feb 2nd, 2011, 10:18 AM
after some more hours of researching, Im still not so much further. I have found this two approaches.

1.
- Write a JavaBean, containing member variables, like String username, annotated with @NotNull - but there are not many possibilities for those annotations
- Controller is given (@Valid JavaBean bean, BindingResult result) and checks if result.hasErrors()

--> But where do I implement checks like "does username exist", "@Email" (I found this in a tutorial) and other specific tests? @Email is from Hibernate-Validator, can I combine them by importing org.hibernate.validator.constraints.Email?

2.
- Write a JavaBean and then write a Class which implements Validator. This class implements the methods supports() (checks if this class is supported) and validate(). With validate you can call for example "ValidationUtils.rejectIfEmpty(e, "name", "name.empty")" or some similiar methods


Im confused of this two approaches. Can I combine them? Write a JavaBean, annotate the variables with @NotNull, etc. Then write a validator, the supports()-method checks then, if the class is supported: that means, if the annotations (@NotNull, etc, from the JavaBean) are correct for the class given.

Then, with validate() I check other things I want to validate? That means, I must write then my own methods, checking if the username is existing, etc? The ValidationUtils-Methods are just very very few!!

How do I call then the Validator? Not with @Valid, but by creating a Validator instance and then passing the object from the controller to it?

Well, it sounds a little bit complicated to me. I would like to know if Im on the right way. Thanks in advance!

Here is my RegisterForm-Class, I created until now :-)




import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;

public class RegisterForm {

@NotNull
@Size(min = 2, max = 30, message = "The size of your username must be between {min} and {max}")
private String username;

@NotNull
@Email
private String email;

@NotNull
@Email
private String valemail;

@NotNull
private String password;

@NotNull
private String valpassword;

@NotNull
private String agb;

// getter + setter