0
我在驗證我的表單時遇到了這個問題,對於單個實體,它工作正常。但是當我嘗試使用嵌套對象驗證對象時,出現此錯誤。從屬對象上的彈簧窗體驗證
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>