我无法从表单向表中插入数据。
所以,我有两个与ManyToOne关系的表。
TORDER和TSYSTEM。
我想通过.jsp页面中的表单将数据插入TORDER表。一个字段的数据来自TSYSTEM表-系统名称。
我能够使用上述表格中的数据填充下拉列表,但是在提交表单并尝试将数据插入TORDER表时出现错误消息。
您知道为什么这里可能出错吗?
我的订单模型类:
@Entity
@Table(name="TORDER")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ORDER_ID")
private int orderId;
@ManyToOne(cascade = CascadeType.ALL, targetEntity=System.class)
@JoinColumn(name = "SYSTEM_ID", referencedColumnName = "SYSTEM_ID")
private int sysId;
@ManyToOne(cascade = CascadeType.ALL, targetEntity=System.class)
@JoinColumn(name = "SYSTEM_NAME", referencedColumnName = "SYSTEM_NAME")
private String systemName;
@Column(name="ORDER_DATE")
private String orderDate;
@Column(name="STATUS")
private String status;
@Column(name="COMMENT")
private String comment;
@Column(name="APPROVED_BY_MANAGER")
private String managerApproval;
@Column(name="APPROVED_BY_ADMIN")
private String adminApproval;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getSystemId() {
return sysId;
}
public void setSystemId(int sysId) {
this.sysId = sysId;
}
public String getSystemName() {
return systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
public String getOrderDate() {
return orderDate;
}
public void setOrderDate(String orderDate) {
this.orderDate = orderDate;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getManagerApproval() {
return managerApproval;
}
public void setManagerApproval(String managerApproval) {
this.managerApproval = managerApproval;
}
public String getAdminApproval() {
return adminApproval;
}
public void setAdminApproval(String adminApproval) {
this.adminApproval = adminApproval;
}
}
系统型号:
@Entity
@Table(name="TSYSTEM")
public class System implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="SYSTEM_ID")
private int sysId;
@Column(name="SYSTEM_NAME")
private String systemName;
@Column(name="DESCRIPTION")
private String description;
@Column(name="GROUP_NAME")
private String groupName;
public int getSysId() {
return sysId;
}
public void setSysId(int sysId) {
this.sysId = sysId;
}
public String getSystemName() {
return systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
控制器方法:
@RequestMapping(value="/addOrder", method=RequestMethod.GET)
public ModelAndView addOrder() {
ModelAndView model = new ModelAndView();
Order order = new Order();
model.addObject("orderForm", order);
List<System> systemList = systemService.getAllSystemsName();
model.addObject("systemList", systemList);
model.setViewName("order_form");
return model;
}
@RequestMapping(value="/saveOrder", method=RequestMethod.POST)
public ModelAndView save(@ModelAttribute("orderForm") Order order) {
orderService.saveOrUpdate(order);
return new ModelAndView("redirect:/employee/orderList");
}
存储库方法,以从TSYSTEM表获取所有系统名称:
public interface SystemRepository extends CrudRepository<System, Integer> {
@Query("select a.systemName from System a")
public List<System> getAllSystemsName();
}
最后是我的 .jsp表单:
<form:form modelAttribute="orderForm" method="post" action="${saveURL }" cssClass="form" >
<form:hidden path="orderId"/>
<table>
<tr>
<td>System Name</td>
<td><form:select path="systemName">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${systemList}" />
</form:select>
</td>
<td><form:errors path="systemName" cssClass="error" /></td>
</tr>
<tr>
<td><label>Komentarz</label></td>
<td><form:textarea path="comment" cssClass="form-control" id="comment"
title="Dodaj komentarz do zamówienia" /></td>
</tr>
<tr>
<td><label>Data zamówienia</label></td>
<td><form:input type="text" path="orderDate" cssClass="form-control"
id="orderDate" pattern="{2,}" title="Data zamówienia" required="required"/>
</td>
</tr>
<tr>
<td><button type="reset" class="btn btn-primary">Wyczyść dane</button>
</td>
<td><button type="submit" class="btn btn-primary">Zamów dostęp</button>
</td>
</tr>
</table>
</form:form>
在表单提交操作之后,我收到了错误消息:
2018-11-13 17:32:32.846 ERROR 7416 --- [nio-8090-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Error accessing field [private int com.project.model.System.sysId] by reflection for persistent property [com.project.model.System#sysId] : 0; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int com.project.model.System.sysId] by reflection for persistent property [com.project.model.System#sysId] : 0] with root cause
java.lang.IllegalArgumentException: Can not set int field com.project.model.System.sysId to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_161]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_161]
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) ~[na:1.8.0_161]
at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56) ~[na:1.8.0_161]
at java.lang.reflect.Field.getInt(Field.java:574) ~[na:1.8.0_161]
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:62) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
答案 0 :(得分:0)
请发布SYSTEM模型。我认为您需要将字段sysId的系统模型更改为Integer类型。因为您使用模型sysId设置存储库是Integer。