简体   繁体   中英

generic dao in java with hibernate

I wite this two mothod for save students . now i want use this . this is my methods:

  @Override
public void save(T entity) {
    Session session = getSession();
    Transaction transaction = session.beginTransaction();
    session.saveOrUpdate(entity);
    transaction.commit();
    session.close();
}

@Override
public void save(Set<T> entity) {
    Session session = getSession();
    Transaction transaction = session.beginTransaction();
    for (T t : entity) {
        session.saveOrUpdate(t);
    }
    transaction.commit();
    session.close();
}

for use them ex for add a student i write a instance for it :

    static IStudentDao stDao = new StudentDao();
    and wite this :

  stdId = Console.getInputInteger("enter your code");
                    int stage = Console.getInputInteger("enter your    age");
                    String stname = Console.getInputString("enter your name:");
                    String stlname = Console.getInputString("enter your lastname:");
                    String stfname = Console.getInputString("enter your fathername:");
                    String stmajor = Console.getInputString("enter your major:");

                    Student student = new Student();
                    student.setFirstName(stname);
                    student.setLastName(stlname);
                    student.setAge(stage);
                    student.setFatherName(stfname);
                    student.setMajor(stmajor);
                    student.setStudentcode(stdId);

                    IStudentDao.save();
                    repository.showStudents();

but it fell into error . i don't know what should i send to it :

     IStudentDao.save();

Your IStudentDao::save is not static hence you cannot call it like IStudentDao.save() .

You must call method on instance of IStudentDao which is your stDao :

`stDao.save()`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM