gpt4 book ai didi

java - Hibernate只执行8次查询

转载 作者:行者123 更新时间:2023-12-01 14:50:15 25 4
gpt4 key购买 nike

我认为我的 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...可能有什么问题?

最佳答案

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

EntityManager entityManager = entityManagerFactory.createEntityManager();

^ 行应该位于executeSQL 方法之外。

关于java - Hibernate只执行8次查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14958283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com