2013-02-19 70 views
0

我认为我的JUnit测试会比我用单词更好地解释这个问题!Hibernate只执行查询8次

@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 

所以,I'm使用Spring数据...可能是什么问题?

+2

你是如何运行你的测试?我不能相信你原来的查询被命名为query8times是巧合。让我相信你可能有一个测试类缓存在某个地方。 – Perception 2013-02-19 13:09:31

+0

为了确保,使它打印“Testing”+ n +“times”并在for循环中使用* n *。 – 2013-02-19 13:10:50

+0

我怀疑你正在使用一个在更改'query8times'方法之前编译的类文件。 – 2013-02-19 13:32:15

回答

1

你不应该只创建一个EntityManager吗?

EntityManager entityManager = entityManagerFactory.createEntityManager(); 

^行应该在executeSQL方法之外。

0

是的,我只创建了一个entityManager,就像adarshr说的那样。但那不是问题。 我忘了提交事务,关闭实体管理器...

这是最后的结果是:

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

您应该将其作为评论或编辑原始问题发布,因为这不是答案。 – 2013-02-19 13:54:58