session级缓存 保存一个hashmap, 读出来的对象放在里面, 如果读出来50条放50条, 如果另起session原来的50条就被清除.可以手动session.clear清除.

如果同一个session里一直读, 内存里越来越多. 所以要用session.clear()处理.

java有内存泄露吗? 语法上来说没有, 但是写程序的时候会有, 用了资源要回收, 比如打开连接池.

1+N问题: 如果一个对象里关联另外一个对象, 同时fetchtype为eager, 比如many to one, 要取many里面对象的时候, 被关联对象也会被取出来, 就会取出来1+N条.

获取Topic里面的内容的时候, 被关联对象category也被取出:

category.java:

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import org.hibernate.annotations.BatchSize; @Entity
//@BatchSize(size=5)
public class Category {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

topic.java:

package com.bjsxt.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany; import org.hibernate.annotations.BatchSize; @Entity
@NamedQueries(
{
@NamedQuery(name="topic.selectCertainTopic", query="from Topic t where t.id = :id")
}
)
/*
@NamedNativeQueries(
{
@NamedNativeQuery(name="topic.select2_5Topic", query="select * from topic limit 2, 5")
}
)
*/ public class Topic {
private int id;
private String title;
private Category category;
private Date createDate;
private List<Msg> msgs = new ArrayList<Msg>();
@OneToMany(mappedBy="topic")
public List<Msg> getMsgs() { return msgs;
}
public void setMsgs(List<Msg> msgs) {
this.msgs = msgs;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@ManyToOne
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} }

msg.java:

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne; @Entity
public class Msg {
private int id;
private String cont;
private Topic topic;
@ManyToOne
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} public String getCont() {
return cont;
}
public void setCont(String cont) {
this.cont = cont;
} }

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">linda0213</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost::SXT</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
--> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size"></property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">update</property>
-->
<!-- <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
<mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
-->
<mapping class="com.bjsxt.hibernate.Category"/>
<mapping class="com.bjsxt.hibernate.Msg"/>
<mapping class="com.bjsxt.hibernate.Topic"/> </session-factory> </hibernate-configuration>

test文件:

package com.bjsxt.hibernate;

import java.util.Date;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; public class HibernateQLTest {
private static SessionFactory sf; @BeforeClass
public static void beforeClass() {
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
} @Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
} @Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction(); for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(c);
session.save(t);
} session.getTransaction().commit();
session.close();
} //N+1
@Test
public void testQuery1() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list(); for(Topic t : topics) {
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close(); } @Test
public void testQuery2() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list(); for(Topic t : topics) {
System.out.println(t.getId() + "-" + t.getTitle());
System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close(); } //@BatchSize
@Test
public void testQuery3() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list(); for(Topic t : topics) {
System.out.println(t.getId() + "-" + t.getTitle());
System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close(); }
//join fetch
@Test
public void testQuery4() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
List<Topic> topics = (List<Topic>)session.createQuery("from Topic t left join fetch t.category c").list(); for(Topic t : topics) {
System.out.println(t.getId() + "-" + t.getTitle());
System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close(); }
public static void main(String[] args) {
beforeClass();
}
}

此时输出结果会顺带打印出 category的查询信息,

Hibernate:
select
topic0_.id as id2_,
topic0_.category_id as category4_2_,
topic0_.createDate as createDate2_,
topic0_.title as title2_
from
Topic topic0_
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
Hibernate:
select
category0_.id as id0_0_,
category0_.name as name0_0_
from
Category category0_
where
category0_.id=?
1-t0
2-t1
3-t2
4-t3
5-t4
6-t5
7-t6
8-t7
9-t8
10-t9

解决方案

1. 在topic.java 里many to one下写入fetch=FetchType.LAZY

2. 在test文件里用Creteria替换Query.因为Creteria是表连接的方式, 和4一样.

3. 在category里@Entity下面加上 @BatchSize(size=5), 这样10条记录的话, 发出2条sql就可以了.

4, 使用join fetch:

//join fetch
@Test
public void testQuery4() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
List<Topic> topics = (List<Topic>)session.createQuery("from Topic t left join fetch t.category c").list(); for(Topic t : topics) {
System.out.println(t.getId() + "-" + t.getTitle());
System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close(); }

如何选择使用哪个?

lazy:就取对象属性, 不取关联对象的属性

