一级缓存:
一级缓存不能跨session, 只能缓存实体对象, 不能缓存属性
package com.rl.hiber.test;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.rl.hiber.model.User;
import com.rl.hiber.utils.HibernateUtils;
/**
* 不跨session的一级缓存
* @author Administrator
*
*/
public class TestHibernate {
@Test
public void testAdd(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
try {
for(int i = 0; i < 10; i++){
User user = new User();
user.setUname("张三");
user.setGender(1);
user.setBirthday(new Date());
session.save(user);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeResource(session);
}
}
/**
* load方法使用一级缓存
*/
@Test
public void query1(){
Session session = HibernateUtils.getSession();
try {
//发出sql
User user = (User)session.load(User.class, 1);
System.out.println(user);
//不发sql(一级缓存)
User user1 = (User)session.load(User.class, 1);
System.out.println(user1);
} finally{
HibernateUtils.closeResource(session);
}
}
/**
* get的方法使用一级缓存
*/
@Test
public void query2(){
Session session = HibernateUtils.getSession();
try {
User user = (User)session.get(User.class, 1);
System.out.println(user);
User user1 = (User)session.load(User.class, 1);
System.out.println(user1);
} finally{
HibernateUtils.closeResource(session);
}
}
/**
* list方法查询不使用一级缓存
*/
@Test
public void query3(){
Session session = HibernateUtils.getSession();
try {
Query query = session.createQuery("from User");
//发出sql
List<User> userList = query.list();
for(User user: userList){
System.out.println(user);
}
Query query1 = session.createQuery("from User");
//发出sql
List<User> userList1 = query1.list();
for(User user: userList1){
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* iterate方法查询使用一级缓存
*/
@Test
public void query4(){
Session session = HibernateUtils.getSession();
try {
Query query = session.createQuery("from User");
//发出sql 查出所有id
Iterator<User> iterator = query.iterate();
while(iterator.hasNext()){
//根据具体的每一个id查出详情
User user = iterator.next();
System.out.println(user);
}
Query query1 = session.createQuery("from User");
//发出sql 查出所有的id
Iterator<User> iterator1 = query1.iterate();
while(iterator1.hasNext()){
//不在发出sql, 使用一级缓存
User user = iterator1.next();
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.rl.hiber.test;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.rl.hiber.model.User;
import com.rl.hiber.utils.HibernateUtils;
public class TestHibernate2 {
/**
* 无法跨session
*/
@Test
public void query1(){
Session session = HibernateUtils.getSession();
try {
//发出sql
User user = (User)session.load(User.class, 1);
System.out.println(user);
} finally{
//关闭session
HibernateUtils.closeSession(session);
}
Session session1 = HibernateUtils.getSession();
try {
//发出sql
User user = (User)session1.load(User.class, 1);
System.out.println(user);
} finally{
HibernateUtils.closeResource(session1);
}
}
/**
* 一级缓存不能缓存属性
*/
@Test
public void query2(){
Session session = HibernateUtils.getSession();
try {
Query query = session.createQuery("select u.uname from User u where u.userId = 1");
//发出sql
Object name = query.uniqueResult();
System.out.println(name);
Query query1 = session.createQuery("select u.uname from User u where u.userId = 1");
//发出sql
Object name1 = query1.uniqueResult();
System.out.println(name1);
} finally{
HibernateUtils.closeResource(session);
}
}
/**
* 一级缓存的刷新
*/
@Test
public void update(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
try {
//发出sql
User user = (User)session.load(User.class, 1);
System.out.println(user);
//发出update的sql, 同时更新一级缓存
user.setUname("李四");
session.update(user);
tx.commit();
//不发出sql, 查询到的是更新后的数据
User use1 = (User)session.load(User.class, 1);
System.out.println(use1);
} catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeResource(session);
}
}
/**
* 保存时刷新缓存
*/
@Test
public void save(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
try {
User user = new User();
user.setBirthday(new Date());
user.setGender(1);
user.setUname("赵六");
//发出insert的sql
session.save(user);
//不发出sql, 直接从一级缓存中获取数据
User user1 = (User)session.load(User.class, user.getUserId());
System.out.println(user1);
tx.commit();
} catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeResource(session);
}
}
/**
* 一级缓存的管理(evit()和clear())
*/
@Test
public void manageTest(){
Session session = HibernateUtils.getSession();
try {
//发出sql
User user = (User)session.load(User.class, 2);
System.out.println(user);
//将一级缓存中的user逐出
session.evict(user);
//把一级缓存中所有的数据都清空
session.clear();
//必须重新发出sql
User user1 = (User)session.load(User.class, 2);
System.out.println(user1);
} finally{
HibernateUtils.closeResource(session);
}
}
}