alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (session_api.xml)

This example Hibernate source code file (session_api.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

cat, cat, domesticcat, hibernate, hibernate, java, java, list, query, sql, the, this, this, you

The Hibernate session_api.xml source code

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
  ~ indicated by the @author tags or express copyright attribution
  ~ statements applied by the authors.  All third-party contributions are
  ~ distributed under license by Red Hat Middleware LLC.
  ~
  ~ This copyrighted material is made available to anyone wishing to use, modify,
  ~ copy, or redistribute it subject to the terms and conditions of the GNU
  ~ Lesser General Public License, as published by the Free Software Foundation.
  ~
  ~ This program is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "../HIBERNATE_-_Relational_Persistence_for_Idiomatic_Java.ent">
%BOOK_ENTITIES;
]>
<chapter id="objectstate">
  <title>Working with objects

  <para>Hibernate is a full object/relational mapping solution that not only
  shields the developer from the details of the underlying database management
  system, but also offers <emphasis>state management of objects.
  This is, contrary to the management of SQL <literal>statements in
  common JDBC/SQL persistence layers, a natural object-oriented view of
  persistence in Java applications.</para>

  <para>In other words, Hibernate application developers should always think
  about the <emphasis>state of their objects, and not necessarily
  about the execution of SQL statements. This part is taken care of by
  Hibernate and is only relevant for the application developer when tuning the
  performance of the system.</para>

  <section id="objectstate-overview">
    <title>Hibernate object states

    <para>Hibernate defines and supports the following object states:

    <itemizedlist>
      <listitem>
        <para>Transient - an object is transient if it
        has just been instantiated using the <literal>new operator,
        and it is not associated with a Hibernate <literal>Session.
        It has no persistent representation in the database and no identifier
        value has been assigned. Transient instances will be destroyed by the
        garbage collector if the application does not hold a reference
        anymore. Use the Hibernate <literal>Session to make an
        object persistent (and let Hibernate take care of the SQL statements
        that need to be executed for this transition).</para>
      </listitem>

      <listitem>
        <para>Persistent - a persistent instance has a
        representation in the database and an identifier value. It might just
        have been saved or loaded, however, it is by definition in the scope
        of a <literal>Session. Hibernate will detect any changes
        made to an object in persistent state and synchronize the state with
        the database when the unit of work completes. Developers do not
        execute manual <literal>UPDATE statements, or
        <literal>DELETE statements when an object should be made
        transient.</para>
      </listitem>

      <listitem>
        <para>Detached - a detached instance is an object
        that has been persistent, but its <literal>Session has been
        closed. The reference to the object is still valid, of course, and the
        detached instance might even be modified in this state. A detached
        instance can be reattached to a new <literal>Session at a
        later point in time, making it (and all the modifications) persistent
        again. This feature enables a programming model for long running units
        of work that require user think-time. We call them
        <emphasis>application transactions, i.e., a unit of work
        from the point of view of the user.</para>
      </listitem>
    </itemizedlist>

    <para>We will now discuss the states and state transitions (and the
    Hibernate methods that trigger a transition) in more detail.</para>
  </section>

  <section id="objectstate-makingpersistent" revision="1">
    <title>Making objects persistent

    <para>Newly instantiated instances of a persistent class are considered
    <emphasis>transient by Hibernate. We can make a transient
    instance <emphasis>persistent by associating it with a
    session:</para>

    <programlisting role="JAVA">DomesticCat fritz = new DomesticCat();
fritz.setColor(Color.GINGER);
fritz.setSex('M');
fritz.setName("Fritz");
Long generatedId = (Long) sess.save(fritz);</programlisting>

    <para>If Cat has a generated identifier, the identifier
    is generated and assigned to the <literal>cat when
    <literal>save() is called. If Cat has an
    <literal>assigned identifier, or a composite key, the identifier
    should be assigned to the <literal>cat instance before calling
    <literal>save(). You can also use persist()
    instead of <literal>save(), with the semantics defined in the
    EJB3 early draft.</para>

    <itemizedlist spacing="compact">
      <listitem>
        <para>persist() makes a transient instance
        persistent. However, it does not guarantee that the identifier value
        will be assigned to the persistent instance immediately, the
        assignment might happen at flush time. <literal>persist()
        also guarantees that it will not execute an <literal>INSERT
        statement if it is called outside of transaction boundaries. This is
        useful in long-running conversations with an extended
        Session/persistence context.</para>
      </listitem>

      <listitem>
        <para>save() does guarantee to return an
        identifier. If an INSERT has to be executed to get the identifier (
        e.g. "identity" generator, not "sequence"), this INSERT happens
        immediately, no matter if you are inside or outside of a transaction.
        This is problematic in a long-running conversation with an extended
        Session/persistence context.</para>
      </listitem>
    </itemizedlist>

    <para>Alternatively, you can assign the identifier using an overloaded
    version of <literal>save().

    <programlisting role="JAVA">DomesticCat pk = new DomesticCat();
pk.setColor(Color.TABBY);
pk.setSex('F');
pk.setName("PK");
pk.setKittens( new HashSet() );
pk.addKitten(fritz);
sess.save( pk, new Long(1234) );</programlisting>

    <para>If the object you make persistent has associated objects (e.g. the
    <literal>kittens collection in the previous example), these
    objects can be made persistent in any order you like unless you have a
    <literal>NOT NULL constraint upon a foreign key column. There is
    never a risk of violating foreign key constraints. However, you might
    violate a <literal>NOT NULL constraint if you
    <literal>save() the objects in the wrong order.

    <para>Usually you do not bother with this detail, as you will normally use
    Hibernate's <emphasis>transitive persistence feature to save
    the associated objects automatically. Then, even <literal>NOT
    NULL</literal> constraint violations do not occur - Hibernate will take
    care of everything. Transitive persistence is discussed later in this
    chapter.</para>
  </section>

  <section id="objectstate-loading">
    <title>Loading an object

    <para>The load() methods of Session
    provide a way of retrieving a persistent instance if you know its
    identifier. <literal>load() takes a class object and loads the
    state into a newly instantiated instance of that class in a persistent
    state.</para>

    <programlisting role="JAVA">Cat fritz = (Cat) sess.load(Cat.class, generatedId);

    <programlisting role="JAVA">// you need to wrap primitive identifiers
long id = 1234;
DomesticCat pk = (DomesticCat) sess.load( DomesticCat.class, new Long(id) );</programlisting>

    <para>Alternatively, you can load state into a given instance:

    <programlisting role="JAVA">Cat cat = new DomesticCat();
// load pk's state into cat
sess.load( cat, new Long(pkId) );
Set kittens = cat.getKittens();</programlisting>

    <para>Be aware that load() will throw an unrecoverable
    exception if there is no matching database row. If the class is mapped
    with a proxy, <literal>load() just returns an uninitialized
    proxy and does not actually hit the database until you invoke a method of
    the proxy. This is useful if you wish to create an association to an
    object without actually loading it from the database. It also allows
    multiple instances to be loaded as a batch if
    <literal>batch-size is defined for the class mapping.

    <para>If you are not certain that a matching row exists, you should use
    the <literal>get() method which hits the database immediately
    and returns null if there is no matching row.</para>

    <programlisting role="JAVA">Cat cat = (Cat) sess.get(Cat.class, id);
if (cat==null) {
    cat = new Cat();
    sess.save(cat, id);
}
return cat;</programlisting>

    <para>You can even load an object using an SQL SELECT ... FOR
    UPDATE</literal>, using a LockMode. See the API
    documentation for more information.</para>

    <programlisting role="JAVA">Cat cat = (Cat) sess.get(Cat.class, id, LockMode.UPGRADE);

    <para>Any associated instances or contained collections will
    <emphasis>not be selected FOR UPDATE, unless
    you decide to specify <literal>lock or all as
    a cascade style for the association.</para>

    <para>It is possible to re-load an object and all its collections at any
    time, using the <literal>refresh() method. This is useful when
    database triggers are used to initialize some of the properties of the
    object.</para>

    <programlisting role="JAVA">sess.save(cat);
sess.flush(); //force the SQL INSERT
sess.refresh(cat); //re-read the state (after the trigger executes)</programlisting>

    <para>How much does Hibernate load from the database and how many SQL
    <literal>SELECTs will it use? This depends on the
    <emphasis>fetching strategy. This is explained in 
  </section>

  <section id="objectstate-querying" revision="1">
    <title>Querying

    <para>If you do not know the identifiers of the objects you are looking
    for, you need a query. Hibernate supports an easy-to-use but powerful
    object oriented query language (HQL). For programmatic query creation,
    Hibernate supports a sophisticated Criteria and Example query feature (QBC
    and QBE). You can also express your query in the native SQL of your
    database, with optional support from Hibernate for result set conversion
    into objects.</para>

    <section id="objectstate-querying-executing" revision="1">
      <title>Executing queries

      <para>HQL and native SQL queries are represented with an instance of
      <literal>org.hibernate.Query. This interface offers methods
      for parameter binding, result set handling, and for the execution of the
      actual query. You always obtain a <literal>Query using the
      current <literal>Session:

      <programlisting role="JAVA">List cats = session.createQuery(
    "from Cat as cat where cat.birthdate < ?")
    .setDate(0, date)
    .list();

List mothers = session.createQuery(
    "select mother from Cat as cat join cat.mother as mother where cat.name = ?")
    .setString(0, name)
    .list();

List kittens = session.createQuery(
    "from Cat as cat where cat.mother = ?")
    .setEntity(0, pk)
    .list();

Cat mother = (Cat) session.createQuery(
    "select cat.mother from Cat as cat where cat = ?")
    .setEntity(0, izi)
    .uniqueResult();]]

Query mothersWithKittens = (Cat) session.createQuery(
    "select mother from Cat as mother left join fetch mother.kittens");
