构造函数问题,找不到合适的构造函数

时间:2014-03-07 04:49:08

标签: java netbeans

在解决此问题时遇到一些麻烦。我一直在使用我之前完成的一个例子来帮助它,它几乎完全相同并且有效。 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", "bobdylan@gcu.ac.uk", "ISD");
        Student s2 = new Student("2", "Joni", "Mitchell", "jonimitchell@gcu.ac.uk", "ISD");
        Student s3 = new Student("3", "Paul", "Simon", "paulsimon@gcu.ac.uk", "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();
    }

1 个答案:

答案 0 :(得分:2)

问题:您正在调用Student构造函数,传入5个字符串。即:

 new Student("1", "Bob", "Dylan", "bobdylan@gcu.ac.uk", "ISD");

但是,如果检查Student类,你会发现它实际上没有这种构造函数,而是一个构造函数,它接受3个字符串,一个构造函数需要4个字符串和一个Program对象(不管这是什么),所以编译器是正确的抱怨。

解决方案:你可以给Student类一个有用的5 String构造函数(但是只有这对你的程序有意义,你需要决定),或者调用其中一个可用的构造函数,例如:带 四个 字符串和程序引用的那个。