hibernate 1 + N 问题解决
1+N就是在hibernate中第一次查一个所需要的表的内容,他会把别的相关表的内容也查询一遍。
解决办法有三种:
1,设置LAZY。
2,借鉴createCriteria的查询语句,from Topic t left join fetch t.category c,通过join fetch来屏蔽多于查询。
3,将多次查询整成一次查询。给多于表加上BatchSize注解。这种方法并没有解决问题,只是相对来说优化了一点。
解决办法3的写法:
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)//加上BachtSize注解,如此,一次会查询五条相关内容
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;
}
}
测试类:
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();//关联关系不设lazy也不会全部查询,因为使用的是关联表方式
List<Topic> topics = (List<Topic>)session.createQuery("from Topic").list();//关联关系不设lazy会全部查询
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();
}
}
hibernate 1 + N 问题解决的更多相关文章
- org.hibernate.QueryException: duplicate alias: r hibernate别名重复问题解决
今天做项目的过程中发现,多表查询的时候如果使用hibernate的DetachedCriteria离线查询方式的时候, 在多表关联的时候我们需要使用别名的方式去实现. 但是代码运行的过程中抛出了下面的 ...
- ogn1.MethodFailedException:Method "xxx" failed for object xxx
问题描述:初学ssh写了个小项目,访问界面出现以下错误 java. lang. NoSuchllethodError: org. hi bernate. SessionF actory. openSe ...
- 使用hibernate原生sql查询,结果集全为1的问题解决
问题如下: String sqlTest ="select summary,summaryno from F_Summary"; List<Map<Object, Ob ...
- hibernate 4.3 在使用获取数据获取不到数据库中最新变更的数据问题解决
hibernate 4.3 在使用获取数据获取不到数据库中最新变更的数据问题解决,应该是因为缓存问题 问题过程和现象: 查询一个数据列表=>数据库中手动update了数据=>刷新页面,数据 ...
- hibernate添加数据,默认字段为null的问题解决
数据库中的一个字段默认为0,但是在用hibernate的添加之后,默认字段竟然不是0,为NULL. 查了一下.发现想要让默认字段生效.需要在*.hbm.xml添加一些参数,如下.(红色部分) dyna ...
- mybatis 并发问题解决,参考hibernate
时候操作同一账户就是典型的样例. 比方A.B操作员同一时候读取一剩余金额为1000元的账户,A操作员为该账户添加100元.B操作员同一时候为该账户减去 50元.A先提交.B后提交. 最后实际账户剩余金 ...
- Confluence 6 "net.sf.hibernate.PropertyValueException: not-null" 相关问题解决
如果你遇到了下面的错误信息,例如: ERROR [Importing data task] [confluence.importexport.impl.ReverseDatabinder] endEl ...
- Hibernate 自动创建表bug问题解决
我在hibernate.cfg.xml配置文件中添加了自动创建表的的属性:(这样当数据库中没有此表是,hibernate就会自动帮我们创建一张表) <property name="hb ...
- cloudera-scm-server启动时出现Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection问题解决方法(图文详解)
问题现象 查看 [root@cmbigdata1 cloudera-scm-server]# pwd /var/log/cloudera-scm-server [root@cmbigdata1 clo ...
随机推荐
- 删除Kali Linux多余的系统架构
删除Kali Linux多余的系统架构 现在大家通常安装的都是Kali Linux 64位操作系统,其架构为amd64.如果要安装运行32位的程序(如wine32),则必须添加32位架构i386. ...
- BestCoder Round #65 (ZYB's Biology)
ZYB's Biology Accepts: 848 Submissions: 1199 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 13 ...
- Heavy Transportation(POJ - 1797 变形版 dijkstra)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- POJ 1830 开关问题(Gauss 消元)
开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7726 Accepted: 3032 Description ...
- [CODECHEF]RIN
题意:一个人要在$m$个学期上$n$节课,在第$j$学期上$i$课有$X_{i,j}$的收益,有些课$B_i$有前置课程$A_i$,问最大得分 这个题我都做不出来还去看题解...我退役吧== 考虑每种 ...
- 【插头dp】CDOJ1690 这是一道比CCCC简单题难的简单题
最裸的插头dp,可参见大白书. #include<cstdio> #include<cstring> using namespace std; #define MOD 1000 ...
- 【推导】Codeforces Round #411 (Div. 1) B. Minimum number of steps
最后肯定是bbbb...aaaa...这样. 你每进行一系列替换操作,相当于把一个a移动到右侧. 会增加一些b的数量……然后你统计一下就行.式子很简单. 喵喵喵,我分段统计的,用了等比数列……感觉智障 ...
- 【FFT】BZOJ2179- FFT快速傅立叶
[题目大意] 给出n位十进制a和b,求a*b. [思路] FFT.感觉弄起来比较麻烦,不如直接背板子. 注意一下MAXN的取值,我一开始非常随意地就写了60000*2+50,其实n是要扩展到最接近的2 ...
- #Html学习积累#分割线中间添加文字
类似效果: ————————————xxxxx———————————————————— <!DOCTYPE HTML> <html> <head> <meta ...
- hdu2263Heavy Cargo
#include <iostream> #include <cstdio> #include <algorithm> #include <queue>/ ...