Set uniqueMothers = new HashSet(mothersWithKittens.list());</programlisting>

      <para>A query is usually executed by invoking list().
      The result of the query will be loaded completely into a collection in
      memory. Entity instances retrieved by a query are in a persistent state.
      The <literal>uniqueResult() method offers a shortcut if you
      know your query will only return a single object. Queries that make use
      of eager fetching of collections usually return duplicates of the root
      objects, but with their collections initialized. You can filter these
      duplicates through a <literal>Set.

      <section id="objectstate-querying-executing-iterate">
        <title>Iterating results

        <para>Occasionally, you might be able to achieve better performance by
        executing the query using the <literal>iterate() method.
        This will usually be the case if you expect that the actual entity
        instances returned by the query will already be in the session or
        second-level cache. If they are not already cached,
        <literal>iterate() will be slower than
        <literal>list() and might require many database hits for a
        simple query, usually <emphasis>1 for the initial select
        which only returns identifiers, and <emphasis>n additional
        selects to initialize the actual instances.</para>

        <programlisting role="JAVA">// fetch ids
Iterator iter = sess.createQuery("from eg.Qux q order by q.likeliness").iterate();
while ( iter.hasNext() ) {
    Qux qux = (Qux) iter.next();  // fetch the object
    // something we couldnt express in the query
    if ( qux.calculateComplicatedAlgorithm() ) {
        // delete the current instance
        iter.remove();
        // dont need to process the rest
        break;
    }
}</programlisting>
      </section>

      <section id="objectstate-querying-executing-tuples">
        <title>Queries that return tuples

        <para>Hibernate queries sometimes return tuples of objects. Each tuple
        is returned as an array:</para>

        <programlisting role="JAVA">Iterator kittensAndMothers = sess.createQuery(
            "select kitten, mother from Cat kitten join kitten.mother mother")
            .list()
            .iterator();

while ( kittensAndMothers.hasNext() ) {
    Object[] tuple = (Object[]) kittensAndMothers.next();
    Cat kitten = (Cat) tuple[0];
    Cat mother = (Cat) tuple[1];
    ....
}</programlisting>
      </section>

      <section id="objectstate-querying-executing-scalar" revision="1">
        <title>Scalar results

        <para>Queries can specify a property of a class in the
        <literal>select clause. They can even call SQL aggregate
        functions. Properties or aggregates are considered "scalar" results
        and not entities in persistent state.</para>

        <programlisting role="JAVA">Iterator results = sess.createQuery(
        "select cat.color, min(cat.birthdate), count(cat) from Cat cat " +
        "group by cat.color")
        .list()
        .iterator();

while ( results.hasNext() ) {
    Object[] row = (Object[]) results.next();
    Color type = (Color) row[0];
    Date oldest = (Date) row[1];
    Integer count = (Integer) row[2];
    .....
}</programlisting>
      </section>

      <section id="objectstate-querying-executing-parameters">
        <title>Bind parameters

        <para>Methods on Query are provided for binding
        values to named parameters or JDBC-style <literal>?
        parameters. <emphasis>Contrary to JDBC, Hibernate numbers parameters
        from zero.</emphasis> Named parameters are identifiers of the form
        <literal>:name in the query string. The advantages of named
        parameters are as follows:</para>

        <itemizedlist spacing="compact">
          <listitem>
            <para>named parameters are insensitive to the order they occur in
            the query string</para>
          </listitem>

          <listitem>
            <para>they can occur multiple times in the same query
          </listitem>

          <listitem>
            <para>they are self-documenting
          </listitem>
        </itemizedlist>

        <programlisting role="JAVA">//named parameter (preferred)
Query q = sess.createQuery("from DomesticCat cat where cat.name = :name");
q.setString("name", "Fritz");
Iterator cats = q.iterate();</programlisting>

        <programlisting role="JAVA">//positional parameter
Query q = sess.createQuery("from DomesticCat cat where cat.name = ?");
q.setString(0, "Izi");
Iterator cats = q.iterate();</programlisting>

        <programlisting role="JAVA">//named parameter list
List names = new ArrayList();
names.add("Izi");
names.add("Fritz");
Query q = sess.createQuery("from DomesticCat cat where cat.name in (:namesList)");
q.setParameterList("namesList", names);
List cats = q.list();</programlisting>
      </section>

      <section id="objectstate-querying-executing-pagination">
        <title>Pagination

        <para>If you need to specify bounds upon your result set, that is, the
        maximum number of rows you want to retrieve and/or the first row you
        want to retrieve, you can use methods of the <literal>Query
        interface:</para>

        <programlisting role="JAVA">Query q = sess.createQuery("from DomesticCat cat");
q.setFirstResult(20);
q.setMaxResults(10);
List cats = q.list();</programlisting>

        <para>Hibernate knows how to translate this limit query into the
        native SQL of your DBMS.</para>
      </section>

      <section id="objectstate-querying-executing-scrolling">
        <title>Scrollable iteration

        <para>If your JDBC driver supports scrollable
        <literal>ResultSets, the Query interface
        can be used to obtain a <literal>ScrollableResults object
        that allows flexible navigation of the query results.</para>

        <programlisting role="JAVA">Query q = sess.createQuery("select cat.name, cat from DomesticCat cat " +
                            "order by cat.name");
