转载自: http://www.qingsoft.cn/viewthread.php?tid=1175
用户和角色
用户user中有set<role> roles = new hashset<role>(0);
当得到user中的roles集合时,会出现下面的异常:
java代码
* org.hibernate.lazyinitializationexception: failed to lazily initialize a collection of role: com.demo.security.entity.user.roles, no session or session was closed
1. 当时我的解决是:
userdaoimpl.java
java代码
* @suppresswarnings("unchecked")
* public list<user> queryall(final integer isinitialize) {
*
* return gethibernatetemplate().executefind(new hibernatecallback(){
* public object doinhibernate(session session)
* throws hibernateexception, sqlexception {
* query q = session.createquery("from user u left join fetch u.userinfo");
* if(isinitialize == 0) return q.list();
* else{
* list<user> users = q.list();
* for(user user : users){
* if(isinitialize ==1)
* gethibernatetemplate().initialize(user.getroles());
* }
* return users;
* }
* return q.list();
* }});
* }
感觉这样写比较麻烦,觉得效率上也不高。
2. 看到网上有的是把lazy=false,在hibernate annotation中也这样的(注意红色部分)
java代码
* @manytomany(
* targetentity = role.class,
* cascade = {cascadetype.persist, cascadetype.merge},
* fetch = fetchtype.eager)
* @jointable(
* name = "user_role",
* joincolumns = {@joincolumn(name = "user_id")},
* inversejoincolumns = {@joincolumn(name = "role_id")})
* @cache(usage = cacheconcurrencystrategy.read_write)
* public set<role> getroles() {
* return roles;
* }
要是这样,效率更是糟糕(不管要不要某个数据,都会查询出来,很是浪费)
最后看了篇文章:
就是使用filter,过滤所有的链接
如果在使用filter的时候,要配置事务处理,否则会导致session处于只读状态而不能做修改、删除的动作
web.xml
xml/html代码
* <filter>
* <filter-name>hibernatefilter</filter-name>
* <filter-class>
* org.springframework.orm.hibernate3.support.opensessioninviewfilter
* </filter-class>
* </filter>
*
* <filter-mapping>
* <filter-name>hibernatefilter</filter-name>
* <url-pattern>*.action</url-pattern>
* </filter-mapping>
上面的fetch = fetchtype.eager 改成 fetch = fetchtype.lazy
是这样解释的:
spring为我们解决最让人头痛的难题之一,hibernate的session的关闭与开启问题。
hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 hibernate session 范围之内进行。如果 service 层返回一个启用了延迟加载功能的领域对象给 web 层,当 web 层访问到那些需要延迟加载的数据时,由于加载领域对象的 hibernate session 已经关闭,这些导致延迟加载数据的访问异常。而spring为我们提供的opensessioninviewfilter过滤器为我们很好的解决了这个问题。opensessioninviewfilter的主要功能是使每个请求过程绑定一个 hibernate session,即使最初的事务已经完成了,也可以在 web 层进行延迟加载的操作。opensessioninviewfilter 过滤器将 hibernate session 绑定到请求线程中,它将自动被 spring 的事务管理器探测到。所以 opensessioninviewfilter 适用于 service 层使用hibernatetransactionmanager 或 jtatransactionmanager 进行事务管理的环境,也可以用于非事务只读的数据操作中。
request-->open session-->打开连接、开始事务-->持久操作-->渲染(关闭连接、session)-->response
其中一些过程省略了,不是很关心。
分享到:
相关推荐
在这种情况下,问题的根源在于将一个序列化的Collection对象错误地解析为了List。 在问题分析部分,可以看到在写入Redis时,使用了`JSON.toJSONString(value)`将对象序列化为JSON字符串并存储在Redis中的hash结构中...
延迟加载 - 没有 SEO 负面影响 Qazy 是一个图像延迟加载器,并且与库无关(没有 jQuery)。 Qazy 消除了。 这就是它与其他惰性加载器的不同之处。 尽快在网页中加载脚本,以便它可以开始跟踪图像并延迟加载它们。 ...
懒惰地ReactIMG React Lazily IMG是一个React Wrapper组件,用于延迟加载图像。 目标是使用方便且已知的标准HTML标记,并只是延迟加载它们。特征图片标签和IMG srcset支持Webp检测占位符HTML && CSS图像支持下载图像...
* "git fetch" that grabs from a group of remotes learned to run the auto-gc only once at the very end. * A handful of Windows build patches have been upstreamed. * The code to read state files ...
延迟安装昂贵的组件,直到占位符组件滚动到视图中为止。 安装 npm install --save react-lazily-render 用法 () import React from 'react' ; import LazilyRender from 'react-lazily-render' ; ...lots of ...
- **A Quick Tip on the Execution and Memory Analysis of an Assembly in Visual Studio 2015**: Using tools like the Visual Studio Profiler to analyze the memory usage and performance of .NET ...
Exporting a lazily initialized bean (which implements SelfNaming and is annotated with ManagedResource annotation) gives IllegalStateException [SPR-17592] #22124 MockHttpServletRequest changes Accept-...
Apply the core principles of concurrency to both browser and server side development Explore the latest tools and techniques at the forefront of concurrent programming, including JavaScript promises, ...
Table of Contents Preface What this book covers What you need for this book Running a Jupyter Notebook Who this book is for How to get the most out of this book Conventions Assumptions for every ...
9. **延迟加载**:考虑到性能和用户体验,源代码可能会采用延迟加载技术,如Lazily加载图片或数据,只在实际需要时才加载,提高应用启动速度。 总的来说,这个“新特性源代码”提供了对ViewPager组件的深度定制,...
lazily. Copyright (c) 2009 Armando Blancas. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 ...
// Subscribes to a Pub/Sub channel using the internal, lazily initialized SubscriberClient redis.subscriber.subscribe("My Channel") { case message @ PubSubMessage.Message(channel, ...
例如,"After about ten minutes of waiting, I saw a middle-aged man enter her courtyard looking back carefully."中,"After about ten minutes of waiting"作为时间状语,描述动作发生的时间;"looking back ...
具有延迟同步和异步迭代器支持的出色的快速递归目录搜寻器。 安装 支持Node.js版本10及更高版本。 $ npm i get-all-files 用法 import getAllFiles from 'get-all-files' // Get array of filenames ...
/ Errrrr...Actually, I hesitate to release the source code of this control, / Because when I checked after completion, I found that it's really ugly! Putting all / graphic codes in the CDraw class ...
The aim is to provide, lazily evaluated, pull-based datastream support with the same naming as in RxJava mainly for the pre-Java-8 world. The Stream API in Java 8 is not exactly the same thing because...
- ` Lazily loaded images`:利用`loading="lazy"`属性延迟加载图片。 开放式问题涉及的技能和流程,如JavaScript延迟加载技术、关注的技术趋势、开发流程与使用的工具、网站性能优化策略、原型继承和闭包的理解等...
源码实现了怎样支持延迟加载(lazily load),可以任意多个视图内容的ScrollView。所谓延迟加载,即只加载需要显示的页面内容,即按需加载,从而当需要加载许多视图内容时,不会消耗过多内存。也支持循环滚动视图。...
- "advice"意为"建议",常搭配"a piece of advice",动词形式如"give advice", "act on sb's advice"等。 4. 替代和程度副词: - "instead"表示"代替",如"There isn't any cigarette, have a biscuit instead....
Kodein is a very simple and yet very useful dependency retrieval container. it is very easy to use and configure. Kodein works: On the JVM. On Android. On Javascript (both in the browser and on Node....