02hibernate_session
package com.bjsxt.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);//发Hibernate: insert into User //save执行后:让id=402881e538dc30fd0138dc3189490001
user.setName("猪八戒");
//发Hibernate: update User set,Hibernate怎么知道什么时候要发update语句呢?
//它做的是数据版本也叫 脏数据检查
//session.update(user);
tx.commit();
}catch(Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
HibernateUtils.closeSession(session);
}
//detached状态//显著特点:a.没有被session管理,同时数据库中有相应的记录
user.setName("七仙女");
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//又变成了persistent状态
session.update(user);
session.getTransaction().commit();//commit后,发Hibernate: update User set XX ,此时数据库中记录被更新
}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, "402881e538dc30fd0138dc3189490001");
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, "402881e538dc33410138dc335ef30001");
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("402881e538dc33410138dc335ef30001");
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, "402881e538dc33410138dc335ef30001");
session.delete(user);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
//transient状态
}
}
-----------------------------------------------------
package com.bjsxt.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import junit.framework.TestCase;
public class QueryTest extends TestCase {
public void testQuery() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Query query = session.createQuery("from User");
query.setFirstResult(0);//注意第一条记录从0开始
query.setMaxResults(4);
List userList = query.list();
for (Iterator iter=userList.iterator(); iter.hasNext();) {
User user = (User)iter.next();
System.out.println(user.getId());
System.out.println(user.getName());
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
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);//发Hibernate: insert into User //save执行后:让id=402881e538dc30fd0138dc3189490001
user.setName("猪八戒");
//发Hibernate: update User set,Hibernate怎么知道什么时候要发update语句呢?
//它做的是数据版本也叫 脏数据检查
//session.update(user);
tx.commit();
}catch(Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
HibernateUtils.closeSession(session);
}
//detached状态//显著特点:a.没有被session管理,同时数据库中有相应的记录
user.setName("七仙女");
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//又变成了persistent状态
session.update(user);
session.getTransaction().commit();//commit后,发Hibernate: update User set XX ,此时数据库中记录被更新
}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, "402881e538dc30fd0138dc3189490001");
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, "402881e538dc33410138dc335ef30001");
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("402881e538dc33410138dc335ef30001");
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, "402881e538dc33410138dc335ef30001");
session.delete(user);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
//transient状态
}
}
-----------------------------------------------------
package com.bjsxt.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import junit.framework.TestCase;
public class QueryTest extends TestCase {
public void testQuery() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Query query = session.createQuery("from User");
query.setFirstResult(0);//注意第一条记录从0开始
query.setMaxResults(4);
List userList = query.list();
for (Iterator iter=userList.iterator(); iter.hasNext();) {
User user = (User)iter.next();
System.out.println(user.getId());
System.out.println(user.getName());
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}