【www.129028.com】ehcache下全体包,集合延迟加载领

2019-11-18 02:21 来源:未知

public static void main(String[] args) {
 
  DeptEntity dept = getDept("402882e762ae888d0162ae888e420000");

转载:

延迟加载在Hibernate中是默认延迟加载;

  //dept.getEmp()得到子表的记录集合
  System.out.println(dept.getEmp());

hibernate使用版本是:hibernate-release-4.3.4.Final

测试代码一:

}

需要的jar包:hibernate-release-4.3.4Finallibrequired下所有jar包

HibernateTest.java

private static DeptEntity getDept(String did){
  Session session = sessionFactory.openSession();
  DeptEntity dept = (DeptEntity)session.get(DeptEntity.class, did);
  session.close();
  return dept;
}

ehcache  jar包:hibernate-release-4.3.4.Finalliboptionalehcache下所有包

代码:

 

junit:junit-4.10.jar和mysql-connector-java-5.1.15-bin.jar

/**
 *
 */
package com.b510.examples;

运行结果:

注:hibernate 4.2.5版本ehcache缓存不依赖commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar

import java.util.Set;

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)

项目结构如下
 
www.129028.com 1  

import org.hibernate.Session;

 

hibernate.cfg.xml

/**
 *
 * @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();  
 }
 
 
}

集合延迟加载初始化失败,不能初始化一个代理。就是集合在非一对一对象关系中,为了节省资源是默认延迟加载,而get方法又是非延迟加载,所以在执行完一次数据库查询后就执行session.close();关闭了session,而集合是延迟加载,在使用集合时再加载,此时session已经关闭,所以得不到代理。解决方法:可以在主表的hbm配置文件中,在<set>标签里设置lazy="false",集合就不延迟加载了,因此在执行get方法时,集合也获取到了,就不会出现延迟加载问题了。

  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         ";  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <!-- Database connection settings -->  
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  9.         <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate4</property>  
  10.         <property name="connection.username">root</property>  
  11.         <property name="connection.password">root</property>  
  12.         <!-- JDBC connection pool (use the built-in) -->  
  13.         <property name="connection.pool_size">1</property>  
  14.         <!-- SQL dialect -->  
  15.         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  
  16.         <!-- Enable Hibernate's automatic session context management -->  
  17.         <property name="current_session_context_class">thread</property>  
  18.         <!-- Disable the second-level cache -->  
  19.         <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 
  20.         -->  
  21.         <!-- 配置二级缓存 -->  
  22.         <property name="hibernate.cache.use_second_level_cache">true</property>  
  23.         <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>  
  24.         <!-- hibernate3的二级缓存配置 -->  
  25.         <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->  
  26.         <!-- 开启查询缓存 -->  
  27.         <property name="hibernate.cache.use_query_cache">true</property>  
  28.           
  29.         <!-- Echo all executed SQL to stdout -->  
  30.         <property name="show_sql">true</property>  
  31.         <!-- Drop and re-create the database schema on startup -->  
  32.         <property name="hbm2ddl.auto">update</property>  
  33.         <mapping class="com.test.pojo.User" />  
  34.     </session-factory>  
  35. </hibernate-configuration>  

运行结果:

 

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好啊

注意:hibernate4和hibernate3配置不一样,hibernate4是

这里我们看到我们关心的是id,name和description属性,

  1. <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>  

虽然有:  Set<Product> products=category.getProducts(); 代码,即:不处理集合对象。但是我们只要的是:

 

  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());
输出的是id,name和description属性值,其他的我们不管,所以Hibernate用了lazy loading(延迟加载),带来的好处就是我们不关心的

而hibernate3的配置是

数据,不用现在加载,当我们要用的时候,才去加载

TAG标签:
版权声明:本文由www.129028.com-澳门金沙唯一官网www129028com发布于编程新闻,转载请注明出处:【www.129028.com】ehcache下全体包,集合延迟加载领