주석을 사용하여 스프링 유효성 검사를 사용하여 양식 데이터의 유효성을 검사하고 싶습니다. 그래서 내가 예를 들어 다음과 같은 목적을 가지고 : 당신이 여기에서 볼 수 있듯이스프링 폼 유효성 검사는 데이터베이스 삽입을 확인합니까?
@Entity
@ComponentScan
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@NotEmpty
private String type;
...
}
내가는 type
문자열에 @NotEmpty
을 사용했다. 이 형식은 내 양식의 유효성을 검사 할 때만 사용하고 싶습니다. 데이터베이스 삽입에 대해 유효성 검증을해서는 안됩니다.
그래서 내가 수행 할 때 내가 그것을 작업 할 것처럼이 작동
@RequestMapping(value = "/myForm", method = RequestMethod.POST)
public String categoryPOST(HttpServletRequest request, Model model, @Valid Category category, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "form";
}
return "redirect:/results";
}
. 하지만 난 더미 개체를 만들 때 :
Category category = new Category();
을하고 나는 그 빈 객체에 저장 수행
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [my.project.jpa.entity.Category] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='darf nicht leer sein', propertyPath=type, rootBeanClass=class my.project.jpa.entity.Category, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
]
:
this.category_repository.save(category);
내가 (그것의 중요한 일부) 오류
그리고 그건 내가 원하는 것이 아닙니다. 폼 유효성 검사에 주석을 사용하고 싶지만 데이터베이스 작업에 유효성 검사를 수행하지 않으려합니다.
어떻게 든 가능합니까?
javax.validation.constraints.NotNull;
주석 :
추가 정보는 내가 사용하여 동일한 결과를 가지고 있었다.
이 BTW,'@ 엔티티에 ComponentScan' 주석 이상한 보인다. 당신이 필요하다고 확신합니까? –
아니요, 확실하지 않습니다. 완전히 새로운 봄. – Mulgard
대부분 제거 할 수 있습니다. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-scanning-autodetection –