package net.knight.hibernate.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* 从hibernate.cfg.xml获取配置文件
* getSessionFactory():获得Session工厂
* getSession():从SessionFactory获得Session
* getTransaction():从Session中获得Transaction
* closeSession():关闭Session
*/
public class HibernateUtils {
private static SessionFactory factory;
/**
* 建立一个static块可以写多条语句.
*/
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static Transaction getTransaction(Session session) {
return session.beginTransaction();
}
public static void closeSession(Session session) {
if(session != null) {
if(session.isOpen()){
session.close();
}
}
}
}
The first class is used to test Client category Hibernate_first project, taking into account a number of categories if the test when the test is not convenient, the junit unit testing tools to use to test, you can also study the use of junit. These are as follows
1. Since the test category will not be finally released when packaged, so put the test file on the project are outside a wise choice. Also easy to manage. First of all a set up in the project source directory, the directory name into my test set up . this directory and the src directory level class.
2. Set up in the directory test-class path, here set my path and the type of consistent src. Whether or not consensus can be, there is no requirement.
3. Set up the test class, because it is a test Session, set up so as SessionTest
package net.knight.hibernate.test;
import junit.framework.TestCase;
/**
* junit单元测试,主要对方法进行测试
* 1.继承TestCase方法
* 2.方法名称要以test开头
* 3.测试方法不能含有参数和返回值
*/
public class SessionTest extends TestCase {
// 公共信息初始化可以放到这个方法里.
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testHello() {
System.out.println("==========SessionTest.testHello===========");
// 断言的使用方法,前面的参数是预期值,后面的参数是实际值,如果预期值不等于实际值就失败了.
this.assertEquals("", "");
}
// 需要销毁的信息放到这里,类似于C中间的析构函数.
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
} Session has three states
1.Transient, new, when an object is Transient object, the most significant feature of this approach is not yet in the database the corresponding record. Also not included in the management of the Session at any time garbage collector can be recovered.
2.Persistent, when Transient object call save () method has become a Persistent object since the object is in the database has corresponding records. It into the management of the Session, the objects are garbage collector can not be recovered .
3.Detached, when the Persistent object to call the close () methods, such as when the object is Detached, call close () method, this object from the cache is being removed, the records of the database will not be removed. Therefore, this object is also not included in the management of the Session, but the database is recorded in the corresponding, the object can be at any time dispose of garbage recycling. because there is no object reference it.
4. There is because the corresponding database record, so can call update () and other methods, such as Detached object method call, the object became a Persistent object. When Persistent objects and then call delete () method Transient became the object of the object.
Transient - save ()--> Persistent - close ()--> Detached - update ()--> Persistent - delete ()--> Transient
package net.knight.hibernate.test;
import java.util.Date;
import net.knight.hibernate.User;
import net.knight.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import junit.framework.TestCase;
/**
* junit单元测试
* 1.继承TestCase方法
* 2.方法名称要以test开头
*
*/
public class SessionTest extends TestCase {
public void testSession() {
Session session = null;
Transaction ts = null;
User user = null;
try {
session = HibernateUtils.getSession();
ts = HibernateUtils.getTransaction(session);
// Transient状态
user = new User();
user.setName("李四");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
// Persistent状态.当属性发生改变的时候,hibernate会自动同步数据库
// 生成并执行insert into,此时的数据在缓存中,只是一个快照.
session.save(user);
// 修改user对象后,发生脏数据对比,对数据进行修改
// 生成并执行update,存入数据库.清理缓存
user.setName("王五");
ts.commit();
} catch (RuntimeException e) {
e.printStackTrace();
ts.rollback();
} finally {
HibernateUtils.closeSession(session);
}
// 再修改username,此时就是Detached状态
user.setName("赵六");
try {
// 因为Session已经关闭,则需要重新获取
session = HibernateUtils.getSession();
ts = HibernateUtils.getTransaction(session);
// 获取到session后又转换成Persistent
session.update(user);
ts.commit();
}catch(Exception e) {
e.printStackTrace();
ts.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
} 






