2016-02-03 40 views
0

我是新来的弹簧& hibernate,请建议一个最好的方式来提供hibernate项目的验证弹簧mvc &。我尝试过hibernate验证器,但是我不知道如何在实体对象之间存在关系(如@OneToOne)时使用它。如果有任何示例,请提供链接。提供弹簧mvc&hibernate项目验证的最佳方式

+0

您对@OneToOne的期望是什么样的验证关系? –

回答

0

您的@OneToOne关系不够清晰。你能给一个具体的场景吗?

在Spring MVC你有三种验证的:

  • JSR-303 Bean验证(Hibernate验证)
  • 自定义验证
  • 春天验证

JSR-303的伟大工程用于单个字段的简单验证。您有标准注释(如@Max,@Min,@NULL,@Pattern等)。在这种情况下,对你来说这似乎不够。

自定义验证用于更复杂的场景。例如,如果我们需要验证新添加的产品ID与任何现有产品ID不相同,该怎么办?

Spring验证表示交叉字段验证。 例如,当我们想要比较两个或多个字段时,使用它来查看 的值是否可以在组合时被视为有效。

0
you can use spring validation method. 
using valid annotion in controller classes infront of modelattribute annotaion. 
then you can use validaions in the model class. 

below is an example for controller and bean classes. 

this is a many to one configuration inside a bean class: 

@ManyToOne(targetEntity = UserType.class) 
@JoinColumn(name = "user_type", referencedColumnName = "id") 
@NotNull 
private UserType userTypeTb; 

this is a method inside controller class with valid and modelattribute annotaion: 

public ModelAndView home(@Valid @ModelAttribute("user") User user, BindingResult result, 
    HttpServletRequest request) { 
// 
// 
}