ScrollableResults cats = q.scroll();
if ( cats.first() ) {

    // find the first name on each page of an alphabetical list of cats by name
    firstNamesOfPages = new ArrayList();
    do {
        String name = cats.getString(0);
        firstNamesOfPages.add(name);
    }
    while ( cats.scroll(PAGE_SIZE) );

    // Now get the first page of cats
    pageOfCats = new ArrayList();
    cats.beforeFirst();
    int i=0;
    while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );

}
cats.close()</programlisting>

        <para>Note that an open database connection and cursor is required for
        this functionality. Use
        <literal>setMaxResult()/setFirstResult()
        if you need offline pagination functionality.</para>
      </section>

      <section id="objectstate-querying-executing-named" revision="1">
        <title>Externalizing named queries

        <para>Queries can also be configured as so called named queries using
        annotations or Hibernate mapping documents.
        <literal>@NamedQuery and @NamedQueries
        can be defined at the class level as seen in <xref
        linkend="example-named-query-annotation" /> . However their
        definitions are global to the session factory/entity manager factory
        scope. A named query is defined by its name and the actual query
        string.</para>

        <example id="example-named-query-annotation">
          <title>Defining a named query using
          <classname>@NamedQuery

          <programlisting language="JAVA" role="JAVA">@Entity
@NamedQuery(name="night.moreRecentThan", query="select n from Night n where n.date >= :date")
public class Night {
    ...
}

public class MyDao {
    doStuff() {
        Query q = s.getNamedQuery("night.moreRecentThan");
        q.setDate( "date", aMonthAgo );
        List results = q.list();
        ...
    }
    ...
}      </programlisting>
        </example>

        <para>Using a mapping document can be configured using the
        <literal><query> node. Remember to use a
        <literal>CDATA section if your query contains characters
        that could be interpreted as markup.</para>

        <example>
          <title>Defining a named query using
          <literal><query>

          <programlisting role="XML"><query name="ByNameAndMaximumWeight"><![CDATA[
    from eg.DomesticCat as cat
        where cat.name = ?
        and cat.weight > ?
] ]></query></programlisting>
        </example>

        <para>Parameter binding and executing is done programatically as seen
        in <xref linkend="example-parameter-binding-named-query" />.

        <example id="example-parameter-binding-named-query">
          <title>Parameter binding of a named query

          <programlisting role="JAVA">Query q = sess.getNamedQuery("ByNameAndMaximumWeight");
q.setString(0, name);
q.setInt(1, minWeight);
List cats = q.list();</programlisting>
        </example>

        <para>The actual program code is independent of the query language
        that is used. You can also define native SQL queries in metadata, or
        migrate existing queries to Hibernate by placing them in mapping
        files.</para>

        <para>Also note that a query declaration inside a
        <literal><hibernate-mapping> element requires a global
        unique name for the query, while a query declaration inside a
        <literal><class> element is made unique automatically
        by prepending the fully qualified name of the class. For example
        <literal>eg.Cat.ByNameAndMaximumWeight.
      </section>
    </section>

    <section id="objectstate-filtering" revision="1">
      <title>Filtering collections

      <para>A collection filter is a special type of
      query that can be applied to a persistent collection or array. The query
      string can refer to <literal>this, meaning the current
      collection element.</para>

      <programlisting role="JAVA">Collection blackKittens = session.createFilter(
    pk.getKittens(), 
    "where this.color = ?")
    .setParameter( Color.BLACK, Hibernate.custom(ColorUserType.class) )
    .list()
);</programlisting>

      <para>The returned collection is considered a bag that is a copy of the
      given collection. The original collection is not modified. This is
      contrary to the implication of the name "filter", but consistent with
      expected behavior.</para>

      <para>Observe that filters do not require a from
      clause, although they can have one if required. Filters are not limited
      to returning the collection elements themselves.</para>

      <programlisting role="JAVA">Collection blackKittenMates = session.createFilter(
    pk.getKittens(), 
    "select this.mate where this.color = eg.Color.BLACK.intValue")
    .list();</programlisting>

      <para>Even an empty filter query is useful, e.g. to load a subset of
      elements in a large collection:</para>

      <programlisting role="JAVA">Collection tenKittens = session.createFilter(
    mother.getKittens(), "")
    .setFirstResult(0).setMaxResults(10)
    .list();</programlisting>
    </section>

    <section id="objecstate-querying-criteria" revision="1">
      <title>Criteria queries

      <para>HQL is extremely powerful, but some developers prefer to build
      queries dynamically using an object-oriented API, rather than building
      query strings. Hibernate provides an intuitive
      <literal>Criteria query API for these cases:

      <programlisting role="JAVA">Criteria crit = session.createCriteria(Cat.class);
crit.add( Restrictions.eq( "color", eg.Color.BLACK ) );
crit.setMaxResults(10);
List cats = crit.list();</programlisting>

      <para>The Criteria and the associated
      <literal>Example API are discussed in more detail in 
    </section>

    <section id="objectstate-querying-nativesql" revision="2">
      <title>Queries in native SQL

      <para>You can express a query in SQL, using
      <literal>createSQLQuery() and let Hibernate manage the mapping
      from result sets to objects. You can at any time call
      <literal>session.connection() and use the JDBC
      <literal>Connection directly. If you choose to use the
      Hibernate API, you must enclose SQL aliases in braces:</para>

      <programlisting role="JAVA">List cats = session.createSQLQuery("SELECT {cat.*} FROM CAT {cat} WHERE ROWNUM<10")
    .addEntity("cat", Cat.class)
