我认为我的JUnit测试会用语言更好地解释这个问题!
@Test
public void query8times(){
for(int i=0; i<15; i++){
ProspectoRadarQueryBuilder prqb = new ProspectoRadarQueryBuilder("jardeu");
List<Object[]> prospectosNotas = (List<Object[]>) genericFilterDao.executeSQL(prqb.buildQuery());
System.out.println("------------------------------------- "+i);
}
}
控制台上的结果是:
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 0
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 1
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 2
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 3
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 4
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 5
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 6
Hibernate: SELECT * FROM ( select p.id, comparestrings('jardeu', pc.valor) as nota from com_prospecto p inner join com_prospecto_campo pc ON (p.id = pc.id_prospecto) inner join com_campo c ON (pc.id_campo = c.id AND c.flag_nome = true) ) as subQuery where nota is not null AND nota > 0.35 order by nota desc;
------------------------------------- 7
现在让我们看一下源代码!
public List<?> executeSQL(String sql) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Session hibernateSession = entityManager.unwrap(Session.class);
Query q = hibernateSession.createSQLQuery(sql);
return q.list();
}
我用另一个查询做了另一个测试
@Test
public void anotherQuery(){
for(int i=0; i<15; i++){
List<Object[]> prospectosNotas = (List<Object[]>) genericFilterDao.executeSQL("select * from com_campo");
System.out.println("------------------------------------- "+i);
}
}
结果如下:
Hibernate: select * from com_campo
------------------------------------- 0
Hibernate: select * from com_campo
------------------------------------- 1
Hibernate: select * from com_campo
------------------------------------- 2
Hibernate: select * from com_campo
------------------------------------- 3
Hibernate: select * from com_campo
------------------------------------- 4
Hibernate: select * from com_campo
------------------------------------- 5
Hibernate: select * from com_campo
------------------------------------- 6
Hibernate: select * from com_campo
------------------------------------- 7
所以,我使用Spring Data ......可能是什么问题?
答案 0 :(得分:1)
你不应该只创建一个EntityManager吗?
EntityManager entityManager = entityManagerFactory.createEntityManager();
^行应该在executeSQL方法之外。
答案 1 :(得分:0)
是的,正如adarshr所说,我只创建了一个entityManager。但那不是问题。 我忘了提交交易并关闭实体经理......
这是最终结果:
public List<?> executeSQL(String sql) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Session hibernateSession = entityManager.unwrap(Session.class);
Query q = hibernateSession.createSQLQuery(sql);
List<?> list = q.list();
entityManager.getTransaction().commit();
entityManager.close();
return list;
}