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

Hibernate example source code file (batch.xml)

This example Hibernate source code file (batch.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

general, gnu, hibernate, hibernate, inc, java, java, jp-ql, jp-ql, lesser, license, public, this, this

The Hibernate batch.xml source code

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2008, Red Hat Inc 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 Inc.
  ~
  ~ 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">
<chapter id="batch">
  <title>Batch processing

  <para>Batch processing has traditionally been difficult in full
  object/relational mapping. ORM is all about object state management, which
  implies that object state is available in memory. However, Hibernate has
  some features to optimize batch processing which are discussed in the
  Hibernate reference guide, however, EJB3 persistence differs
  slightly.</para>

  <section id="batch-direct">
    <title>Bulk update/delete

    <para>As already discussed, automatic and transparent object/relational
    mapping is concerned with the management of object state. This implies
    that the object state is available in memory, hence updating or deleting
    (using SQL <literal>UPDATE and DELETE) data
    directly in the database will not affect in-memory state. However,
    Hibernate provides methods for bulk SQL-style <literal>UPDATE
    and <literal>DELETE statement execution which are performed
    through JP-QL (<xref linkend="queryhql" />).

    <para>The pseudo-syntax for UPDATE and
    <literal>DELETE statements is: ( UPDATE | DELETE )
    FROM? ClassName (WHERE WHERE_CONDITIONS)?</literal>. Note that:

    <itemizedlist spacing="compact">
      <listitem>
        <para>In the from-clause, the FROM keyword is optional.
      </listitem>

      <listitem>
        <para>There can only be a single class named in the from-clause, and
        it <emphasis>cannot have an alias (this is a current
        Hibernate limitation and will be removed soon).</para>
      </listitem>

      <listitem>
        <para>No joins (either implicit or explicit) can be specified in a
        bulk JP-QL query. Sub-queries may be used in the where-clause.</para>
      </listitem>

      <listitem>
        <para>The where-clause is also optional.
      </listitem>
    </itemizedlist>

    <para>As an example, to execute an JP-QL UPDATE, use
    the <literal>Query.executeUpdate() method:

    <programlisting role="JAVA" language="JAVA">EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

String jpqlUpdate = "update Customer set name = :newName where name = :oldName"
int updatedEntities = entityManager.createQuery( jpqlUpdate )
                            .setParameter( "newName", newName )
                            .setParameter( "oldName", oldName )
                            .executeUpdate();
entityManager.getTransaction().commit();
entityManager.close();</programlisting>

    <para>To execute an JP-QL DELETE, use the same
    <literal>Query.executeUpdate() method (the method is named for
    those familiar with JDBC's
    <literal>PreparedStatement.executeUpdate()):

    <programlisting role="JAVA" language="JAVA">EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = entityManager.createQuery( hqlDelete )
                            .setParameter( "oldName", oldName )
                            .executeUpdate();
entityManager.getTransaction().commit();
entityManager.close();</programlisting>

    <para>The int value returned by the
    <literal>Query.executeUpdate() method indicate the number of
    entities effected by the operation. This may or may not correlate with the
    number of rows effected in the database. A JP-QL bulk operation might
    result in multiple actual SQL statements being executed, for
    joined-subclass, for example. The returned number indicates the number of
    actual entities affected by the statement. Going back to the example of
    joined-subclass, a delete against one of the subclasses may actually
    result in deletes against not just the table to which that subclass is
    mapped, but also the "root" table and potentially joined-subclass tables
    further down the inheritance hierarchy.</para>
  </section>
</chapter>

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate batch.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.