Sessionclear

否则session缓存里越来越多

Java有内存泄露吗?

在语法中没有(垃圾自动回收) 但是在实际中会有 比如读文件没有关什么的

1+N问题

解决方法:把fetch设置为lazy 什么时候就为空

Bachsize 只是缓解

使用joinfetch

Category

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;
}
}

Msg

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;
} }

Msginfo

package com.bjsxt.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne; public class MsgInfo {
private int id;
private String cont;
private String topicName;
private String categoryName;
public MsgInfo(int id, String cont, String topicName, String categoryName) {
super();
this.id = id;
this.cont = cont;
this.topicName = topicName;
this.categoryName = categoryName;
}
public String getTopicName() {
return topicName;
}
public void setTopicName(String topicName) {
this.topicName = topicName;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
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;
} }

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;
} }

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


list是马上取出来 而iterator是在用的时候才取 不占缓存  

Category

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;
}
}

Msg

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;
} }

Msginfo

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne; public class MsgInfo {
private int id;
private String cont;
private String topicName;
private String categoryName;
public MsgInfo(int id, String cont, String topicName, String categoryName) {
super();
this.id = id;
this.cont = cont;
this.topicName = topicName;
this.categoryName = categoryName;
}
public String getTopicName() {
return topicName;
}
public void setTopicName(String topicName) {
this.topicName = topicName;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
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;
} }

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;
} }
    @Test
public void testQueryList() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list(); //发一次select
List<Category> categories = (List<Category>)session.createQuery("from Category").list(); //找出来 但是不查找 for(Category c : categories) {
System.out.println(c.getName()); //用的时候才发select
} List<Category> categories2 = (List<Category>)session.createQuery("from Category").list(); //再一次faselect
for(Category c : categories2) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close(); } //join fetch
@Test
public void testQueryIterate() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
Iterator<Category> categories = (Iterator<Category>)session.createQuery("from Category").iterate(); while(categories.hasNext()) {
Category c = categories.next();
System.out.println(c.getName());
} Iterator<Category> categories2 = (Iterator<Category>)session.createQuery("from Category").iterate(); //从session缓存里面找 while(categories2.hasNext()) {
Category c = categories2.next();
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close(); }

Hibernate 再接触 性能优化的更多相关文章

  1. Hibernate 再接触 悲观锁和乐观锁

    为什么取1248 二进制 CRUD 移位效率高 在并发和效率选择一个平衡点 一般不会考虑幻读 因为我们不会再一个事务里查询两次,(只能设置为seralizable) 悲观锁和乐观锁的前提是read-u ...

  2. Hibernate 再接触 一级缓存 二级缓存 查询缓存

    缓存 就是把本来应该放在硬盘里的东西放在内存里  将来存内存里读 一级缓存: session缓存 二级缓存: sessionFactory级别的   (适合经常访问,数据量有限,改动不大) 很多的se ...

  3. Hibernate 再接触 CRUD

    1.save 一对多双向 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import jav ...

  4. Hibernate 再接触 核心开发接口

    1.可以重载方法进行配置文件的指定 sessionFactory = new AnnotationConfiguration().configure("hibernate.xml" ...

  5. Hibernate 再接触 ID生成策略

    Xml 方法 在student.hbm.xml中 <generator class="uuid"></generator> 取值如下 1.identity: ...

  6. Hibernate 再接触 HQL

    Category.java package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence ...

  7. Hibernate 再接触 树状结构设计以及学生课程成绩表的设计

    1 树状结构的设计 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax. ...

  8. Hibernate 再接触 继承映射

    用一张 每一个类一张表 建立外键 第一种 一张总表 Person package com.bjsxt.hibernate; import javax.persistence.Discriminator ...

  9. Hibernate 再接触 集合映射

    不太重要 List 用于排序 Map  key一般是user的某个字段(多半是主键 integer) package com.bjsxt.hibernate; import java.util.Has ...

随机推荐

  1. componentWillMount和componentDidMount的区别

    1.componentWillMount  将要装载,在render之前调用: componentDidMount,(装载完成),在render之后调用 2.componentWillMount  每 ...

  2. requests模块的使用

    requests模块 什么是request模块:requests是python原生一个基于网络请求的模块,模拟浏览器发起请求. requests-get请求 # get请求 import reques ...

  3. Dubbo的Filter实战--整合Oval校验框架

    前言: 其实很早之前就想写一篇关于oval和具体服务相整合的常见做法, 并以此作为一篇笔记. 趁现在项目中间空闲期, 刚好对dubbo的filter有一些了解. 因此想结合两者, 写一下既结合校验框架 ...

  4. 【转】使用Mybatis时遇到的延迟加载造成返回异常的问题——HttpMessageConversionException: Type definition error

    在使用Mybatis的过程中,使用了resultMap延迟加载. 延迟加载:association联表查询的过程中,查询另外两个表的对象.而延迟加载是指只有在使用这两个对象的时候才会进行查询. 问题的 ...

  5. supervisord.conf

    ; Sample supervisor config file.;; For more information on the config file, please see:; http://supe ...

  6. python 多协程异步IO爬取网页加速3倍。

    from urllib import request import gevent,time from gevent import monkey#该模块让当前程序所有io操作单独标记,进行异步操作. m ...

  7. IndentationError:expected an indented block错误解决

    Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...

  8. golang web framework--Martini

    Martini是一个功能强大的软件包,用于在Golang中快速编写模块化Web应用程序/服务. 下载 $ go get github.com/go-martini/martini Demo serve ...

  9. axiso实战问题

    this.axios({ method: 'get', url: '/api/projectmgt/project/Project/list', withCredentials : true, hea ...

  10. Spring boot 配置 mybatis xml和动态SQL 分页配置

    更新时间 2018年4月30日23:27:07 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...