[b]Hibernate version: 3.2.0.cr4[/b]
[b]Mapping documents: None - using JPA implementation[/b]
[b]Code between sessionFactory.openSession() and session.close():[/b]
I am using the following named query:
@NamedQuery(name=Country.COUNTRIES_AS_SELECT_ITEMS,
query="select new javax.faces.model.SelectItem(c, c.name) from Country c")
Which is called from the following method:
public List<SelectItem> getAllCountriesAsSelectItems() {
return entityManager.createNamedQuery(Country.COUNTRIES_AS_SELECT_ITEMS).getResultList();
}
[b]Name and version of the database you are using:Oracle 9.2 and HSQLDB 1.7.3.3 (same results with either database)[/b]
[b]The generated SQL (show_sql=true):[/b]
Hibernate: select country0_.id as col_0_0_, country0_.name as col_1_0_ from Country country0_
Hibernate: select country0_.id as id3_0_, country0_.name as name3_0_ from Country country0_ where country0_.id=?
Hibernate: select country0_.id as id3_0_, country0_.name as name3_0_ from Country country0_ where country0_.id=?
Hibernate: select country0_.id as id3_0_, country0_.name as name3_0_ from Country country0_ where country0_.id=?
Hibernate: select country0_.id as id3_0_, country0_.name as name3_0_ from Country country0_ where country0_.id=?
Hibernate: select country0_.id as id3_0_, country0_.name as name3_0_ from Country country0_ where country0_.id=?
Hibernate: select country0_.id as col_0_0_, country0_.name as col_1_0_ from Country country0_
[b]Comments:[/b]
Assuming that we have 5 countries, calling the method above would result in 7 queries. The 7th is just due to the JavaServer Faces lifecycle, and so I am ignoring that. The first query of course gets all of the ids, and the next 5 queries retrieve the objects by id.
However, if we change the query to:
"select new javax.faces.model.SelectItem(c.name) from Country c" and only select an attribute of country, instead of an attribute of the country and the country entity as well, it will only issue 1 query (or two, due to the JSF lifecycle).
As per issue HHH-544 Select new n+1 queries, Gavin responded that the parser treats all select new calls as shallow, as is manifest in the above example.
[b]What I am wondering is if there is a way to make select new calls non-shallow (avoiding the n+1 database queries), or if it would be a valid feature request?[/b]
|