Hibernate中多种方式解除延迟加载
问题引发:因为dao使用load(),默认延迟加载的,当在biz关闭session之后,UI层无法获取对象的非id属性值
解决方案:
1.变成get,即时加载
2.用Hibernate.isInitialized(obj)被初始化
3.类级别的lazy属性设为true
4.用final修饰类,因为用final修饰的类不允许有子类。而我们所说的内存中保存的代理对象其实就是该类的子类。此方法从根本上解决了延迟加载
5.在事务提交之前,先调用一下该类的非id属性
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAy0AAAFRCAIAAAD2DdXCAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAgAElEQVR4nO3dT2gcSZ7o8ci3fjAwb5/d72DDLm0veHHDsGDPHkzPwYtxDzTb2FSBuxcPGObi9lBiMAvjQ+9BWvf4sgfVZQ4zUNLF0A+Z7l6wkRhoaGkb9tCiLy3RyzTSTZqTij2oeLw5vJ2ZfIdY54bizy8jszIzStL3gzGlzMiIX0ZGZv4qM6sqy/NcAQAAoHP/LXUAAAAApxR5GAAAQBrkYQAAAGmQhwEAAKRBHgYAAJAGeRgAAEAa5GEAAABpkIcBAACkQR4GAACQBnkYAABAGuRhAAAAaZCHAQAApEEeBgAAkAZ5GAAAQBrkYQAAAGmQh82oLMtShwAAANqV5XmeOgb4ZVmW57mQkFnbzioZWpYtDgDAjDiTOgAE6YQplDbJOZaea/5J+gUAwKwhD5tFtdMmb3JGEgYAwGzq4vmw3d3dhYWFLMuyLFtaWuqgxVMrf6WYojOz7JV0oQEAAFsXedhHH3304MGDPM/X19cfPnx48lKxfr/f7/cnk0kblWdHRRY2J7rJGQAAmAVd37GynluCl3knMeZ1aVXm/y3FDAAAqpqV763Ismw4HG5ubs7NzenX5tzt7W09vd/vb2xsTNPQeDweDoe6qu3t7WL67u5u0cTa2ppQ2J2oX5dGK6/jNLKApuoHAACtyDt0cHCglJqfn3dn6WBWVlbyPN/Z2VFKjUYjPUv/ubq6muf56uqqUmp9fb12DIPBoNfrHR4e5nluNaGr3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6RWlfNSsTr/ZZUdXr82a3VI2+FWa1OkiEHSH0ujS2JCtSKnL/tYrN8i4MHEfsMAAAAGlwXxIAACAN8jAAAIA0yMMAAADSIA8DAABIgzwMAAAgDfIwAACANMjDAAAA0iAPAwAASIM8DAAAIA3yMAAAgDTIwwAAANIgDwMAAEiDPAwAACAN8jAAAIA0yMMAAADSIA8DAABIgzwMAAAgDfIwAACANMjDAAAA0iAPAwAASIM8DAAAIA3yMAAAgDTIwwAAANIgDwMAAEiDPAwAACAN8jAAAIA0yMMAAADSIA8DAABIgzwMAAAgDfIwAACANMjDAAAA0iAPAwAASIM8DAAAIA3yMAAAgDTIwwAAANIgDwMAAEiDPAwAACAN8jAAAIA0yMMAAADSIA8DAABIgzwMAAAgDfIwAACANMjDAAAA0iAPAwAASIM8DAAAIA3yMAAAgDTIwwAAANIgDwMAAEiDPAwAACAN8jAAAIA0yMMAAADSIA8DAABIgzwMAAAgDfIwAACANMjDAAAA0iAPAwAASIM8DAAAIA3yMAAAgDTIwwAAANIgDwMAAEiDPAwAACAN8jAAAIA0yMMAAADSIA8DAABIgzwMAAAgDfIwAACANMjDAAAA0vj/UGyj1Zbw550AAAAASUVORK5CYII=" alt="" />
package cn.happy.entity; public class Users {
private Integer id;
private String name;
private String password;
private String telephone;
private String userName;
private String isAdmin; public Integer getId() {
return id;
} public void setId(Integer 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 String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getIsAdmin() {
return isAdmin;
} public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
} }
User.java
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity"> <class name="Users" table="USERS" lazy="false">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" type="string" column="NAME" />
<property name="password" />
<property name="telephone" />
<property name="userName" />
<property name="isAdmin" />
</class> </hibernate-mapping>
Users.hbm.xml
package cn.happy.dao; import java.io.Serializable; import cn.happy.until.HibernateUtil; public class UsersDao {
//添加用户记录
public Serializable save(Object object){
return HibernateUtil.currentSession().save(object);
}
//检索oid
public Object get(Class clazz, Serializable id){
return HibernateUtil.currentSession().load(clazz, id);
} //修改用户记录
public void update(Object object){
HibernateUtil.currentSession().update(object);
} }
UsersDao
package cn.happy.biz; import java.io.Serializable; import org.hibernate.Hibernate;
import org.hibernate.Transaction; import cn.happy.dao.UsersDao;
import cn.happy.until.HibernateUtil; public class UsersBiz {
UsersDao user=new UsersDao();
//添加用户记录
public Serializable save(Object object){
Transaction tx=HibernateUtil.currentSession().beginTransaction();
Serializable result=user.save(object);
tx.commit();
return result;
}
//检索oid
public Object get(Class clazz, Serializable id){
Transaction tx = HibernateUtil.currentSession().beginTransaction();
Object obj=user.get(clazz, id);
// if(!Hibernate.isInitialized(obj)){
// Hibernate.initialize(obj);
// }
tx.commit();
HibernateUtil.closeSession();
return obj;
} //修改用户记录
public void update(Object object){
Transaction tx=HibernateUtil.currentSession().beginTransaction();
user.update(object);
tx.commit();
}
}
UsersBiz
package cn.happy.ui; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import cn.happy.biz.UsersBiz;
import cn.happy.entity.Users;
import cn.happy.until.HibernateUtil; public class Test {
public static void main(String[] args) {
get();
}
//添加
public static void add(){
Users user=new Users();
user.setName("回青");
user.setIsAdmin("yes");
user.setPassword("123456");
user.setTelephone("18265808945");
user.setUserName("户梦艳");
UsersBiz usersBiz=new UsersBiz();
usersBiz.save(user);
System.out.println("ok"); }
//修改
public static void modify(){
Session session = HibernateUtil.currentSession();
Transaction transaction = session.beginTransaction();
Users user = (Users)session.get(Users.class, 6);
user.setName("岁月静好");
session.update(user);
transaction.commit();
HibernateUtil.closeSession();
} //
public static void get(){
UsersBiz ub=new UsersBiz();
Users user = (Users)ub.get(Users.class, 6);
System.out.println(user.getName());
}
}
测试类
6.用openSessionInView模式
Open Session In View模式的主要思想:在用户的每一次请求过程始终保持 一个Session对象打开着
过滤器:过滤用的。过滤请求和响应。双向过滤
实现步骤:
Until包
package cn.happy.until; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
private static final ThreadLocal sessionTL=new ThreadLocal();
private static Configuration configuration;
private final static SessionFactory sessionFactory;
static{
configuration=new Configuration().configure();
sessionFactory=configuration.buildSessionFactory();
}
public static Session currentSession(){
Session session=(Session)sessionTL.get();
//如果session为null,则打开一个新的session
if(session==null){
session=sessionFactory.openSession();
sessionTL.set(session);
}
return session;
}
public static void closeSession(){
Session session=(Session)sessionTL.get();
sessionTL.set(null);
session.close();
}
}
package cn.happy.entity; public class Users {
private Integer id;
private String name;
private String password;
private String telephone;
private String userName;
private String isAdmin; public Integer getId() {
return id;
} public void setId(Integer 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 String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getIsAdmin() {
return isAdmin;
} public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
} }
HibernateUtil.javaUser.java
entity包
package cn.happy.entity; public class Users {
private Integer id;
private String name;
private String password;
private String telephone;
private String userName;
private String isAdmin; public Integer getId() {
return id;
} public void setId(Integer 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 String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getIsAdmin() {
return isAdmin;
} public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
} }
Users.java
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity"> <class name="Users" table="USERS">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" type="string" column="NAME" />
<property name="password" />
<property name="telephone" />
<property name="userName" />
<property name="isAdmin" />
</class> </hibernate-mapping>
Users.hbm.xml
Dao包
package cn.happy.dao; import java.io.Serializable; import cn.happy.until.HibernateUtil; public class UsersDao {
//添加用户记录
public Serializable save(Object object){
return HibernateUtil.currentSession().save(object);
}
//检索oid
public Object get(Class clazz, Serializable id){
return HibernateUtil.currentSession().load(clazz, id);
} //修改用户记录
public void update(Object object){
HibernateUtil.currentSession().update(object);
} }
UsersDao.java
Biz包
package cn.happy.biz; import java.io.Serializable; import org.hibernate.Hibernate;
import org.hibernate.Transaction; import cn.happy.dao.UsersDao;
import cn.happy.until.HibernateUtil; public class UsersBiz {
UsersDao user=new UsersDao();
//添加用户记录
public Serializable save(Object object){
Transaction tx=HibernateUtil.currentSession().beginTransaction();
Serializable result=user.save(object);
tx.commit();
return result;
}
//检索oid
public Object get(Class clazz, Serializable id){
//Transaction tx = HibernateUtil.currentSession().beginTransaction();
Object obj=user.get(clazz, id);
// if(!Hibernate.isInitialized(obj)){
// Hibernate.initialize(obj);
// }
//tx.commit();
//HibernateUtil.closeSession();
return obj;
} //修改用户记录
public void update(Object object){
Transaction tx=HibernateUtil.currentSession().beginTransaction();
user.update(object);
tx.commit();
}
}
UsersBiz.java
Filter包
package cn.happy.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction; import cn.happy.until.HibernateUtil; public class OpenSessionInViewFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
Session session=null;
Transaction tx=null;
try {
session=HibernateUtil.currentSession();
System.out.println("filter\t"+session.hashCode());
tx=session.beginTransaction();
//执行请求处理链 双向过滤
chain.doFilter(request, response);
//返回响应 提交事务
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtil.closeSession();
} } public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub }
public void destroy() {
// TODO Auto-generated method stub } }
OpensessionInviewFilter
配置文件
从网址访问即可达到过滤器的作用
Hibernate中多种方式解除延迟加载的更多相关文章
- java中多种方式读文件
转自:http://www.jb51.net/article/16396.htm java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内 ...
- hibernate(七) hibernate中查询方式详解
序言 之前对hibernate中的查询总是搞混淆,不明白里面具体有哪些东西.就是因为缺少总结.在看这篇文章之前,你应该知道的是数据库的一些查询操作,多表查询等,如果不明白,可以先去看一下 MySQL数 ...
- Hibernate学习(七)———— hibernate中查询方式详解
序言 之前对hibernate中的查询总是搞混淆,不明白里面具体有哪些东西.就是因为缺少总结.在看这篇文章之前,你应该知道的是数据库的一些查询操作,多表查询等 --WH 一.hibernate中的5种 ...
- 013——VUE中多种方式使用VUE控制style样式属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java中多种方式解析xml
第一种:DOM.DOM的全称是Document Object Model,也即文档对象模型.在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称DOM树),应用程序正 ...
- iOS中多种方式实现打电话、发短信、写邮件
一.打电话 打电话--方法1 NSURL *URL = [NSURL URLWithString:@"tel://10010"]; [[UIApplication sharedAp ...
- JS中多种方式创建对象
1.内置对象创建 var girl=new Object(); girl.name='hxl'; console.log(typeof girl); 2.工厂模式,寄生构造函数模式 function ...
- Hibernate中延迟加载和缓存
什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象 访问 ...
- (十六)Hibernate中的延迟加载
一.什么是延迟加载 为了节省Hibernate加载对象的性能节销,在Hibernate中真正需要用到这个对象时,才会发出 SQL语句来抓取这个对象.这一个过程称为延迟加载. 二.延迟加载 ...
随机推荐
- Linux平台下安装MySQL
1.下载RPM包 http://dev.mysql.com/downloads/mysql/5.5.html#downloads 选择[Red Hat & Oracle Enterprise ...
- 如何用管理员账户登录windows10
1.判断自己是否是管理员 在命令行中输入 whoami 只要显示不是 administrator 都不是管理员 2. 接着在命令行中输入 net user 可以查看这台电脑有多少个用户 ...
- Permute Digits 915C
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...
- 模块hashlib和logging
Python的hashlib提供了常见的摘要算法MD5. 我们以常见的摘要算法MD5为例,计算出一个字符串的MD5值: import hashlib md5=hashlib.md5() md5.upd ...
- SpringMVC + MyBatis简单示例
该项目基于Maven开发,该项目中包含了MyBatis自动创建表的功能,具体实现查阅MyBatis---自动创建表 源码下载 配置 maven支持pom.xml <project xmlns=& ...
- str_replace字符串替换
字符串替换, src 源字符串, buf_size 缓冲大小, search搜索的字符串大小, repstr 需要替换成的字符串 int str_replace(char *src, unsigned ...
- SetConsoleCtrlHandler
Excerpt: Registering a Control Handler Function This is an example of the SetConsoleCtrlHandler fu ...
- 自己定义的TryParse()
out 参数的练习,自己定义的TryParse()... 一开始写错了,错在:判断str[i] >= '0'&& str[i]<='9'时,把str[i]当数字去判断了.. ...
- MySQL 5.7.18 压缩包版配置记录
1.解压到一个目录(建议根目录),比如:D:\mysql2.在系统Path中添加 D:\mysql\bin3.这个版本不带my-default.ini,需要自己写,放在D:\mysql\my.ini, ...
- 珍藏版 Python 开发工程师面试试题
珍藏版 Python 开发工程师面试试题 说明:不拿到几家公司的offer,那就是卑鄙的浪费 一.Python_基础语法 1.可变与不可变类型: 2.浅拷贝与深拷贝的实现方式.区别:deepcopy如 ...