我正在创建一个hibernate程序,它创建了一个员工数据库。当我运行我的驱动程序时,我得到错误“引起:java.sql.SQLException:参数索引超出范围(5>参数数量,这是4)”。我的驱动程序调用DepartmentEmps.load(),整个类都粘贴在下面。另外两个表可以正常加载,但是这个表和另一个类似的类(departmentManagers)在参数索引超出范围错误时遇到问题。
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");
}
}
堆栈追踪:
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 > number of parameters, which is 4).
我如何找出程序试图插入表中的第五个参数是什么?