.list();</programlisting>

      <programlisting role="JAVA">List cats = session.createSQLQuery(
    "SELECT {cat}.ID AS {cat.id}, {cat}.SEX AS {cat.sex}, " +
           "{cat}.MATE AS {cat.mate}, {cat}.SUBCLASS AS {cat.class}, ... " +
    "FROM CAT {cat} WHERE ROWNUM<10")
    .addEntity("cat", Cat.class)
.list()</programlisting>

      <para>SQL queries can contain named and positional parameters, just like
      Hibernate queries. More information about native SQL queries in
      Hibernate can be found in <xref linkend="querysql" />.
    </section>
  </section>

  <section id="objectstate-modifying" revision="1">
    <title>Modifying persistent objects

    <para>Transactional persistent instances (i.e.
    objects loaded, saved, created or queried by the
    <literal>Session) can be manipulated by the application, and any
    changes to persistent state will be persisted when the
    <literal>Session is flushed. This is
    discussed later in this chapter. There is no need to call a particular
    method (like <literal>update(), which has a different purpose)
    to make your modifications persistent. The most straightforward way to
    update the state of an object is to <literal>load() it and then
    manipulate it directly while the <literal>Session is
    open:</para>

    <programlisting role="JAVA">DomesticCat cat = (DomesticCat) sess.load( Cat.class, new Long(69) );
cat.setName("PK");
sess.flush();  // changes to cat are automatically detected and persisted</programlisting>

    <para>Sometimes this programming model is inefficient, as it requires in
    the same session both an SQL <literal>SELECT to load an object
    and an SQL <literal>UPDATE to persist its updated state.
    Hibernate offers an alternate approach by using detached instances.</para>

  </section>

  <section id="objectstate-detached" revision="2">
    <title>Modifying detached objects

    <para>Many applications need to retrieve an object in one transaction,
    send it to the UI layer for manipulation, then save the changes in a new
    transaction. Applications that use this kind of approach in a
    high-concurrency environment usually use versioned data to ensure
    isolation for the "long" unit of work.</para>

    <para>Hibernate supports this model by providing for reattachment of
    detached instances using the <literal>Session.update() or
    <literal>Session.merge() methods:

    <programlisting role="JAVA">// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catId);
Cat potentialMate = new Cat();
firstSession.save(potentialMate);

// in a higher layer of the application
cat.setMate(potentialMate);

// later, in a new session
secondSession.update(cat);  // update cat
secondSession.update(mate); // update mate</programlisting>

    <para>If the Cat with identifier
    <literal>catId had already been loaded by
    <literal>secondSession when the application tried to reattach
    it, an exception would have been thrown.</para>

    <para>Use update() if you are certain that the session
    does not contain an already persistent instance with the same identifier.
    Use <literal>merge() if you want to merge your modifications at
    any time without consideration of the state of the session. In other
    words, <literal>update() is usually the first method you would
    call in a fresh session, ensuring that the reattachment of your detached
    instances is the first operation that is executed.</para>

    <para>The application should individually update()
    detached instances that are reachable from the given detached instance
    <emphasis>only if it wants their state to be updated. This can
    be automated using <emphasis>transitive persistence. See 

    <para>The lock() method also allows an application to
    reassociate an object with a new session. However, the detached instance
    has to be unmodified.</para>

    <programlisting role="JAVA">//just reassociate:
sess.lock(fritz, LockMode.NONE);
//do a version check, then reassociate:
sess.lock(izi, LockMode.READ);
//do a version check, using SELECT ... FOR UPDATE, then reassociate:
sess.lock(pk, LockMode.UPGRADE);</programlisting>

    <para>Note that lock() can be used with various
    <literal>LockModes. See the API documentation and the chapter on
    transaction handling for more information. Reattachment is not the only
    usecase for <literal>lock().

    <para>Other models for long units of work are discussed in 
  </section>

  <section id="objectstate-saveorupdate">
    <title>Automatic state detection

    <para>Hibernate users have requested a general purpose method that either
    saves a transient instance by generating a new identifier or
    updates/reattaches the detached instances associated with its current
    identifier. The <literal>saveOrUpdate() method implements this
    functionality.</para>

    <programlisting role="JAVA">// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catID);

// in a higher tier of the application
Cat mate = new Cat();
cat.setMate(mate);

