Hibernate Query API
API of org.hibernate.query.Query<T>
org.hibernate.query.Query<T>
This interface is specified in
javax.persistence
specification
Iterator iterate() throws HibernateException
This api is deprecated since
5.2
versionReturn the query results as an Iterator.
If the query contains multiple results per row, the results are returned Object[].
Entities are returned in lazy manner.
Read more here for difference between
list()
anditerator()
.
Execute query to get List of selected PERSISTENT POJOs
API of
org.hibernate.query.Query
interface, taken fromjavax.persistence.TypedQuery<T>
.Execute a
SELECT
query and return the query results as a genericList<T>
.T is the type of POJO/Result.
Passing IN parameters to query & execute it.
Objective : Display all books from specified author, with price less than the specified price.
API from
org.hibernate.query.Query
interfaceBind a named query parameter using its inferred Type.
name : query parameter name
value : parameter value
How to set
IN
parameters ?org.hibernate.query.Query<T>
APIpublic Query<T> setParameter(String pName, Object val);
Example,
Updating POJOs
Can be done either with select, followed by update or ONLY with update queries. (following is eg of 2nd option commonly known as bulk update).
Objective : Decrease price of all books with author=specified author.
This approach is typically not recommended often, since it bypasses L1 cache. Interesting read here
Cascading is not supported. Doesn't support optimistic locking directly.
Use case, for bulk updations to be performed on a standalone unrelated table.
Pagination API
Query setMaxResults(int maxResults)
Set the maximum number of rows to retrieve.
If not set, there is no limit to the number of rows retrieved.
Query setFirstResult(int firstResult)
Set the first row to retrieve.
If not set, rows will be retrieved beginnning from row 0. (NOTE row num starts from 0).
How to count rows & use it in pagination techniques?
<T> T getSingleResult()
Executes a
SELECT
query that returns a single result.Returns a single instance (persistent object) that matches the query.
It throws:
NoResultException, if there is no result.
NonUniqueResultException, if more than one result.
IllegalStateException, if called for a JPQL
UPDATE
orDELETE
statement.
How to get
Scrollable
Result from Query?ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException
The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.
Return the query results as ScrollableResults. Then, one can use methods of ScrollableResults
first(), next(), last(), scroll(n)
.
How to create
NamedQuery
from Session interface?What is a named query ?
It's a technique to group the
HQL
statements in single location (typically in POJOS) and lately refer them by some name whenever need to use them. It helps largely in code cleanup because these HQL statements are no longer scattered in whole code.
It is Fail fast: Their syntax is checked when the session factory is created, making the application fail fast in case of an error.
Reusable: They can be accessed and used from several places which increases re-usability.
Example, In POJO class, at class level, one can declare Named queries.
How to invoke native sql from hibernate?
Hibernate Criteria API
A powerful and elegent alternative to
HQL
. Well adapted for dynamic search functionalities where complex Hibernate queries have to be generated 'on-the-fly'.Typical steps are,
Create a criteria for POJO
Add restrictions, projections, add order
Fire query (via list() or uniqueResult())
Composite primary key
Use annotation
@Embeddable
(NOT @Entity).Must be Serializable.
Must implement
hashCode()
&equals()
as per general contract.In owning Entity class add usual annotation
@Id
.
Last updated