ฉันใช้ thymeleaf+sprinboot สำหรับเว็บแอปพลิเคชัน และฉันสงสัยว่าฉันจะควบคุมข้อผิดพลาดในการตรวจสอบความถูกต้องได้อย่างไรเมื่อฉันใช้ Value Objects (VO)
อย่างที่คุณเห็น เอนทิตีมีข้อจำกัด (ไม่ซ้ำกัน) สำหรับแอตทริบิวต์หนึ่งรายการ:
@Entity
public class Company {
@Id
@GeneratedValue
Integer id = 0;
@Column(unique = true)
String code;
String name;
String address;
...
}
แต่ VO ไม่ได้ตั้งใจให้มี:
public class CompanyVO {
Integer id = 0;
String code;
String name;
String address;
...
}
ดังนั้นตัวควบคุมจึงไม่สามารถตรวจสอบได้ว่าค่านั้นซ้ำหรือไม่ซ้ำกัน:
@RequestMapping(method = RequestMethod.POST)
public String set(@Valid CompanyVO companyVO, BindingResult result, Model model){
Company company = new Company();
if(result.hasErrors()){
return "company";
}
if(companyVO.getId() != 0){
company = companyRepository.findOne(companyVO.getId());
}
mapper.map(companyVO, company);
companyRepository.save(company);
return "redirect:/companies";
}
ฉันจะแสดงข้อผิดพลาดในการตรวจสอบความถูกต้องบนมุมมองได้อย่างไรเมื่อฉันใช้ VO