Hibernate 最简单例子 (12)

       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.

 

存在问题和解决

问题1hibernate.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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjzyw.html