0

私の 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を使用しています...何が問題なのでしょうか?

4

2 に答える 2

1

EntityManager を 1 つだけ作成するべきではありませんか?

EntityManager entityManager = entityManagerFactory.createEntityManager();

^ 行は executeSQL メソッドの外にある必要があります。

于 2013-02-19T13:10:05.937 に答える
0

ええ、adershr が言ったように、entityManager を 1 つだけ作成しました。しかし、それは問題ではありませんでした。トランザクションをコミットしてエンティティマネージャを閉じるのを忘れていました...

これが最終結果です。

 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;
}
于 2013-02-19T13:15:50.833 に答える