HHH-16973 document optional 'from' clause and use it in @HQL examples
This commit is contained in:
parent
5b2d347b6f
commit
09dd5daa73
|
@ -189,7 +189,7 @@ We'll need a place to put this method, and since our `Book` entity isn't an abst
|
|||
[source,java]
|
||||
----
|
||||
interface Queries {
|
||||
@HQL("from Book where title like :title and type = :type")
|
||||
@HQL("where title like :title and type = :type")
|
||||
List<Book> findBooksByTitleAndType(String title, String type);
|
||||
}
|
||||
----
|
||||
|
@ -216,7 +216,7 @@ public abstract class Queries_ {
|
|||
}
|
||||
|
||||
static final String FIND_BOOKS_BY_TITLE_AND_TYPE_String_Type =
|
||||
"from Book where title like :title and type = :type";
|
||||
"where title like :title and type = :type";
|
||||
|
||||
}
|
||||
|
||||
|
@ -229,7 +229,7 @@ If we want to explicitly specify the name and type of this parameter, we may dec
|
|||
[source,java]
|
||||
----
|
||||
interface Queries {
|
||||
@HQL("from Book where title like :title and type = :type")
|
||||
@HQL("where title like :title and type = :type")
|
||||
List<Book> findBooksByTitleAndType(StatelessSession session, String title, String type);
|
||||
}
|
||||
----
|
||||
|
@ -277,7 +277,7 @@ We may call this method anything we like:
|
|||
interface Queries {
|
||||
EntityManager entityManager();
|
||||
|
||||
@HQL("from Book where title like :title and type = :type")
|
||||
@HQL("where title like :title and type = :type")
|
||||
List<Book> findBooksByTitleAndType(String title, String type);
|
||||
}
|
||||
----
|
||||
|
@ -316,7 +316,7 @@ public class Queries_ implements Queries {
|
|||
}
|
||||
|
||||
static final String FIND_BOOKS_BY_TITLE_AND_TYPE_String_Type =
|
||||
"from Book where title like :title and type = :type";
|
||||
"where title like :title and type = :type";
|
||||
|
||||
}
|
||||
----
|
||||
|
@ -342,7 +342,7 @@ interface Queries {
|
|||
// handwritten method replacing previous generated implementation
|
||||
default List<Book> findBooksByTitleAndType(String title, String type) {
|
||||
entityManager()
|
||||
.createQuery("from Book where title like :title and type = :type", Book.class)
|
||||
.createQuery("where title like :title and type = :type", Book.class)
|
||||
.setParameter("title", title)
|
||||
.setParameter("type", type)
|
||||
.setFlushMode(COMMIT)
|
||||
|
@ -558,7 +558,7 @@ It might return a single `Book`.
|
|||
|
||||
[source,java]
|
||||
----
|
||||
@HQL("from Book where isbn = :isbn")
|
||||
@HQL("where isbn = :isbn")
|
||||
Book findBookByIsbn(String isbn);
|
||||
----
|
||||
|
||||
|
@ -592,7 +592,7 @@ A query method might even return `TypedQuery` or `SelectionQuery`:
|
|||
|
||||
[source,java]
|
||||
----
|
||||
@HQL("from Book where title like :title")
|
||||
@HQL("where title like :title")
|
||||
SelectionQuery<Book> findBooksByTitle(String title);
|
||||
----
|
||||
|
||||
|
|
|
@ -554,7 +554,7 @@ Suppose we simplify `Queries` to just the following:
|
|||
[source,java]
|
||||
----
|
||||
interface Queries {
|
||||
@HQL("from Book where title like :title order by title")
|
||||
@HQL("where title like :title order by title")
|
||||
List<Book> findBooksByTitleWithPagination(String title, Page page);
|
||||
}
|
||||
----
|
||||
|
|
|
@ -489,7 +489,7 @@ But when there's no explicit `select` clause, the select list is implied by the
|
|||
List<Book> books =
|
||||
session.createQuery("from Book join authors", Book.class)
|
||||
.getResultList();
|
||||
for (Person person: persons) {
|
||||
for (Book book: books) {
|
||||
...
|
||||
}
|
||||
----
|
||||
|
@ -546,6 +546,23 @@ HQL makes it very easy to fetch all the data we need in a single trip to the dat
|
|||
Typically, it's much worse to fetch exactly the data we need, but in many round trips to the database server, than it is to fetch just a bit more data than what we're going to need, all a single SQL query.
|
||||
====
|
||||
|
||||
When there's no explicit `select` clause, a further abbreviation is sometimes possible.
|
||||
|
||||
[TIP]
|
||||
====
|
||||
When the result type of a `select` query is an entity type, and we specify the type explicitly by passing the entity class to `createQuery()` or `createSelectionQuery()`, we're sometimes allowed to omit the `from` clause, for example:
|
||||
|
||||
[source, java]
|
||||
[%unbreakable]
|
||||
----
|
||||
// explicit result type Book, so 'from Book' is inferred
|
||||
List<Book> books =
|
||||
session.createQuery("where title like :title", Book.class)
|
||||
.setParameter("title", title)
|
||||
.getResultList();
|
||||
----
|
||||
====
|
||||
|
||||
[[returning-to-java]]
|
||||
=== Representing result sets in Java
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ orderedQuery
|
|||
query
|
||||
: selectClause fromClause? whereClause? (groupByClause havingClause?)?
|
||||
| fromClause whereClause? (groupByClause havingClause?)? selectClause?
|
||||
| whereClause
|
||||
|
||||
queryOrder
|
||||
: orderByClause limitClause? offsetClause? fetchClause?
|
||||
|
|
Loading…
Reference in New Issue