// later, in a new session
secondSession.saveOrUpdate(cat);   // update existing state (cat has a non-null id)
secondSession.saveOrUpdate(mate);  // save the new instance (mate has a null id)</programlisting>

    <para>The usage and semantics of saveOrUpdate() seems
    to be confusing for new users. Firstly, so long as you are not trying to
    use instances from one session in another new session, you should not need
    to use <literal>update(), saveOrUpdate(), or
    <literal>merge(). Some whole applications will never use either
    of these methods.</para>

    <para>Usually update() or
    <literal>saveOrUpdate() are used in the following
    scenario:</para>

    <itemizedlist spacing="compact">
      <listitem>
        <para>the application loads an object in the first session
      </listitem>

      <listitem>
        <para>the object is passed up to the UI tier
      </listitem>

      <listitem>
        <para>some modifications are made to the object
      </listitem>

      <listitem>
        <para>the object is passed back down to the business logic tier
      </listitem>

      <listitem>
        <para>the application persists these modifications by calling
        <literal>update() in a second session
      </listitem>
    </itemizedlist>

    <para>saveOrUpdate() does the following:

    <itemizedlist spacing="compact">
      <listitem>
        <para>if the object is already persistent in this session, do
        nothing</para>
      </listitem>

      <listitem>
        <para>if another object associated with the session has the same
        identifier, throw an exception</para>
      </listitem>

      <listitem>
        <para>if the object has no identifier property,
        <literal>save() it
      </listitem>

      <listitem>
        <para>if the object's identifier has the value assigned to a newly
        instantiated object, <literal>save() it
      </listitem>

      <listitem>
        <para>if the object is versioned by a
        <literal><version> or
        <literal><timestamp>, and the version property value
        is the same value assigned to a newly instantiated object,
        <literal>save() it
      </listitem>

      <listitem>
        <para>otherwise update() the object
      </listitem>
    </itemizedlist>

    <para>and merge() is very different:

    <itemizedlist spacing="compact">
      <listitem>
        <para>if there is a persistent instance with the same identifier
        currently associated with the session, copy the state of the given
        object onto the persistent instance</para>
      </listitem>

      <listitem>
        <para>if there is no persistent instance currently associated with the
        session, try to load it from the database, or create a new persistent
        instance</para>
      </listitem>

      <listitem>
        <para>the persistent instance is returned
      </listitem>

      <listitem>
        <para>the given instance does not become associated with the session,
        it remains detached</para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="objectstate-deleting" revision="1">
    <title>Deleting persistent objects

    <para>Session.delete() will remove an object's state
    from the database. Your application, however, can still hold a reference
    to a deleted object. It is best to think of <literal>delete() as
    making a persistent instance, transient.</para>

    <programlisting role="JAVA">sess.delete(cat);

    <para>You can delete objects in any order, without risk of foreign key
    constraint violations. It is still possible to violate a <literal>NOT
    NULL</literal> constraint on a foreign key column by deleting objects in
    the wrong order, e.g. if you delete the parent, but forget to delete the
    children.</para>
  </section>

  <section id="objectstate-replicating" revision="1">
    <title>Replicating object between two different datastores

    <para>It is sometimes useful to be able to take a graph of persistent
    instances and make them persistent in a different datastore, without
    regenerating identifier values.</para>

    <programlisting role="JAVA">//retrieve a cat from one database
Session session1 = factory1.openSession();
Transaction tx1 = session1.beginTransaction();
Cat cat = session1.get(Cat.class, catId);
tx1.commit();
session1.close();

//reconcile with a second database
Session session2 = factory2.openSession();
Transaction tx2 = session2.beginTransaction();
session2.replicate(cat, ReplicationMode.LATEST_VERSION);
tx2.commit();
session2.close();</programlisting>

    <para>The ReplicationMode determines how
    <literal>replicate() will deal with conflicts with existing rows
    in the database:</para>

    <itemizedlist spacing="compact">
      <listitem>
        <para>ReplicationMode.IGNORE: ignores the object
        when there is an existing database row with the same identifier</para>
      </listitem>

      <listitem>
        <para>ReplicationMode.OVERWRITE: overwrites any
        existing database row with the same identifier</para>
      </listitem>

      <listitem>
        <para>ReplicationMode.EXCEPTION: throws an
        exception if there is an existing database row with the same
        identifier</para>
      </listitem>

      <listitem>
        <para>ReplicationMode.LATEST_VERSION: overwrites
        the row if its version number is earlier than the version number of
        the object, or ignore the object otherwise</para>
      </listitem>
    </itemizedlist>

    <para>Usecases for this feature include reconciling data entered into
    different database instances, upgrading system configuration information
    during product upgrades, rolling back changes made during non-ACID
    transactions and more.</para>
  </section>

  <section id="objectstate-flushing">
    <title>Flushing the Session

    <para>Sometimes the Session will execute the SQL
    statements needed to synchronize the JDBC connection's state with the
    state of objects held in memory. This process, called
    <emphasis>flush, occurs by default at the following
    points:</para>

    <itemizedlist spacing="compact">
      <listitem>
        <para>before some query executions
      </listitem>

      <listitem>
        <para>from
        <literal>org.hibernate.Transaction.commit()
      </listitem>

      <listitem>
        <para>from Session.flush()
      </listitem>
    </itemizedlist>

    <para>The SQL statements are issued in the following order:

    <orderedlist spacing="compact">
      <listitem>
        <para>all entity insertions in the same order the corresponding
        objects were saved using <literal>Session.save()
      </listitem>

      <listitem>
        <para>all entity updates
      </listitem>

      <listitem>
        <para>all collection deletions
      </listitem>

      <listitem>
        <para>all collection element deletions, updates and insertions
      </listitem>

      <listitem>
        <para>all collection insertions
      </listitem>

      <listitem>
        <para>all entity deletions in the same order the corresponding objects
        were deleted using <literal>Session.delete()
      </listitem>
    </orderedlist>

    <para>An exception is that objects using native ID
    generation are inserted when they are saved.</para>

    <para>Except when you explicitly flush(), there are
    absolutely no guarantees about <emphasis>when the
    <literal>Session executes the JDBC calls, only the
    <emphasis>order in which they are executed. However, Hibernate
    does guarantee that the <literal>Query.list(..) will never
    return stale or incorrect data.</para>

    <para>It is possible to change the default behavior so that flush occurs
    less frequently. The <literal>FlushMode class defines three
    different modes: only flush at commit time when the Hibernate
    <literal>Transaction API is used, flush automatically using the
    explained routine, or never flush unless <literal>flush() is
    called explicitly. The last mode is useful for long running units of work,
    where a <literal>Session is kept open and disconnected for a
    long time (see <xref
    linkend="transactions-optimistic-longsession" />).</para>

    <programlisting role="JAVA">sess = sf.openSession();
