论坛首页 入门技术论坛

hibernate:测试实体对象的生命周期

浏览 2124 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-11   最后修改:2009-03-10
测试实体对象的生命周期

pojo:
package com.lhtwqh.hibernate;

import java.util.Date;

public class User {
	private String id;
	private String name;
	private String password;
	private Date createTime;
	private Date expireTime;
	
	public Useffffffffffffffffffffffffffffffffffffffffffr(){
		
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getExpireTime() {
		return expireTime;
	}
	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}

}



session工厂:HibernateUtils
package com.lhtwqh.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
	private static SessionFactory sessionFactory;

	static {
		try {
			Configuration config = new Configuration().configure();
			sessionFactory = config.buildSessionFactory();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static Session getSession() {
		return sessionFactory.openSession();
	}

	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.close();
			}
		}
	}

}


junit简介:
* 编写测试类xxxTest,需要继承TestCase
* 编写单元测试方法,测试方法必须以test开头,测试方法不能含有参数和返回值,如:
  public void testHello1() {}
* 最好单元测试的代码单独建立一个目录
package com.lhtwqh.hibernate;



import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

import junit.framework.TestCase;

public class SessionTest extends TestCase {

	public void testHello1() {
		System.out.println("-----------SessionTest.Hello1()------------");
		//throw new java.lang.RuntimeException();
	}
	
	public void testHello2() {
		System.out.println("-----------SessionTest.testHello2()------------");
		
		//this.assertEquals("hello", "hello111");
	}
	
	public void testSave1() {
		Session session = null;
		Transaction tx = null;
		User user = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			
			//Transient状态
			user = new User();
			user.setName("李四");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			
			//persistent状态,当属性发生改变的时候,hibernate会自动和数据库同步
			session.save(user);
			
			user.setName("王五");
			//session.update(user);
			tx.commit();
		}catch(Exception e) {
			e.printStackTrace();
			tx.rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		//detached状态
		user.setName("张三");
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
	 		//persistent状态
			session.update(user);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}		
	}
	
	public void testReadByGetMethod1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//马上发出查询sql,加载User对象
			User user = (User)session.get(User.class, "402880d01b9bf210011b9bf2a2ff0001");
			System.out.println("user.name=" + user.getName());
			
			//persistent状态,当属性发生改变的时候,hibernate会自动和数据库同步
			user.setName("龙哥");
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testReadByGetMethod2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//采用get加载数据,如果数据库中不存在相应的数据,返回null
			User user = (User)session.get(User.class, "asdfsafsdfdsf");

			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testReadByLoadMethod1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//不会发出查询sql,因为load方法实现了lazy(懒加载或延迟加载)
			//延迟加载:只有真正使用这个对象的时候,才加载(发出sql语句)
			//hibernate延迟加载实现原理是代理方式
			User user = (User)session.load(User.class, "402880d01b9bf210011b9bf2a2ff0001");
			System.out.println("user.name=" + user.getName());
			
			//persistent状态,当属性发生改变的时候,hibernate会自动和数据库同步
			user.setName("发哥");
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testReadByLoadMethod2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//采用load加载数据,如果数据库中没有相应的数据
			//那么抛出ObjectNotFoundException
			User user = (User)session.load(User.class, "55555555");
			
			System.out.println(user.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			throw new java.lang.RuntimeException();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testUpdate1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//手动构造的detached状态的对象
			User user = new User();
			user.setId("402880d01b9be8dc011b9be9b23d0001");
			user.setName("德华");
			
			session.update(user);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	

	public void testDelete1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
//			//手动构造的detached状态的对象
//			User user = new User();
//			user.setId("402880d01b9be8dc011b9be9b23d0001");
//			user.setName("德华");
//			session.delete(user);
			
			User user = (User)session.load(User.class, "402880d01b9be8dc011b9be9b23d0001");
			session.delete(user);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		//transient状态
	}	
	
}




get和load的区别?
* get不支持lazy,load支持lazy
* 采用get加载数据,如果没有匹配的数据,返回null,而load则抛出异常

transient状态的特征?
* 在数据库中没有与之匹配的数据
* 没有纳入session的管理

persistent状态的特征?
* persistent状态的对象在数据库中有与之匹配的数据
* 纳入了session的管理
* 在清理缓存(脏数据检查)的时候,会和数据库同步

detached状态的特征?
* 在数据库中有与之匹配的数据
* 没有纳入session的管理
   发表时间:2008-11-22  
楼主是尚学堂的学生??
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics