我在验证表单时遇到了这个问题,对于单个实体,它运行正常。但是当我尝试使用嵌套对象验证对象时,我得到了这个错误。
HTTP Status 500 - Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.poultry.jp.model.PaymentRequest] during persist time for groups [javax.validation.groups.Default, ]
以下是我的控制器
@RequestMapping(value = {"user/addMedicineOrder","admin/addMedicineOrder"}, method = RequestMethod.GET)
public String addMedicineOrderGET(@ModelAttribute("medicineOrder") MedicineOrder medicineOrder,Model model) {
List<SupplierReport> supplierReport = supplierService.getSupplierNames();
List<MedicineOrderReport> medicineOrderReport = medicineOrderService.getMedicineOrderDetails();
List<PaymentRequestReport> paymentRequestReport = paymentRequestSerivice.getPaymentRequestDates();
List<MedicineReport> medicineReport = medicineService.getMedicineDetails();
model.addAttribute("medicineOrderReport",medicineOrderReport);
model.addAttribute("supplierReport", supplierReport);
model.addAttribute("paymentRequestReport", paymentRequestReport);
model.addAttribute("medicineReport", medicineReport);
return "addMedicineOrder";
}
@RequestMapping(value = {"user/addMedicineOrder","admin/addMedicineOrder"}, method = RequestMethod.POST)
public String addMedicineOrderPOST(@Valid @ModelAttribute("medicineOrder") MedicineOrder medicineOrder,BindingResult result) {
if (result.hasErrors()) {
return "addMedicineOrder";
} else {
medicineOrderService.save(medicineOrder);
return "redirect:addMedicineOrder.html";
}
}
以下是我的医学订单模型
public class MedicineOrder {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long medicineOrderId;
@OneToMany(mappedBy = "medicineOrder", cascade = CascadeType.ALL,fetch = FetchType.EAGER,orphanRemoval=true)
private List<MedicineOrderDetails> medicineOrderDetails = new ArrayList<MedicineOrderDetails>();
@ManyToOne
private Supplier supplier;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Temporal(TemporalType.DATE)
private Date date;
private double total;
private double discount;
private boolean inStock;
@OneToOne(cascade={CascadeType.MERGE , CascadeType.PERSIST})
@JoinColumn(name="paymentRequest_id")
private PaymentRequest paymentRequest;
private boolean isDeleted;
private Date created;
private Date updated;
// Getters and Setters
}
以下是依赖类付款请求 公共类PaymentRequest {
public static final String FIND_PAYMENT_REQUEST_DATES = "findPaymentReequestDates";
public static final String FIND_PAYMENT_REQUEST_BY_ID = "findPaymentReequestById";
public static final String FIND_PAYMENT_REQUEST_REPORT = "findPaymentRequestReport";
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long paymentId;
private double amount;
@Type(type = "text")
@NotEmpty
private String description;
private String chequeID;
private String status;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Temporal(TemporalType.DATE)
private Date date;
private boolean isDeleted;
private boolean isPaid;
private Date created;
private Date updated;
//Getters and Setters
}
以下是我坚持医药订单的地方,它坚持付款请求。
@Repository("MedicineOrderRepository")
公共类MedicineOrderRepositoryImp实现了MedicineOrderRepository {
@PersistenceContext
private EntityManager entityManager;
public MedicineOrder save(MedicineOrder medicineOrder){
for(MedicineOrderDetails order : medicineOrder.getMedicineOrderDetails())
{
order.setMedicineOrder(medicineOrder);
}
medicineOrder.getPaymentRequest().setStatus("Pending");
entityManager.persist(medicineOrder);
entityManager.flush();
return medicineOrder;
}
}
最后关注jsp代码。
<div class=".col-md-6">
<form:form commandName="medicineOrder" class="form-horizontal"
autocomplete="off">
<form:errors path="*" Class="alert alert-danger" element="div" />
<div class="form-group">
<label for="inputDescription" class="col-sm-2 control-label">Description</label>
<div class="col-sm-3">
<form:textarea path="paymentRequest.description" type="text"
class="form-control" id="inputDescription"
placeholder="Enter Short Description" rows="3" />
</div>
</div>
<div class="form-group">
<label for="inputAmount" class="col-sm-2 control-label">Amount</label>
<div class="col-sm-3">
<form:input path="paymentRequest.amount" type="text"
class="form-control" id="inputAmount" placeholder="Enter Amount" />
</div>
</div>
<div class="form-group">
<label for="supplierId" class="col-sm-2 control-label">Supplier</label>
<div class="col-sm-5">
<form:select path="supplier.supplierId" id="supplierId"
class="form-control">
<form:options items="${supplierReport}" itemLabel="name"
itemValue="supplierId" id="" />
</form:select>
</div>
</div>
<div class="form-group">
<label for="inputTotal" class="col-sm-2 control-label">Total</label>
<div class="col-sm-5">
<form:input path="total" type="text" class="form-control"
id="inputTotal" name="inputTotal" />
</div>
</div>
<div class="form-group">
<label for="inputDiscount" class="col-sm-2 control-label">Discount
</label>
<div class="col-sm-5">
<form:input path="discount" type="text" class="form-control"
id="inputDiscount" />
</div>
</div>
<input type="submit" value="Save Medicine Order"
class="btn btn-primary">
</form:form>
</div>
答案 0 :(得分:-1)
我能够解决这个问题。 感谢您访问此论坛http://forum.spring.io/forum/spring-projects/web/87783-jsr-303-validation-on-child-objects
我要做的只是在我父类的子对象中添加@Valid注释 EX:
@Valid
private PaymentRequest paymentRequest;