我试图通过spring将数据保存到表中,但是这个错误会在数据提交时显示..
org.springframework.beans.NotReadablePropertyException:bean类的无效属性'user'[com.jit.model.Signup]:Bean属性'user'不可读或getter方法无效:是否返回getter的类型与setter的参数类型匹配?
控制器类
@Controller
public class DatabaseController
{
@RequestMapping("/signup.htm")
public String loginCheck(@ModelAttribute("bean") @Valid Signup bean,BindingResult result,HttpServletRequest request , HttpServletResponse response) throws IOException {
Session session= HiberSession.getHiber();
if (result.hasErrors()){
return "signup";
} else{
session.save(bean);
return "abc";
}
Bean类
@Entity
@Table(name="user")
public class Signup {
@Id
@GeneratedValue
@Column(name="uid")
private Integer uid;
@NotEmpty
@Column(name="name")
private String name;
@NotEmpty
@Column(name="father_name")
private String father;
@NotEmpty
@Size(min =4,max =10)
@Column(name="password")
private String pass;
@NotEmpty
@Length(min =10,max =10)
@Column(name="contact")
private String contact;
@NotEmpty
@Column(name="city")
private String city;
@NotNull
@Column(name="introducer")
private Integer introducer;
@Column(name="status")
private Integer status;
@Column(name="amount")
private Integer amount=400;
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFather() {
return father;
}
public void setFather(String father) {
this.father = father;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getIntroducer() {
return introducer;
}
public void setIntroducer(Integer introducer) {
this.introducer = introducer;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
signup.jsp
<%@page language="java" contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<style>
.error {
color: #ff0000;
}
</style>
</head>
<body>
<c:if test="${not empty message}"><h2>${message}</h2></c:if>
<h3>New user registration form..</h3>
<form:form method="POST" commandName='bean' action="/jit/signup.htm">
<table>
<tr>
<td>Name :</td>
<td><form:input path="name"/></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td>Father Name :</td>
<td><form:input path="father"/></td>
<td><form:errors path="father" cssClass="error"/></td>
</tr>
<tr>
<td>Password :</td>
<td><form:password path="pass" /></td>
<td><form:errors path="pass" cssClass="error" /></td>
</tr>
<tr>
<td>Contact Number :</td>
<td><form:input path="contact"/></td>
<td><form:errors path="contact" cssClass="error"/></td>
</tr>
<tr>
<td>City/Village :</td>
<td><form:input path="city"/></td>
<td><form:errors path="city" cssClass="error"/></td>
</tr>
<tr>
<tr>
<td>Introducer ID:</td>
<td><form:input path="introducer"/></td>
<td><form:errors path="introducer" cssClass="error"/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
答案 0 :(得分:4)
我要说你应该避免使用hibernate来保留保留字的表名。当然你可以逃脱它,但它可能在将来引起问题(例如在查询中)。所以最安全的方法是用另一种方式命名表格 - 比如users
@Entity
@Table(name="users")
public class Signup {
}