而hibernate3的配置是
一般情况下,Hibernate会默认给我们设置延迟加载。lazy="true" ,这样会提升我们的系统性能,所以一般情况下,我们不会去
集合延迟加载初始化失败,不能初始化一个代理。就是集合在非一对一对象关系中,为了节省资源是默认延迟加载,而get方法又是非延迟加载,所以在执行完一次数据库查询后就执行session.close();关闭了session,而集合是延迟加载,在使用集合时再加载,此时session已经关闭,所以得不到代理。解决方法:可以在主表的hbm配置文件中,在<set>标签里设置lazy="false",集合就不延迟加载了,因此在执行get方法时,集合也获取到了,就不会出现延迟加载问题了。
Java代码
HibernateTest.java
Exception in thread "main" org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role:
com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not
initialize proxy - no Session
at
org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at
org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at
org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
at
org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
at
org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:326)
at java.lang.String.valueOf(String.java:2827)
at java.io.PrintStream.println(PrintStream.java:771)
at
com.javakc.hibernate.onetomany.action.TestAction.main(TestAction.java:74)
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi=""
- xsi:noNamespaceSchemaLocation=""
- updateCheck="false">
- <!--
- name:cache唯一标识
- eternal:缓存是否永久有效
- maxElementsInMemory:内存中最大缓存对象数
- overflowToDisk(true,false):缓存对象达到最大数后,将缓存写到硬盘中
- diskPersistent:硬盘持久化
- timeToIdleSeconds:缓存清除时间
- timeToLiveSeconds:缓存存活时间
- memoryStoreEvictionPolicy:缓存清空策略
- 1.FIFO:first in first out 先讲先出
- 2.LFU: Less Frequently Used 一直以来最少被使用的
- 3.LRU:Least Recently Used 最近最少使用的
- -->
- <defaultCache maxElementsInMemory="1000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
- <cache name="userCache" eternal="false" maxElementsInMemory="1000"
- overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"
- timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" />
- </ehcache>
import org.hibernate.Session;
//dept.getEmp()得到子表的记录集合
System.out.println(dept.getEmp());
转载:
设置lazy="false",当然在特殊的情况下,我们必须要取消延迟加载的时候,我们就把lazy="false",就可以了
public static void main(String[] args) {
DeptEntity dept = getDept("402882e762ae888d0162ae888e420000");
注意:hibernate4和hibernate3配置不一样,hibernate4是
取消延迟加载:
运行结果:
第二条select语句,对于我们的需求是没有必要的,他只有一个用处就是占用我们的程序执行时间。当然,
private static DeptEntity getDept(String did){
Session session = sessionFactory.openSession();
DeptEntity dept = (DeptEntity)session.get(DeptEntity.class, did);
session.close();
return dept;
}
此处有一个疑问是:hibernate4的官方文档中,已经把class改了,但是属性名称没有改,还是 hibernate.cache.provider_class,不是上面的 hibernate.cache.region.factory_class,但是写成hibernate.cache.provider_class会 报下面错误
/**
*
*/
package com.b510.examples;
项目结构如下
运行效果:
}
hibernate使用版本是:hibernate-release-4.3.4.Final
import java.util.Set;
注:hibernate 4.2.5版本ehcache缓存不依赖commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar
/**
*
*/
package com.b510.examples;
ehcache jar包:hibernate-release-4.3.4.Finalliboptionalehcache下所有包
这里我们看到我们关心的是id,name和description属性,
/**
*
* @author XHW
*
* @date 2011-7-18
*
*/
public class HibernateTest {
public static void main(String[] args) {
new HibernateTest().update();
}
public void update(){
Session
session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Category category=(Category)session.get(Category.class, 1);
System.out.println("id:"+category.getId()+"
,name:"+category.getName()+",
description:"+category.getDescription());
Set<Product> products=category.getProducts();
session.getTransaction().commit();
}
}
ehcache.xml
import java.util.Set;
- <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
log4j:WARN No appenders could be found for logger
(org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
select
category0_.id as id1_0_,
category0_.name as name1_0_,
category0_.description as descript3_1_0_
from
users.category category0_
where
category0_.id=?
id:1 ,name:java,
description:java好啊
本文由美狮美高梅官方网站发布,转载请注明来源
关键词: