UserDAO dao = new UserDAO();
// Start the transaction
Transaction tx = dao.getSession().beginTransaction();
// Add user
dao.save(user);
// Commit the transaction
tx.commit();
// Close the session
dao.getSession().close();
}
privatestaticvoid listUser() {
UserDAO dao = new UserDAO();
User user = dao.findById(1);
printUser("Pringting User, ", user);
dao.getSession().close();
}
privatestaticvoid changeUser() {
UserDAO dao = new UserDAO();
User user = dao.findById(1);
user.setUsername("test");
user.setPassword("test");
user.setFirstName("demo");
user.setLastName("demo");
Transaction tx = dao.getSession().beginTransaction();
// Add user
dao.save(user);
// Commit the transaction
tx.commit();
User updateUser = dao.findById(1);
printUser("Pringting Updated User, ", updateUser);
dao.getSession().close();
}
}
三运行结果如下:
Pringting User, User[Username: guoqiang, Password:guoqiang,First Name: guo,Last Name: qiang]
Pringting Updated User, User[Username: test, Password:test,First Name: demo,Last Name: demo]
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
四存在问题和解决
问题1:hibernate.cfg.xml文件找不到如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.HibernateException: /hibernate.cfg.xml not found