Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting error: Caused by: java.sql.SQLException: Parameter index out of range
    text
    copied!<p>Im creating a hibernate program which creates a database of employees. When i run my driver program, i get the error "Caused by: java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4)". My driver program calls DepartmentEmps.load(), the whole class is pasted below. Two other tables are able to be loaded just fine, but this one is and another similar class (departmentManagers) are having problems with parameter index out of range errors.</p> <pre><code>package DAO; import java.io.Serializable; import java.sql.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Id; import org.hibernate.Transaction; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; @Entity @Table(name="dept_emp") public class DepartmentEmp implements Serializable { private long emp_no; private String dept_no; private Date from_date; private Date to_date; private Employee employee; private Department department; public DepartmentEmp(){} public DepartmentEmp(long emp_no, String dept_no, Date from_date, Date to_date) { this.emp_no = emp_no; this.dept_no = dept_no; this.from_date = from_date; this.to_date = to_date; } //***********************GETTERS AND SETTERS***************************************** @Id @Column(name="emp_no") public long getEmp_no() {return emp_no;} public void setEmp_no(long emp_no) {this.emp_no = emp_no;} @Id @Column(name="dept_no") public String getDept_no() {return dept_no;} public void setDept_no(String dept_no) {this.dept_no = dept_no;} @Column(name="from_date") public Date getFrom_date() {return from_date;} public void setFrom_date(Date from_date) {this.from_date = from_date;} @Column(name="to_date") public Date getTo_date() {return to_date;} public void setTo_date(Date to_date) {this.to_date = to_date;} @ManyToOne @JoinColumn(name="emp_no") public Employee getEmployee() {return employee;} public void setEmployee(Employee employee) {this.employee = employee;} @ManyToOne @JoinColumn(name="dept_no") public Department getDepartment() {return department;} public void setDepartment(Department department) {this.department = department;} //**************************METHODS******************************** public void print(){ System.out.printf("%d, %s: from %s to %s\n", emp_no, dept_no, from_date.toString(), to_date.toString()); } //*****************STATIC METHODS*************************************** public static void load(){ Session session = HibernateContext.getSession(); Employee kyle = Employee.find("Kyle", "Wong"); Employee vlad = Employee.find("Vlad", "Iacob"); Employee sameer = Employee.find("Sameer", "Gupta"); Employee ron = Employee.find("Ronald", "Mak"); Employee john = Employee.find("John", "Pierce"); DepartmentEmp k = new DepartmentEmp(1, "d0001", kyle.getHire_date(), new Date(113, 2, 21)); k.setEmployee(kyle); k.setDepartment(Department.findByName(k.getDept_no())); DepartmentEmp v = new DepartmentEmp(2, "d0001", vlad.getHire_date(), new Date(113, 2, 21)); v.setEmployee(vlad); v.setDepartment(Department.findByName(v.getDept_no())); DepartmentEmp s = new DepartmentEmp(3, "d0001", sameer.getHire_date(), new Date(113, 2, 21)); s.setEmployee(sameer); s.setDepartment(Department.findByName(s.getDept_no())); DepartmentEmp r = new DepartmentEmp(4, "d0001", ron.getHire_date(), new Date(105, 6, 5)); r.setEmployee(ron); r.setDepartment(Department.findByName(r.getDept_no())); DepartmentEmp j = new DepartmentEmp(5, "d0001", john.getHire_date(), new Date(2000, 9, 30)); j.setEmployee(john); j.setDepartment(Department.findByName(j.getDept_no())); Transaction tx = session.beginTransaction();{ session.save(k); session.save(v); session.save(s); session.save(r); session.save(j); } tx.commit(); session.close(); System.out.println("Department employees has been loaded"); } } </code></pre> <p>Stack Trace:</p> <pre><code>Hibernate: insert into dept_emp (dept_no, emp_no, from_date, to_date) values (?, ?, ?, ?) Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [DAO.DepartmentEmp] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2455) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2875) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133) at DAO.DepartmentEmp.load(DepartmentEmp.java:111) at Drivers.CreateDatabase.main(CreateDatabase.java:13) Caused by: java.sql.SQLException: Parameter index out of range (5 &gt; number of parameters, which is 4). </code></pre> <p>How do i find out what the 5th parameter is that the program is trying to insert into the table?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload