0

I know it is not new question in this forum but I am very confused what should i do.

Problem: I am developing one application with spring mvc + hibernate. For server side validation I am using @valid annotation in controller and @null and @notNull annotation in my bean.

e.g

public class User implements Serializable {

    private static final long serialVersionUID = 2158419746939747203L;

    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long userId;

    @Column(name="USERNAME", unique = true)
    @NotEmpty @NotNull @Size(min=6, max=20)
    private String username;

    @Column(name="PASSWORD")
    @NotEmpty @NotNull @Size(min=6, max=20)
    private String password;

This validation is happening fine and data is also saving in DB.

But I want to validate unique constraint,referential integrity and other constraint using annotation without any validator class.

Is it possible? if no, then what is best and easiest way to to do it(less coding)? I will appreciate if framework will do it for me.

Sourabh
  • 59
  • 1
  • 15

1 Answers1

1

Saurabh,

For unique constraint in table,

 @Id

You will be able to enforce referential integrity via hibernate annotations as well eg.

@OneToOne(mappedBy = "foo")

Here is an example post

Referential integrity with One to One using hibernate

Here is a very detailed tutorial also exploring the same:

http://www.journaldev.com/2882/hibernate-tutorial-for-beginners-using-xml-annotations-and-property-configurations

You could write a "CustomUniqueConstraintValidator" kinda like mentioned in

http://www.journaldev.com/2668/spring-mvc-form-validation-example-using-annotation-and-custom-validator-implementation

You can also pass in paramters from the annotation to the custom validator. eg.

@CustomValidDate("columnName")

To make a generic class that applies for any field /column 1. YOu can write a generic custom validator 2. use annotaiton parameters (on each class attribute) to pass in the table name and column name. 3. Then in the validator you can use the table name, column name to apply your validation logic (unique etc).

Thanks, Paul

Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 13
  • 15
  • Hi Paul, Thank you for the answer..but in above URL they are mentioning about project set up..they are not talking about validation...I want my validation errors in BindingResult object...like i am getting for NotEmpty NotNull..these kind of notations..or you can say..I want my bean to be validated from DB along with "notEmpty" notation – Sourabh Feb 28 '15 at 20:28
  • Refrential integrity is secondary part...right now I am struggling with validation in single table for unique constraint etc – Sourabh Feb 28 '15 at 20:31
  • @sourabh checkout this post http://stackoverflow.com/questions/3495368/unique-constraint-with-jpa-and-bean-validation/3499111#3499111 Is this what you are trying to do? – Paul John Mar 01 '15 at 04:56
  • Now I am going this way http://codetutr.com/2013/05/29/custom-spring-mvc-validation-annotations/...Custom Validation for unique constraint but I need some more information...I want to make it generic like should be useful for any class and ant DB table and any Column name....I need some more help..how can i do it. How can I inject "any" class to check unique value, using reflection or spring framework – Sourabh Mar 01 '15 at 10:23
  • so you are looking at a "CustomUniqueConstrainValidator" ? But the catch is that you would like to use it on ony model object - and have that apply for that particular field / table? – Paul John Mar 01 '15 at 10:28
  • yes..u are right..so idea is, in my model object UniqueCheck("UserDaoImpl"), I will put this annotation and in my custom validator, I will autowire this impl class dynamically(anyhow, i don know how to do that) and call a function like "getDataByColumnName" which will be present in all the DAOimpl classes.....What do say?..Do you have any better idea? or did you ever see similar kind of thing already implemented? so that i will get some idea – Sourabh Mar 01 '15 at 10:36
  • you could use a custom validator and try passing in table name / field name as parameters via the annotaiton..see if what i typed above helps? – Paul John Mar 01 '15 at 10:38
  • So You are saying...instead of using that bean specific daoimpl..i should create separate daoimpl class to make this check...ok I think..I got it..let me try it – Sourabh Mar 01 '15 at 10:43
  • one more small doubt...already I am defining table name (class level) and column name (variable level)...can't i take those values any how? in my custom validator – Sourabh Mar 01 '15 at 10:50
  • I see that class are defined mapped to tables and attributes to columns. However, since ours is a generic validator, it would need to know what table + column it's validating the value against. – Paul John Mar 01 '15 at 10:59
  • There maybe another way, but i dont know of it..this was all i could think of and came up in my research as well – Paul John Mar 01 '15 at 11:00
  • table name..we can get using below code: Class> c = entity.getClass(); //Get Table Name Table table = c.getAnnotation(Table.class); String tableName = table.name(); – Sourabh Mar 01 '15 at 11:03
  • if it worked, i would appreciate if you mark my answer correct :-). good luck saurabh. – Paul John Mar 01 '15 at 11:17