join fetch:马上就用关联对象的属性  

hibernate---性能优化, 1+N问题的更多相关文章

  1. Hibernate性能优化之EHCache缓存

    像Hibernate这种ORM框架,相较于JDBC操作,需要有更复杂的机制来实现映射.对象状态管理等,因此在性能和效率上有一定的损耗. 在保证避免映射产生低效的SQL操作外,缓存是提升Hibernat ...

  2. 8.Hibernate性能优化

    性能优化 1.注意session.clear() 的运用,尤其在不断分页的时候 a) 在一个大集合中进行遍历,遍历msg,取出其中额含有敏感字样的对象 b) 另外一种形式的内存泄漏( //面试题:Ja ...

  3. Hibernate性能优化之SessionFactory重用

    Hibernate优化的方式有很多,如缓存.延迟加载以及与SQL合理映射,通过对SessionFactory使用的优化是最基础的. SessionFactory负责创建Session实例,Sessio ...

  4. Hibernate性能优化

    1.性能是与具体的项目挂钩的,并不是对于A项目某种优化方法好就适用于B项目.性能需要不断的测试检验出来的.....(废话) 2.session.clear()方法的使用,通常session是有缓存的 ...

  5. 第七章 Hibernate性能优化

    一对一关联 实体类关系 一对多 多对多 一对一 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方 ...

  6. Hibernate 性能优化一对一关联映射

    概述: hibernate提供了两种映射一对一关联的方式:按照外键映射和按照主键映射. 下面以员工账号和员工档案为例 ,介绍两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1)保存员工档 ...

  7. Hibernate 性能优化之一级缓存

     1.一级缓存的生命周期     一级缓存在session中存放,只要打开session,一级缓存就存在了,当session关闭的时候,一级缓存就不存在了   2.一级缓存是依赖于谁存在的      ...

  8. 关于Hibernate性能优化之 FetchType=Lazy时查询数据

    当表A和表B一对多的关系 对于A和B的实体类,设置FetchType=EAGER时,取A表数据,对应B表的数据都会跟着一起加载,优点不用进行二次查询.缺点是严重影响数据查询的访问时间. 解决办法Fet ...

  9. hibernate 性能优化之 1+N 问题

    1. 注意 session.clear()的运用,尤其在不断分页查询的时候 a) 在一个大集合中进行遍历,遍历 msg,去除其中的含有敏感字样的对象 b) 另外一种形式的内存泄漏 面试题:Java 有 ...

  10. Hibernate 性能优化之查询缓存

    查询缓存是建立在二级缓存基础之上的,所以与二级缓存特性相似,是共享的,适合修改不是很频繁的数据 查询缓存不是默认开启的,需要设置      1.在cfg文件中配置 <property name= ...

随机推荐

  1. 80-th Level Archeology

    80-th Level Archeology time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Bill Total Value

    Bill Total Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. OpenGL网络资源

    转 十大OpenGL教程 1.http://nehe.gamedev.net/这个是我觉得全世界最知名的OpenGL教程,而且有网友将其中48个教程翻译成了中文http://www.owlei.com ...

  4. UIImagePikerController 浅析

    原文链接:http://www.jianshu.com/p/2ac85aca4468 UIImagePickerController是iOS系统提供的和系统的相册和相机交互的一个类,可以用来获取相册的 ...

  5. TcpClient 读写流

    TcpClient 读写流 TcpClient tcp = new TcpClient(); tcp.Connect(IPAddress.Parse("192.168.1.161" ...

  6. lucene3.6.1 经典案例 入门教程 (包含从文件中读取content)

    转载http://liqita.iteye.com/blog/1676664 第一步:下载lucene的核心包 lucene-core-3.6.1-javadoc.jar (3.5 MB) lucen ...

  7. AngularJs: Reload page

    <a ng-click="reloadRoute()" class="navbar-brand" title="home" data- ...

  8. 고 보니: 做完A发现新的事实B

    1. 선생님 얘기를 듣고 보니 이제 이해가 되네요. 2. 막상 옷을 입고 보니 꽤 잘 어우리는 것 같았다. 不能用于过去式 다 보니和 고 보니区别 다 보니: 表示在做某事过程中发现某状 ...

  9. kafka configuration

    http://kafka.apache.org/08/configuration.html

  10. Shorten Diameter

    Shorten Diameter Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 600 points P ...