- 1、加入junit.jar
2、新建一个工具类HibernateUtils.Java把一些对session的公共操作封装起来
- public class HibernateUtils {
- /*
- *读取Hibernate.cfg.xml文件
- */
- private static SessionFactory factory;
- static {
- try {
- //读取hibernate.cfg.xml文件
- Configuration cfg = new Configuration().configure();
- //建立SessionFactory
- factory = cfg.buildSessionFactory();
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
- /*
- *打开Session
- */
- public static Session getSession() {
- return factory.openSession();
- }
- /*
- *关闭Session
- */
- public static void closeSession(Session session) {
- if (session != null) {
- if (session.isOpen()) {
- session.close();
- }
- }
- }
- public static SessionFactory getSessionFactory() {
- return factory;
- }
- }
- public class test extends TestCase {
- /*
- * 增加User
- */
- public void testSave() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- User user = new User();
- user.setName("李四");
- user.setPassword("123");
- user.setCreateTime(new Date());
- user.setExpireTime(new Date());
- session.save(user);
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- /**
- * 删除
- */
- public void testDel(){
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- User user=(User)session.load(User.class, "4028989545a244640145a24466300001");
- session.delete(user);
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- /**
- * 修改
- */
- public void testUpdate(){
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001");
- user.setName("王五");
- session.update(user);
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- /**
- * 查询
- */
- public void testLoad(){
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001");
- System.out.println(user.getName());
- System.out.println(user.getId());
- System.out.println(user.getPassword());
- System.out.println(user.getClass());
- System.out.println(user.getCreateTime());
- System.out.println(user.getExpireTime());
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
- load, get区别:
load返回的代理对象, 真正用到对象时才发出sql语句,
get直接从数据库加载,发出sql语句, 不会延迟.
所以load必须在commit之前执行getName方法的操作.
但是get放在commit之前之后都可以.
@Test
public
void
testLoad() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher)session.load(Teacher.
class
,
1
);
//此数字是ID
System.out.println(t.getName());
session.getTransaction().commit();
//System.out.println(t.getClass());
}
@Test
public
void
testGet() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher)session.get(Teacher.
class
,
1
);
session.getTransaction().commit();
//System.out.println(t.getClass());
System.out.println(t.getName());
}