Java Reference
In-Depth Information
Native SQL queries
Although we mainly use object queries, native SQL queries are useful when you
need to use database-specific features to get good performance. JDO and Hiber-
nate provide an API for executing a SQL query and will construct a collection of
objects from the result set. JDO native SQL queries can return either persistent
objects or DTO s, whereas Hibernate SQL queries can return only persistent objects.
Executing JDOQL, HQL, and SQL queries
JDOQL , HQL , and SQL queries are executed using the Query interface. A JDO
application creates a query by calling a PersistenceManager method, and a Hiber-
nate application creates a query by calling a Session method. The method that
creates the query takes as a parameter either the query string or the name of the
query that is defined in the ORM document. The application can then call various
setters to define various aspects of the query. A JDO application executes the
query by calling Query.execute() , which returns a list of persistent objects or val-
ues. A Hibernate application executes a query by calling either Query.list() ,
which returns a list, or Query.scroll() , which returns a ScrollableResults that
allows the application to navigate through the underlying JDBC ResultSet .
Hibernate criteria queries
In addition to HQL and SQL queries, Hibernate has criteria queries, which are
object queries that are defined using objects rather than a textual query language.
Later in chapter 11 you will see how Hibernate criteria queries are extremely use-
ful when constructing queries dynamically since they avoid the need to write
messy code that concatenates query fragment strings.
4.3.4
Support for transactions
The fourth of the seven ORM framework features is transaction management.
Hibernate and JDO provide a Transaction interface, which allows an application
to explicitly manage transactions. A JDO application obtains a Transaction object
by calling PersistenceManager.currentTransaction() . The application begins a
transaction by calling Transaction.begin() , commits a transaction by calling
Transaction.commit() , and roll backs a transaction by calling Transaction.roll-
back() . Similarly, a Hibernate application accesses a Transaction object by calling
Session.beginTransaction() , which begins a transaction. The application can
commit a transaction by calling Transaction.commit() or roll back a transaction
by calling Transaction.rollback() . Later in chapter 7, you will see how the
 
 
 
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search