为spring mvc& amp;提供验证的最佳方法休眠项目

时间:2016-02-03 13:39:49

标签: spring hibernate spring-mvc

我刚接触春天& hibernate,请建议一个最好的方法来为spring mvc& amp;休眠项目。我尝试了hibernate验证器,但是当实体对象之间存在关系(如@OneToOne)时,我不知道如何使用它。如果有任何示例,请提供链接。

2 个答案:

答案 0 :(得分:0)

您的@OneToOne关系不够明确。你能举个具体的场景吗?

在Spring MVC中,您有三种类型的验证:

  • JSR-303 bean验证(hibernate验证器)
  • 自定义验证
  • 春季验证

JSR-303非常适合单个字段的简单验证。你有标准的注释,如(@ Max,@ Min,@ NULL,@ Pattern等)。在这种情况下,这似乎不够。

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

Spring验证代表跨领域验证。 例如,当我们想要比较两个或多个字段以查看是否时使用它 合并后,他们的价值观可视为有效。

答案 1 :(得分: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) {
//
//
}