显示地初始化Hibernate代理对象
Person person = (Person)session.load(Person.class,1);
if(!Hibernate.isInitialized(person)){
Hibernate.initialize(person);
}
session.close();
person.getName();
如果不显示的初始化Hibernate代理对象,且在session范围内程序没有访问Person导致Person没有初始化,在session关闭后再访问person对象,会抛出以下异常:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
这个东西还是挺有用的,如下:
对于懒加载的对象显示地初始化,既不延长session的范围,也阻止了出错.
Person person = (Person)test.session.get(Person.class,1);
Hibernate.initialize(person.getAddresses());//person.getAddresses()是一个set集合
session.close();
System.out.println(person.getAddresses());
分享到:
相关推荐
3.5.1 Hibernate的初始化 3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 ...
3.5.1 Hibernate的初始化 61 3.5.2 访问Hibernate的Session接口 63 3.6 运行helloapp应用 67 3.6.1 创建运行本书范例的系统环境 67 3.6.2 创建helloapp应用的目录结构 72 3.6.3 把helloapp应用作为独立应用程序...
(2)SessionFactory:初始化Hibernate,充当数据存储源的代理,创建Session对象。一个SessinFactory实例对应一个数据存储源,应用从SessionFactory中获得Session实例。如果应用同时访问多个DB,怎需要为每个数据库...
3.5.1 Hibernate的初始化 61 3.5.2 访问Hibernate的Session接口 63 3.6 运行helloapp应用 67 3.6.1 创建运行本书范例的系统环境 67 3.6.2 创建helloapp应用的目录结构 72 3.6.3 把helloapp应用作为独立应用程序...
SessionFactory接口 SessionFactory接口负责初始化Hibernate。它充当数据存储源的代理,并负责创建Session对象。这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只...
3.5.1 Hibernate的初始化 3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 ...
3.5.1 Hibernate的初始化 3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 ...
3.5.1 Hibernate的初始化 3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 ...
SessionFactory接口负责初始化Hibernate。它充当数据存储源的代理,并负责创建Session对象。这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只需要一个...
(2)SessionFactory:初始化Hibernate,充当数据存储源的代理,创建Session对象。一个SessinFactory实例对应一个数据存储源,应用从SessionFactory中获得Session实例。如果应用同时访问多个DB,怎需要为每个数据库...
延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations) 6.9. 三重关联(Ternary Associations) 6.10....
1、SHH整合 详细清晰的标准配置,主流... 显示层应用多少方式,例如Session显示用户,EL应用,jsp内置对象应用 使用主流的JSP标签JSTL,STRUTS2 标签 ongl数据显示 是一个集成好多主流技术的两个显示数据与登陆的DEMO
b) 在内存中,如果有多个set(代理)容器需要初始化, 则当访问任何一个代理set容器时,一次初始化n个set容器,减少sql语句; c) 产生的语句是:select * from ….. where FK in (?,?,?... …n); 3. 迫切(fetch)左外...
延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations) 6.9. 三重关联(Ternary Associations) 6.10....
1. get() and load() Java代码 session.get(Clazz, id)... 如果类的映射使用了代理(proxy),load()方法会返回一个未初始化的代理,直到你调用该代理的某方法时才会去访问数据库。 若你希望在某对象中创建一个指向另一个
我们前面已经指出Oracle的Lob字段和一般类型的字段在操作上有一个明显的区别--那就是你必须首先通过Oracle的empty_blob()/empty_clob()初始化Lob字段,然后获取该字段的引用,通过这个引用更改其值。所以要完成对...
Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定...
SessionFactory接口 SessionFactory接口负责初始化Hibernate。它充当数据存储源的代理,并负责 创建Session对象。这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的 ,因为一般情况下,一个项目通常只...