Retrieving Objects - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Retrieving Objects

Description:

Retrieving Objects. Hibernate's powerful query facilities allow ... u.email = hvdossani_at_rogers.com' ... Expression.eq('email', 'hvdossani_at_rogers.com' ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 7
Provided by: hvdos
Category:

less

Transcript and Presenter's Notes

Title: Retrieving Objects


1
Retrieving Objects
  • Hibernates powerful query facilities allow you
    to express almost everything you commonly (or
    even uncommonly) need to express in SQL, but in
    object oriented terms- using classes and
    properties of classes.
  • The Query and Criteria interface both define
    several methods for controlling execution of a
    query. Query interface provides methods for
    binding concrete values to query parameters.
  • The Query interface
  • To create a new Query instance, call either
    createQuery() or createSQLQuery()
  • Query hqlQuery session.createQuery(from
    User)
  • The createSQLQuery() is used to create a SQL
    query using the native syntax of underlying
    database
  • Query sqlQuery session.createSQLQuery(select
    u. from USERSu, u, User.class)
  • The Criteria interface
  • To obtain a Criteria instance, call
    createCriteria() passing the class of the objects
    you want query to return.
  • Criteria criteria session.createCriteria(User.cl
    ass)
  • Criteria instance is also used to construct the
    object-oriented representation of the query by
    adding Criterion instances and navigating
    associations to new Criterias
  • Query and Criteria both support pagination of the
    query result

2
Retrieving Objects
  • Listing and iterating results
  • The list() method executes the query and returns
    the results as a list
  • List results session.createQuery(from
    User).list()
  • In case of only a single instance result, we can
    use results.get(0) or setMaxResults(1) and then
    execute the query with uniqueResult()
  • Order maxAmtOrder (Order) createQuery(from
    Order o order by o.amount desc).setMaxResults(1).
    uniqueResult()
  • If the query returns more than one object, an
    exception is thrown
  • Binding Parameters
  • Hibernate supports positional parameters
    (indicated by ?) as well as named parameters
    (indicated by prefix)
  • Using named parameters
  • String queryString from User u where u.fname
    like searchString
  • List result session.createQuery(queryString).set
    String(searchString, searchString ).list()
  • searchString is a user supplied string variable.
  • Using positional parameters
  • String queryString from User u where u.fname
    like ?
  • List result session.createQuery(queryString).set
    String(0, searchString ).list()
  • Every change of the position of the bind
    parameters requires a change to the parameter
    binding code.
  • setEntity() method binds a persistent entity
  • session.createQuery (from User u where u.order
    order).setEntity(order, order).list()

3
Retrieving Objects
  • Using named queries
  • Hibernate lets you externalize query strings to
    the mapping metadata, a technique that is called
    named queries
  • All queries related to a persistent class (or a
    set of classes), is encapsulated with other
    metadata of that class in an XML mapping file.
    The name of the query is used to call it from the
    application
  • The getNamedQuery() method obtains a Query
    instance for a named query
  • Query query session.getNamedQuery("findManufactu
    rersByMainType").setInteger("typeid", typeID)
  • The named query is defined in mapping metadata
    using the ltquerygt element
  • ltquery name"findManufacturersByMainType"gt
  • lt!CDATA
  • select m from Manufacturer m join
    m.manufacturerTypes mt where mt.typeid typeid
  • gt
  • lt/querygt
  • Named queries dont have to be HQL strings, they
    might even be native SQL queries.

4
Hibernate Query Language
  • Simplest Query
  • The simplest query retrieves all instances of a
    particular persistent class. In HQL, it looks
    like
  • from User
  • Using Criteria query, it looks like
  • session.createCriteria(User.class)
  • Using aliases
  • Usually an alias is assigned to the queried class
    to use as a reference in other parts of the
    query
  • from User as user
  • The as keyword is optional
  • Restrictions
  • Express constraints on the property values of
    objects returned by the query
  • From User u where u.email hvdossani_at_rogers.com
  • Constraint is expressed in terms of property of
    the persistent class
  • For a Criteria query, we must construct Criterion
    object to express the constraint. The Expression
    class provides factory methods for built-in
    Criterion types
  • Criterion emailEq Expression.eq(email,
    hvdossani_at_rogers.com)
  • Criteria criteria session.createCriteria(User.cl
    ass)
  • criteria.add(emailEq)
  • User user (User)criteria.uniqueResult()

5
Hibernate Query Language
  • Comparison Operators
  • HQL supports the same basic operators as SQL.
  • In the case of criteria queries, all the same
    operators are available via the Expression class.
  • String matching
  • String matching is provided by wildcard symbols
    like and _
  • Hibernate provides MatchMode to query string
    starting, ending, anywhere and exact matching
    expressions.
  • HQL also provides the ability to call arbitrary
    SQL functions in the where clause
  • Ordering Query results
  • HQL provides the order by clause similar to SQL
  • from User u order by u.fname
  • Ascending or descending order could be specified
    using asc or desc
  • from User u order by u.fname asc
  • The Criteria API also provides a similar
    functionality
  • Session.createCriteria(User.class).addOrder(Order.
    asc(fname))

6
Joining Associations
  • Refer to the code implementation
Write a Comment
User Comments (0)
About PowerShow.com