gpt4 book ai didi

java - 在我的 Spring Hibernate 项目上获取 InvokingTargetException

转载 作者:行者123 更新时间:2023-11-29 19:29:57 25 4
gpt4 key购买 nike

Here is the table of data

我已经使用 mySQL 中的数据库创建了一个简单的 hibernate/spring 和 CRUD。除了我的编辑功能之外,一切正常。它说这个错误。

root cause

org.springframework.beans.InvalidPropertyException: Invalid property 'id' of bean class [org.jjm.test.model.Phone_$$_jvst33c_0]: Getter for property 'id' threw exception; nested exception is java.lang.reflect.InvocationTargetException
org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:704)
org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:610)
org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229)
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
org.springframework.web.servlet.tags.form.HiddenInputTag.writeTagContent(HiddenInputTag.java:79)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.WEB_002dINF.views.phoneDetails_jsp._jspx_meth_form_005fhidden_005f0(phoneDetails_jsp.java:322)
org.apache.jsp.WEB_002dINF.views.phoneDetails_jsp._jspx_meth_form_005fform_005f0(phoneDetails_jsp.java:231)
org.apache.jsp.WEB_002dINF.views.phoneDetails_jsp._jspService(phoneDetails_jsp.java:175)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

它说错误与我的 ID 相关,但我在代码中看不到错误。

package org.jjm.test.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/*
* This is the model class and it corresponds to Phone table in the database
*/
@Entity
@Table(name="PHONE")
public class Phone {

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;

@Column(name="phoneName")
String phoneName;

@Column(name="make")
String make;

@Column(name="price")
double price;

@Column(name="storage")
String storage;

@Column(name="camera")
String camera;


public Phone() {
super();
}


public Phone(int i, String phoneName, String make, double price, String storage, String camera) {
super();
this.id = i;
this.phoneName = phoneName;
this.make = make;
this.price = price;
this.storage = storage;
this.camera = camera;
}

public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getPhoneName() {
return phoneName;
}


public void setPhoneName(String phoneName) {
this.phoneName = phoneName;
}


public String getMake() {
return make;
}


public void setMake(String make) {
this.make = make;
}


public double getPrice() {
return price;
}


public void setPrice(double price) {
this.price = price;
}


public String getStorage() {
return storage;
}


public void setStorage(String storage) {
this.storage = storage;
}


public String getCamera() {
return camera;
}


public void setCamera(String camera) {
this.camera = camera;
}

}

这就是我显示数据的地方

<table>  
<tr>
<th colspan="2">Add Phone</th>
</tr>
<tr>
<form:hidden path="id" />
<td><form:label path="phoneName">Phone Name:</form:label></td>
<td><form:input path="phoneName" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td><form:label path="make">Make:</form:label></td>
<td><form:input path="make" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td><form:label path="price">Price:</form:label></td>
<td><form:input path="price" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td><form:label path="storage">Storage:</form:label></td>
<td><form:input path="storage" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td><form:label path="camera">Camera:</form:label></td>
<td><form:input path="camera" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
class="blue-button" /></td>
</tr>
</table>

最佳答案

实体ID必须可为空,将其更改为

 @Id  
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer id;

这是为什么呢?在插入新实体的过程中,ID 必须为 null,以便让数据库决定它的实际值。如果您说 0 甚至 162,它可能已经在数据库中使用。

关于java - 在我的 Spring Hibernate 项目上获取 InvokingTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com