I am getting following error while using Spring MVC with hibernate Annotations. I am trying to insert the record in the database using AUTO_INCREMENT
on the mysql database table Employee. On the model class I have the entry as below -
@Entity
@Table(name="EMPLOYEES")
public class Employee implements Serializable{
private static final long serialVersionUID = -1798070786993154676L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="emp_no")
private int empNo=0;
@Column(name="birth_date")
private Date birthDate;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="gender")
@Enumerated(EnumType.STRING)
private Gender gender=Gender.M;
@Column(name="hire_date")
private Date hireDate;
public Employee() {
// TODO Auto-generated constructor stub
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public String toString(){
return "EmpNo ::::"+this.empNo+"Birth Date ::::"+this.birthDate
+"First Name ::::"+this.firstName+" Last Name::::"+
this.lastName+" Hire Date ::::"+this.hireDate+" Gender::::"+this.gender;
}
}
On Database side I have defined the primary type column as below :
Field Type Null Key Extra
emp_no int(11) NO PRI auto_increment
birth_date date YES
first_name varchar(14) YES
last_name varchar(16) YES
GENDER enum('M','F') YES
hire_date date YES
addEmployee method as follows :
@RequestMapping(value="/save",method=RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("command")EmployeeBean employeeBean,BindingResult bindingResult){
Employee employee=prepareModel(employeeBean);
employeeService.addEmployee(employee);
Map<String, Object> model=new HashMap<String,Object>();
model.put("employees", prepareListOfEmployeeBeans(employeeService.listEmployees()));
model.put("genders", employeeBean.getGender().values());
return new ModelAndView("redirect:/add.html");
}
public Employee prepareModel(EmployeeBean employeeBean)
{
if(employeeBean!=null){
Employee employee=new Employee();
employee.setFirstName(employeeBean.getFirstName());
employee.setLastName(employeeBean.getLastName());
employee.setGender(employeeBean.getGender());
employee.setHireDate(employeeBean.getHireDate());
// employee.setEmpNo(employeeBean.getEmpNo());
employee.setBirthDate(employeeBean.getBirthDate());
// employeeBean.setEmpNo(null);
return employee;
}
return null;
}
AddEmployee JSP :
<%@page import="org.springframework.web.servlet.ModelAndView"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee Form</title>
</head>
<body>
<form:form modelAttribute="command" action="/MVC_CRUD_SPRING/save.html" method="post">
<table>
<tr>
<td colspan="2" align="center"><b>Add Employee Form</b></td>
</tr>
<tr>
<td><form:label path="empNo">Employee Number</form:label></td>
<td><form:input path="empNo" value="${employee.empNo}" readonly="true"/> </td>
</tr>
<tr>
<td><form:label path="birthDate">Birth Date</form:label> </td>
<td><form:input path="birthDate" value="${employee.birthDate}"/> </td>
</tr>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" value="${employee.firstName}"/></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label> </td>
<td><form:input path="lastName" value="${employee.lastName}"/> </td>
</tr>
<tr>
<td><form:label path="Gender">Gender</form:label></td>
<%-- <td><form:radiobutton path="gender" value="M"/>Male <form:radiobutton path="gender" value="F"/>Female</td> - -%>
<td><form:radiobuttons path="gender" items="${genders}"/></td>
</tr>
<tr>
<td><form:label path="hireDate">Hire Date</form:label></td>
<td><form:input path="hireDate" value="${employee.hireDate}"/> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
The error stack trace :
Hibernate: insert into EMPLOYEES (birth_date, first_name, last_name, gender, hire_date, emp_no) values (?, ?, ?, ?, ?, ?)
Apr 20, 2015 3:22:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [employee] in context with path [/MVC_CRUD_SPRING] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.cybage.projects.model.Employee]] with root cause
java.sql.SQLException: No value specified for parameter 6
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2578)
In the above code snippet while creating model of Employee I have commented out employee.setEmpNo
method in prepareModel
. What I think is as Employees table emp_no field has auto-increment I don't need to explicitly set empNo
value, the database should take care of this. If I am uncommenting this , then also it is not working.
Any body who can help me understanding where
Keep the employee.setEmpNo statement as commented itself, since you are specifying the Generator here.
There is no need to initialize the empNo with zero value. Have a look on this .
The above link clearly depicts the steps to be followed, when using GenerationType.IDENTITY.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.