hibernate---性能优化, 1+N问题
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问题的更多相关文章
- Hibernate性能优化之EHCache缓存
像Hibernate这种ORM框架,相较于JDBC操作,需要有更复杂的机制来实现映射.对象状态管理等,因此在性能和效率上有一定的损耗. 在保证避免映射产生低效的SQL操作外,缓存是提升Hibernat ...
- 8.Hibernate性能优化
性能优化 1.注意session.clear() 的运用,尤其在不断分页的时候 a) 在一个大集合中进行遍历,遍历msg,取出其中额含有敏感字样的对象 b) 另外一种形式的内存泄漏( //面试题:Ja ...
- Hibernate性能优化之SessionFactory重用
Hibernate优化的方式有很多,如缓存.延迟加载以及与SQL合理映射,通过对SessionFactory使用的优化是最基础的. SessionFactory负责创建Session实例,Sessio ...
- Hibernate性能优化
1.性能是与具体的项目挂钩的,并不是对于A项目某种优化方法好就适用于B项目.性能需要不断的测试检验出来的.....(废话) 2.session.clear()方法的使用,通常session是有缓存的 ...
- 第七章 Hibernate性能优化
一对一关联 实体类关系 一对多 多对多 一对一 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方 ...
- Hibernate 性能优化一对一关联映射
概述: hibernate提供了两种映射一对一关联的方式:按照外键映射和按照主键映射. 下面以员工账号和员工档案为例 ,介绍两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1)保存员工档 ...
- Hibernate 性能优化之一级缓存
1.一级缓存的生命周期 一级缓存在session中存放,只要打开session,一级缓存就存在了,当session关闭的时候,一级缓存就不存在了 2.一级缓存是依赖于谁存在的 ...
- 关于Hibernate性能优化之 FetchType=Lazy时查询数据
当表A和表B一对多的关系 对于A和B的实体类,设置FetchType=EAGER时,取A表数据,对应B表的数据都会跟着一起加载,优点不用进行二次查询.缺点是严重影响数据查询的访问时间. 解决办法Fet ...
- hibernate 性能优化之 1+N 问题
1. 注意 session.clear()的运用,尤其在不断分页查询的时候 a) 在一个大集合中进行遍历,遍历 msg,去除其中的含有敏感字样的对象 b) 另外一种形式的内存泄漏 面试题:Java 有 ...
- Hibernate 性能优化之查询缓存
查询缓存是建立在二级缓存基础之上的,所以与二级缓存特性相似,是共享的,适合修改不是很频繁的数据 查询缓存不是默认开启的,需要设置 1.在cfg文件中配置 <property name= ...
随机推荐
- 做.net的早晚会用到,并且网上还没有这方面的正确资料或几乎很少
原文网址:http://www.cnblogs.com/langu/archive/2012/03/23/2413990.html 一直以来,找安装程序的msi源文件路径得到的都是“system32” ...
- 常用的opengl函数(三)
glBlendFunc 定义像素算法. void WINAPI glBlendFunc(GLenum sfactor,GLenum dfactor); 参数编辑 sfactor 指定红绿蓝和 al ...
- OpenGL ES着色器语言之操作数(官方文档第五章)
OpenGL ES着色器语言之操作数(官方文档第五章) 5.1操作数 OpenGL ES着色器语言包含如下操作符. 5.2数组下标 数组元素通过数组下标操作符([ ])进行访问.这是操作数组的唯一操作 ...
- 内存管理 & 内存优化技巧 浅析
内存管理 浅析 下列行为都会增加一个app的内存占用: 1.创建一个OC对象: 2.定义一个变量: 3.调用一个函数或者方法. 如果app占用内存过大,系统可能会强制关闭app,造成闪退现象,影响用户 ...
- jQuery checkbox 全选
jQuery 1.6版本以后 if($("#id").attr("checked")) 不能返回 ture 和 false 高版本中jQuery 提供prop ...
- js表单提交,判断文本框,用户名密码是否为空,JS表单检测!
当表单提交时先触发验证的js代码,当验证表单的方法返回true时才会提交表单返回false则不提交数据<script type="text/javascript">fu ...
- Win7下安装配置Java
1 安装JDK java.sun.com 下载 Java SE Development Kit 2 配置环境变量 变量名:JAVA_HOME 变量值:C:\Program Files (x86)\J ...
- HDU 2674 N!Again
一开始以为有循环节,输出了前100项发现n>40都是0.因此,水题.... #include<cstdio> ]; ; int main() { a[]=; ; i<=; i+ ...
- C++头文件#include<bits/stdc++.h>
一句话的事,直截了当——#include<bits/stdc++.h>包含C++的所有头文件 参考网站(点击):http://www.tuicool.com/articles/m6neUj ...
- 设置span的宽度
设置span的宽度 在默认的情况下,利用css样式对span进行宽度设定是无效,但有时为了某种排版的要求,需要对span进行宽度设定,那么如何在html中利用css样式设定span的宽度? 思路:这看 ...