Transaction tx = sess.beginTransaction();
sess.setFlushMode(FlushMode.COMMIT); // allow queries to return stale state

Cat izi = (Cat) sess.load(Cat.class, id);
izi.setName(iznizi);

// might return stale data
sess.find("from Cat as cat left outer join cat.kittens kitten");

// change to izi is not flushed!
...
tx.commit(); // flush occurs
sess.close();</programlisting>

    <para>During flush, an exception might occur (e.g. if a DML operation
    violates a constraint). Since handling exceptions involves some
    understanding of Hibernate's transactional behavior, we discuss it in
    <xref linkend="transactions" />.
  </section>

  <section id="objectstate-transitive" revision="1">
    <title>Transitive persistence

    <para>It is quite cumbersome to save, delete, or reattach individual
    objects, especially if you deal with a graph of associated objects. A
    common case is a parent/child relationship. Consider the following
    example:</para>

    <para>If the children in a parent/child relationship would be value typed
    (e.g. a collection of addresses or strings), their life cycle would depend
    on the parent and no further action would be required for convenient
    "cascading" of state changes. When the parent is saved, the value-typed
    child objects are saved and when the parent is deleted, the children will
    be deleted, etc. This works for operations such as the removal of a child
    from the collection. Since value-typed objects cannot have shared
    references, Hibernate will detect this and delete the child from the
    database.</para>

    <para>Now consider the same scenario with parent and child objects being
    entities, not value-types (e.g. categories and items, or parent and child
    cats). Entities have their own life cycle and support shared references.
    Removing an entity from the collection does not mean it can be deleted),
    and there is by default no cascading of state from one entity to any other
    associated entities. Hibernate does not implement <emphasis>persistence by
    reachability</emphasis> by default.

    <para>For each basic operation of the Hibernate session - including
    <literal>persist(), merge(), saveOrUpdate(), delete(), lock(), refresh(),
    evict(), replicate()</literal> - there is a corresponding cascade style.
    Respectively, the cascade styles are named <literal>create, merge,
    save-update, delete, lock, refresh, evict, replicate</literal>. If you
    want an operation to be cascaded along an association, you must indicate
    that in the mapping document. For example:</para>

    <programlisting role="XML"><one-to-one name="person" cascade="persist"/>

    <para>Cascade styles my be combined:

    <programlisting role="XML"><one-to-one name="person" cascade="persist,delete,lock"/>

    <para>You can even use cascade="all" to specify that
    <emphasis>all operations should be cascaded along the
    association. The default <literal>cascade="none" specifies that
    no operations are to be cascaded.</para>

    <para>In case you are using annotatons you probably have noticed the
    <literal>cascade attribute taking an array of
    <classname>CascadeType as a value. The cascade concept in JPA
    is very is similar to the transitive persistence and cascading of
    operations as described above, but with slightly different semantics and
    cascading types:</para>

    <itemizedlist>
      <listitem>
        <para>CascadeType.PERSIST: cascades the persist
        (create) operation to associated entities persist() is called or if
        the entity is managed</para>
      </listitem>

      <listitem>
        <para>CascadeType.MERGE: cascades the merge
        operation to associated entities if merge() is called or if the entity
        is managed</para>
      </listitem>

      <listitem>
        <para>CascadeType.REMOVE: cascades the remove
        operation to associated entities if delete() is called</para>
      </listitem>

      <listitem>
        <para>CascadeType.REFRESH: cascades the refresh
        operation to associated entities if refresh() is called</para>
      </listitem>

      <listitem>
        <para>CascadeType.DETACH: cascades the detach
        operation to associated entities if detach() is called</para>
      </listitem>

      <listitem>
        <para>CascadeType.ALL: all of the above
      </listitem>
    </itemizedlist>

    <note>
      <para>CascadeType.ALL also covers Hibernate specific operations like
      save-update, lock etc...</para>
    </note>

    <para>A special cascade style, delete-orphan, applies
    only to one-to-many associations, and indicates that the
    <literal>delete() operation should be applied to any child
    object that is removed from the association. Using annotations there is no
    <literal>CascadeType.DELETE-ORPHAN equivalent. Instead you can
    use the attribute <literal>orphanRemoval as seen in  collection or an
    associated entity is dereferenced from a <classname>@OneToOne
    association, this associated entity can be marked for deletion if
    <literal>orphanRemoval is set to true.

    <example id="example-one-to-many-with-orphan-removal">
      <title>@OneToMany with
      <literal>orphanRemoval

      <programlisting language="JAVA" role="JAVA">@Entity 
