解決此問題有一些麻煩。我一直在使用我之前完成的一個例子來進行協助,它幾乎完全相同並且可行。 addStudents方法中的構造函數不適合,我不確定我做錯了什麼。儘可能多地包含HibernateStudent文件和Student java文件。任何幫助將不勝感激,謝謝。構造函數問題,找不到合適的構造函數
public HibernateStudent3() {
Configuration configuration = h2Config(new Class[]{Student.class, Programme.class, Module.class});
sessionFactory = configuration.buildSessionFactory();
}
@SuppressWarnings({"unchecked"})
public void addStudents() {
System.out.println("Add Students");
Session session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("\n***Session started...");
System.out.println("\n***Creating student objects...");
Student s1 = new Student("1", "Bob", "Dylan", "[email protected]", "ISD");
Student s2 = new Student("2", "Joni", "Mitchell", "[email protected]", "ISD");
Student s3 = new Student("3", "Paul", "Simon", "[email protected]", "ISD");
System.out.println("\n***Student objects created...");
System.out.println("\n***Saving student objects...");
session.save(s1);
session.save(s2);
session.save(s3);
System.out.println("\n***Student objects saved...");
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("\n***Session closed...");
}
public void listStudents() {
System.out.println("List Students");
Session session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("\n***Session started...");
List studentList = session.createQuery("from Student").list();
System.out.println(studentList.size() + " student(s) found:");
for (Iterator iter = studentList.iterator(); iter.hasNext();) {
Student s = (Student) iter.next();
System.out.println(s);
}
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("\n***Session closed...");
}
public void addProgrammes() {
System.out.println("Add Programmes");
Session session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("\n***Session started...");
System.out.println("\n***Creating programme objects...");
Programme p1 = new Programme("P01627", "Information Systems Development", "FT", null);
Programme p2 = new Programme("P00280", "IT Management for Business", "FT", null);
System.out.println("\n***Programme objects created...");
System.out.println("\n***Module objects retrieved");
Module m1 = (Module) session.createQuery("from Module where moduleCode = 'M1'").uniqueResult();
Module m2 = (Module) session.createQuery("from Module where moduleCode = 'M2'").uniqueResult();
Module m3 = (Module) session.createQuery("from Module where moduleCode = 'M3'").uniqueResult();
Module m4 = (Module) session.createQuery("from Module where moduleCode = 'M4'").uniqueResult();
Module m5 = (Module) session.createQuery("from Module where moduleCode = 'M5'").uniqueResult();
Module m6 = (Module) session.createQuery("from Module where moduleCode = 'M6'").uniqueResult();
Module m7 = (Module) session.createQuery("from Module where moduleCode = 'M7'").uniqueResult();
Set<Module> p1Modules = new HashSet(Arrays.asList(m1, m2, m3, m4, m5, m6));
Set<Module> p2Modules = new HashSet(Arrays.asList(m1, m2, m3, m4, m5, m7));
System.out.println("\n***Attach modules to programmes");
p1.setModules(p1Modules);
p2.setModules(p2Modules);
System.out.println("\n***Saving programme objects...");
session.save(p1);
session.save(p2);
System.out.println("\n***Programme objects saved...");
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("\n***Session closed...");
}
public void listProgrammes() {
System.out.println("List Programmes");
Session session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("\n***Session started...");
List programmeList = session.createQuery("from Programme").list();
System.out.println(programmeList.size() + " programme(s) found:");
for (Iterator iter = programmeList.iterator(); iter.hasNext();) {
Programme p = (Programme) iter.next();
System.out.println(p);
}
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("\n***Session closed...");
}
public void addModules() {
System.out.println("Add Modules");
Session session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("\n***Session started...");
System.out.println("\n***Creating module objects...");
Module m1 = new Module("M1", "IT Project Management 2");
Module m2 = new Module("M2", "Database Systems Development 2");
Module m3 = new Module("M3", "Research Methods");
Module m4 = new Module("M4", "Professional Issues");
Module m5 = new Module("M5", "Honours Project");
Module m6 = new Module("M6", "Web Programming 2");
Module m7 = new Module("M7", "International Business Strategy");
System.out.println("\n***Module objects created...");
System.out.println("\n***Saving module objects...");
session.save(m1);
session.save(m2);
session.save(m3);
session.save(m4);
session.save(m5);
session.save(m6);
session.save(m7);
System.out.println("\n***Module objects saved...");
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("\n***Session closed...");
}
public void listModules() {
System.out.println("List Modules");
Session session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("\n***Session started...");
List moduleList = session.createQuery("from Module").list();
System.out.println(moduleList.size() + " module (s) found:");
for (Iterator iter = moduleList.iterator(); iter.hasNext();) {
Module m = (Module) iter.next();
System.out.println(m);
}
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("\n***Session closed...");
}
public void studentAdmin() {
// Add Modules
addModules();
// List Modules
listModules();
// Add Programmes
addProgrammes();
// List Programmes
listProgrammes();
// Add Students
addStudents();
// List Students
//listStudents();
}
學生類。
public class Student{
private long studentId;
private String matricNo;
private String firstName;
private String surname;
private String email;
private Programme programme;
public Student() {
this.matricNo = null;
this.firstName = null;
this.surname = null;
this.email = null;
this.programme = null;
}
public Student(String matric, String fn, String ln) {
this.matricNo = matric;
this.firstName = fn;
this.surname = ln;
this.email = null;
this.programme = null;
}
public Student (String matric, String fn, String ln, String e, Programme prg) {
this.matricNo = matric;
this.firstName = fn;
this.surname = ln;
this.email = e;
this.programme = prg;
}
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long sid) {
this.studentId = sid;
}
public String getMatricNo() {
return this.matricNo;
}
public void setMatricNo(String matric) {
this.matricNo = matric;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String fn) {
this.firstName = fn;
}
public String getSurname() {
return this.surname;
}
public void setSurname(String ln) {
this.surname = ln;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Programme getProgramme() {
return this.programme;
}
public void setProgramme(Programme prog) {
this.programme = prog;
}
@Override
public int hashCode() {
return getMatricNo().hashCode() * 31 + getSurname().hashCode() * 31 + getFirstName().hashCode() * 31 + getEmail().hashCode() * 31 + getProgramme().hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof Student) {
Student s = (Student)o;
return s.getMatricNo() == getMatricNo() &&
s.getFirstName().equals(getFirstName()) &&
s.getSurname().equals(getSurname()) &&
s.getEmail().equals(getEmail()) &&
s.getProgramme().equals(getProgramme());
} else {
return false;
}
}
@Override
public String toString() {
return "student id: " + getStudentId() + ", " +
"matric number: " + getMatricNo() + ", " +
"first name: " + getFirstName() + ", " +
"surname: " + getSurname() + ", " +
"email: " + getEmail() + ", " +
"programme: " + getProgramme();
}
你得到什麼錯誤,在哪裏? –