澳门新蒲京娱乐

新蒲京娱乐场777 8
excel隐藏表格怎么打印,Excel表格打印时怎么进行页面设置新蒲京娱乐场777:
新蒲京官方下载 4
接口调用

hibernate延迟加载org,Hibernate延迟加载

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

延期加载在Hibernate中是默许延迟加载;

  //dept.getEmp()拿到子表的笔录集结
  System.out.println(dept.getEmp());

测量检验代码生龙活虎:

}

HibernateTest.java

private static DeptEntity getDept(String did){
  Session session = sessionFactory.openSession();
新蒲京官方下载 ,  DeptEntity dept = (DeptEntity)session.get(DeptEntity.class, did);
  session.close();
  return dept;
}

代码:

 

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

运作结果:

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)

import org.hibernate.Session;

 

/**
 *
 * @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方法时,集合也拿到到了,就不会冒出延迟加载难题了。

运转结果:

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

此间我们看到我们关怀的是id,name和description属性,

就算如此有:  Set<Product>
products=category.getProducts();
代码,即:不管理集结对象。不过我们只要的是:

  System.out.println(“id:”+category.getId()+” 
,name:”+category.getName()+”,
description:”+category.getDescription());
出口的是id,name和description属性值,其余的大家随意,所以Hibernate用了lazy
loading(延迟加载),带给的利润正是我们不关切的

多少,不用今后加载,当大家要用的时候,才去加载

测验代码二:

HibernateTest.java

代码:

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

import java.util.Set;

import org.hibernate.Session;

/**
 *
 * @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();
  for(Product product:products){
   System.out.println(“ID:  “+product.getId()+” 
name:”+product.getName()+” price: “+product.getPrice());
  }  
  session.getTransaction().commit();  
 }
 
 
}

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图