public class Customer {
   private Set<Order> orders;

   @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true) 
   public Set<Order> getOrders() { return orders; }

   public void setOrders(Set<Order> orders) { this.orders = orders; }

   [...]
}

@Entity 
public class Order { ... }

Customer customer = em.find(Customer.class, 1l);
Order order = em.find(Order.class, 1l);
customer.getOrders().remove(order); //order will be deleted by cascade</programlisting>
    </example>

    <para>Recommendations:

    <itemizedlist spacing="compact">
      <listitem>
        <para>It does not usually make sense to enable cascade on a
        many-to-one or many-to-many association. In fact the
        <literal>@ManyToOne and @ManyToMany don't
        even offer a <literal>orphanRemoval attribute. Cascading is
        often useful for one-to-one and one-to-many associations.</para>
      </listitem>

      <listitem>
        <para>If the child object's lifespan is bounded by the lifespan of the
        parent object, make it a <emphasis>life cycle object by
        specifying
        <literal>cascade="all,delete-orphan"(@OneToMany(cascade=CascadeType.ALL,
        orphanRemoval=true)</literal>).
      </listitem>

      <listitem>
        <para>Otherwise, you might not need cascade at all. But if you think
        that you will often be working with the parent and children together
        in the same transaction, and you want to save yourself some typing,
        consider using
        <literal>cascade="persist,merge,save-update".
      </listitem>
    </itemizedlist>

    <para>Mapping an association (either a single valued association, or a
    collection) with <literal>cascade="all" marks the association as
    a <emphasis>parent/child style relationship where
    save/update/delete of the parent results in save/update/delete of the
    child or children.</para>

    <para>Furthermore, a mere reference to a child from a persistent parent
    will result in save/update of the child. This metaphor is incomplete,
    however. A child which becomes unreferenced by its parent is
    <emphasis>not automatically deleted, except in the case of a
    one-to-many association mapped with
    <literal>cascade="delete-orphan". The precise semantics of
    cascading operations for a parent/child relationship are as
    follows:</para>

    <itemizedlist spacing="compact">
      <listitem>
        <para>If a parent is passed to persist(), all
        children are passed to <literal>persist()
      </listitem>

      <listitem>
        <para>If a parent is passed to merge(), all
        children are passed to <literal>merge()
      </listitem>

      <listitem>
        <para>If a parent is passed to save(),
        <literal>update() or saveOrUpdate(), all
        children are passed to <literal>saveOrUpdate()
      </listitem>

      <listitem>
        <para>If a transient or detached child becomes referenced by a
        persistent parent, it is passed to
        <literal>saveOrUpdate()
      </listitem>

      <listitem>
        <para>If a parent is deleted, all children are passed to
        <literal>delete()
      </listitem>

      <listitem>
        <para>If a child is dereferenced by a persistent parent,
        <emphasis>nothing special happens - the application should
        explicitly delete the child if necessary - unless
        <literal>cascade="delete-orphan", in which case the
        "orphaned" child is deleted.</para>
      </listitem>
    </itemizedlist>

    <para>Finally, note that cascading of operations can be applied to an
    object graph at <emphasis>call time or at flush
    time</emphasis>. All operations, if enabled, are cascaded to associated
    entities reachable when the operation is executed. However,
    <literal>save-update and delete-orphan are
    transitive for all associated entities reachable during flush of the
    <literal>Session.
  </section>

  <section id="objectstate-metadata">
    <title>Using metadata

    <para>Hibernate requires a rich meta-level model of all entity and value
    types. This model can be useful to the application itself. For example,
    the application might use Hibernate's metadata to implement a "smart"
    deep-copy algorithm that understands which objects should be copied (eg.
    mutable value types) and which objects that should not (e.g. immutable
    value types and, possibly, associated entities).</para>

    <para>Hibernate exposes metadata via the ClassMetadata
    and <literal>CollectionMetadata interfaces and the
    <literal>Type hierarchy. Instances of the metadata interfaces
    can be obtained from the <literal>SessionFactory.

    <programlisting role="JAVA">Cat fritz = ......;
ClassMetadata catMeta = sessionfactory.getClassMetadata(Cat.class);

Object[] propertyValues = catMeta.getPropertyValues(fritz);
String[] propertyNames = catMeta.getPropertyNames();
Type[] propertyTypes = catMeta.getPropertyTypes();

// get a Map of all properties which are not collections or associations
Map namedValues = new HashMap();
for ( int i=0; i<propertyNames.length; i++ ) {
    if ( !propertyTypes[i].isEntityType() && !propertyTypes[i].isCollectionType() ) {
        namedValues.put( propertyNames[i], propertyValues[i] );
    }
}</programlisting>
  </section>
</chapter>

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate session_api.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.