简体   繁体   English

Spring Hibernate Bean验证@valid

[英]Spring Hibernate Bean Validation @valid

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. 问题:我正在使用Spring MVC + Hibernate开发一个应用程序。 For server side validation I am using @valid annotation in controller and @null and @notNull annotation in my bean. 对于服务器端验证,我在控制器中使用@valid批注,在bean中使用@null和@notNull批注。

eg 例如

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. 这种验证进行得很好,数据也保存在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. 如果框架能为我做到,我将不胜感激。

Saurabh, 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 http://www.journaldev.com/2882/hibernate-tutorial-for-beginners-using-xml-annotations-and-property-configurations

You could write a "CustomUniqueConstraintValidator" kinda like mentioned in 您可以像下面提到的那样编写一个“ CustomUniqueConstraintValidator”

http://www.journaldev.com/2668/spring-mvc-form-validation-example-using-annotation-and-custom-validator-implementation 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. 要创建适用于任何字段/列1的通用类。您可以编写通用的自定义验证器2.使用注释参数(在每个类属性上)传入表名和列名。 3. Then in the validator you can use the table name, column name to apply your validation logic (unique etc). 3.然后,在验证器中,您可以使用表名,列名来应用验证逻辑(唯一性等)。

Thanks, Paul 谢谢保罗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM