开发者

How does Hibernate SQL generation works ? (used with Criteria API, setfirstResults and setMaxresult)

I have a question about how Hibernate generates SQL when used with the Criteria API. I have an @Entity called Mission. Each mission is linked to a Client (which the mission has been created for) and a ressource (which is the person assigned to this mission). When I query the list of all missions like this :

entityManager.getTransaction().begin();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Mission> criteria开发者_StackOverflow社区Query = criteriaBuilder.createQuery(Mission.class);
Root<Mission> mission = criteriaQuery.from(Mission.class);
...
criteriaQuery.select(mission);
TypedQuery<Mission> query = entityManager.createQuery(criteriaQuery);
missions = query.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
entityManager.getTransaction().commit();

It seems that Hibernate generates maxResults queries for each of the mission.

Hibernate: 
    select
        missionsc0_.ID_CLIENT as ID10_7_1_,
        missionsc0_.ID_MISSION as ID1_1_,
        missionsc0_.ID_MISSION as ID1_11_0_,
        missionsc0_.active as active11_0_,
        missionsc0_.ID_CLIENT as ID10_11_0_,
        missionsc0_.CODE_ARTICLE as CODE3_11_0_,
        missionsc0_.HAS_PERIODE as HAS4_11_0_,
        missionsc0_.DESCRIPTION as DESCRIPT5_11_0_,
        missionsc0_.END_DATE as END6_11_0_,
        missionsc0_.LIBELLE as LIBELLE11_0_,
        missionsc0_.ID_RESSOURCE as ID11_11_0_,
        missionsc0_.START_DATE as START8_11_0_,
        missionsc0_.VERSION as VERSION11_0_ 
    from
        MISSION missionsc0_ 
    where
        missionsc0_.ID_CLIENT=?

So I have maxResults times this query. I thought it would only fire ONE TIME with something like

SELECT * FROM Mission WHERE ... LIMIT 0, MaxResults

Can somebody explain to me why it's generating all those queries ? I suspect it to have an impact on general performances.

Thanks a lot,


What is the underlying database? Hibernate does what it can, but if the database does not implement limit, it'll have to do the legwork itself (SQL Server for example).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