我有一個應用程序在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