我有一个应用程序在50个域类中使用bean验证。它已经在Spring MVC控制器中使用@Valid几个月没有任何问题。Spring MVC怪异问题,使用@Valid和Hibernate进行Bean验证
现在突然之间,我在Hibernate中做了许多“懒”字段来提高性能。我不得不处理各种奇怪的问题,从equals()方法不再有效,因为数据未加载而导致会话崩溃的对象。
我遇到了一个非常奇怪的问题,现在我将这些数据加载到Spring MVC表单上的会话属性中,视图正确呈现它,但是当传递给@Valid时,它会报告所有字段即使数据是100%有效的,也有错误。
public class EducationFacility extends DomainObject {
/* Members */
@NotEmpty(message = "{educationFacility.name.notEmpty}")
private String name;
@Valid
private Address address = new Address();
@Pattern(message = "{educationFacility.phoneNumber.valid}",
regexp = "(\\()?(\\d){3}(\\))?(\\s|-)(\\d){3}(\\s|-)(\\d){4}")
private String phoneNumber = "";
...
}
这里是休眠的定义:
<class name="jobprep.domain.educationfacility.EducationFacility" table="education_facility">
<id name="id" column="education_facility_id" type="long">
<generator class="native" />
</id>
<property name="name" column="name"/>
<component name="address" class="jobprep.domain.educationfacility.Address">
<property name="address" column="address"/>
<property name="postalCode" column="postal_code"/>
<many-to-one name="province" class="jobprep.domain.educationfacility.Province" column="province_id" />
</component>
<property name="phoneNumber" column="phone_number"/>
<property name="isEnabled" column="is_enabled"/>
<property name="homepageViewable" column="homepage_viewable" />
<property name="coursesCreated" />
<many-to-one name="admin" class="jobprep.domain.user.Admin" column="admin_id" />
<many-to-one name="director" class="jobprep.domain.educationfacility.Director"
column="director_id" cascade="all" />
<bag name="teachers" inverse="true" cascade="all-delete-orphan" order-by="username asc">
<key column="education_facility_id" />
<one-to-many class="jobprep.domain.teacher.Teacher" />
</bag>
<bag name="students" inverse="true" cascade="all-delete-orphan" order-by="username asc">
<key column="student_education_facility_id" />
<one-to-many class="jobprep.domain.student.Student"/>
</bag>
<bag name="ipRestrictions" inverse="true" cascade="all-delete-orphan">
<key column="education_facility_id" />
<one-to-many class="jobprep.domain.educationfacility.IpRestriction" />
</bag>
<bag name="allowedModules" table="education_facility_to_module"
inverse="false" lazy="true">
<key column="education_facility_id" />
<many-to-many class="jobprep.domain.module.Module" column="module_id"/>
</bag>
</class>
这里的控制器定义:
@Controller
@RequestMapping("/myEducationFacility")
@SessionAttributes("educationFacility")
@PreAuthorize("hasRole('ROLE_DIRECTOR')")
public class MyEducationFacilityController extends ControllerSupport {
....
}
这里的MVC的保存方法:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@Valid EducationFacility educationFacility, BindingResult result, SessionStatus status) {
if(result.hasErrors()) {
return view("index");
} else {
adminService.saveEducationFacility(educationFacility);
status.setComplete();
return redirect("?complete=true");
}
}
这里的春天增加了错误到bindi当save()被Spring调用时的结果。这些是完全错误的:
{org.springframework.validation.BindingResult.educationFacility=org.springframework.validation.BeanPropertyBindingResult: 4 errors
Field error in object 'educationFacility' on field 'phoneNumber': rejected value [(519) 254-3678]; codes [Pattern.educationFacility.phoneNumber,Pattern.phoneNumber,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [educationFacility.phoneNumber,phoneNumber]; arguments []; default message [phoneNumber],[Ljavax.validation.constraints.Pattern$Flag;@29895454,(\()?(\d){3}(\))?(\s|-)(\d){3}(\s|-)(\d){4}]; default message [Must be of the form: ###-###-####]
Field error in object 'educationFacility' on field 'address.address': rejected value [Windsor]; codes [NotEmpty.educationFacility.address.address,NotEmpty.address.address,NotEmpty.address,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [educationFacility.address.address,address.address]; arguments []; default message [address.address]]; default message [Address may not be empty]
Field error in object 'educationFacility' on field 'name': rejected value [Catholic School Board]; codes [NotEmpty.educationFacility.name,NotEmpty.name,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [educationFacility.name,name]; arguments []; default message [name]]; default message [Name may not be empty]
Field error in object 'educationFacility' on field 'address.postalCode': rejected value [N9a 2a5]; codes [Pattern.educationFacility.address.postalCode,Pattern.address.postalCode,Pattern.postalCode,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [educationFacility.address.postalCode,address.postalCode]; arguments []; default message [address.postalCode],[Ljavax.validation.constraints.Pattern$Flag;@29895454,[a-zA-Z]\d[a-zA-Z](\s|-)\d[a-zA-Z]\d]; default message [Postal Code must be of the format: a#a-#a#], educationFacility=class jobprep.domain.educationfacility.EducationFacility{id=3}}
帮助?
我会试试看。这很奇怪,因为在另外两种形式中,@Valid实际上是工作的(教育设施注册,以及管理员向系统添加/编辑新的教育设施时)。只有当导演编辑自己的教育设施时,它才会报告错误的错误。是的,我确保调用服务层并将其加载到数据库外 - 不使用当前用户会话中的那个。这种东西背后令人沮丧,并且让我想要彻底抛出休眠。 – egervari 2010-12-02 17:19:56