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. Python全栈之路----函数----局部变量

    全局变量是定义在函数外部以及代码的变量,全局能用. 局部变量就是定义在函数里的变量,只能在局部生效. 在函数内部,可以引用全局变量. 如果全局和局部都有一个名字相同的变量,在函数内会优先调用函数内的局 ...

  2. ionic 前端接收到后台推送来的消息后,显示在手机的通知栏上

    这里主要用到cordova提供的插件:(在app没有关闭的情况下只要有推送的消息就会有提醒,但是在app关闭的情况下就不会提示) 首先安装cordova-plugin-local-notificati ...

  3. Python开发 基礎知識 2.變量 ( *arg, **kwargs )

    變量 *args 和 **kwargs ( *和**為本體,名稱為通俗的名稱約定 ) *args 用於函式定義. 可將不定數量的參數傳遞給一個函數,傳入函式的引數,會先以Tuple物件收集,再設定給參 ...

  4. 生产redis client 链接报:ERR max number of clients reached 含义: 达到最大客户端数错误

    1.通过netstat 命令查看TCP又11822个连接  (netstat命令是一个监控TCP/IP网络的非常有用的工具) 2.默认redis最大的连接数10000 ,但是此时无法连接redis客户 ...

  5. 使用outflux 导入influxdb 的数据到timescaledb

    influxdb 以及timescaledb 都是不错的时序数据库,timescaledb 团队提供了直接从influxdb 导入 环境准备 docker-compose 文件 version: &q ...

  6. 记一次php脚本memory exhausted

    表象报错如下: Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16651985 bytes) 出 ...

  7. 第一个Python小项目:图片转换成字符图片

    实现的效果:                                                                                               ...

  8. mysql悲观锁

    悲观锁与乐观锁是两种常见的资源并发锁设计思路,也是并发编程中一个非常基础的概念. 悲观锁(Pessimistic Lock) 悲观锁的特点是先获取锁,再进行业务操作,即“悲观”的认为获取锁是非常有可能 ...

  9. 学会数据库读写分离、分表分库——用Mycat

    系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...

  10. 使用pageoffice进行多个文档的合并

    提前给test模板文件中 手动插入一个书签,因为pageoffice必须有一个书签后,才能在后台进行书签的创建 //多个word文件进行合并 string strCopyFolder = System ...