1

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 &nbsp;&nbsp;&nbsp;&nbsp;<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

Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
GauravP
  • 81
  • 3
  • 14
  • remove commented code employee.setEmpNo and make private Long empNo; may be this will resolve problem – Dev Apr 20 '15 at 11:46
  • visit http://stackoverflow.com/questions/4102449/how-to-annotate-mysql-autoincrement-field-with-jpa-annotations and http://stackoverflow.com/questions/20603638/what-is-the-use-of-annotations-id-and-generatedvaluestrategy-generationtype for more information – Dev Apr 20 '15 at 11:48
  • On DB side emp_no field is int(11) so does Long work?....Also there are two commented lines which line you are asking me to uncomment? – GauravP Apr 20 '15 at 13:14
  • i was asking to undo commenting this line employee.setEmpNo method in prepareModel – Dev Apr 20 '15 at 13:16
  • Still stuck with the above error any more suggestions on this? – GauravP Apr 27 '15 at 12:03

1 Answers1

0
  1. Keep the employee.setEmpNo statement as commented itself, since you are specifying the Generator here.

  2. 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.

Mirsa
  • 26
  • 4
  • Hi Misra will check and let you know – GauravP Apr 21 '15 at 06:52
  • I removed the empNo initialization from model and bean but still the same error – GauravP Apr 22 '15 at 06:03
  • Null value is going from hibernate model to MySql. The error 'No value specified for parameter' means MySql is expecting some value set to emp_no. So MySQL is not auto incrementing emp_no MySQL expect this parameter to be not null. – GauravP Apr 22 '15 at 08:10
  • Do we need to check on MySQL table definition? The issue may be on MySQL side? – GauravP Apr 22 '15 at 08:11