7.3. Bare Essentials of Hibernate

In this section, I will rapidly and only tersely introduce the features of the Hibernate object/relational mapping and persistence framework. I will not talk about Hibernate-EntityManager, which as a JPA implementation serves to bridge the minor gap in syntax and semantics between JPA and Hibernate.

Hibernate is feature-rich compared to the lightweight JPA, but they share the same core concepts. This is no surprise, as the lead developers of Hibernate created and influenced the JPA specification. In some sense, Hibernate's simplicity and elegance were brought into the Java persistence architecture as a much needed relief to the beleaguered world of the legacy entity beans.

A Hibernate entity is a Plain Old Java Object (POJO) that declares a no-argument constructor. The no-argument constructor allows instantiation of the POJO using reflection. In addition, it's recommended that entities use Java bean–style getter and setter methods for its properties and declare its properties private.

Entities can define metadata using annotations or external XML configuration files. For the most part, Hibernate entity annotations are the same as JPA annotations. They both utilize Java 5 annotations. Hibernate Annotations, which is available as a separate download, maps the Java 5 and JPA annotations to the Hibernate metadata. XML configuration files are more traditional and have existed from the first few versions of Hibernate. By convention, entity metadata XML files follow a .hbm.xml file extension naming scheme.

Following convention, the configuration file for OSSProduct.java would be OSSProduct.hbm.xml. This XML file would reside in the same directory where OSSProduct.java resides. If you were to create a Hibernate entity that was the same as the OSSProduct JPA entity that I listed in the last section and use an XML configuration file to wire up its metadata, then you would likely see a configuration file (OSSProduct.hbm.xml) that contains the code shown in Listing 7-3.

Example 7.3. OSSProduct.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="problazeds.ch07.OSSProduct" table="OSSPRODUCTS">
        <id name="oSSProductId" column="OSSPRODUCT_ID">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="licenseType" column="LICENSE_TYPE"/>
        <set name="contributors" table="OSSPRODUCT_CONTRIBUTOR">
            <key column="OSSPRODUCT_ID"/>
            <many-to-many column="CONTRIBUTOR_ID" class="problazeds.ch07.
               Contributor"/>
        </set>
    </class>

</hibernate-mapping>

You could create the Contributor entity along the same lines as the OSSProduct entity. Once the entities and their configuration files are ready, you can configure Hibernate itself. Hibernate can be configured using any of the following three methods:

  • Using hibernate.properties

  • Using hibernate.cfg.xml

  • Programmatically

Using hibernate.cfg.xml, the configuration for an application that maintains a record of open source software products and its contributors will turn out as shown in Listing 7-4. I am intentionally reusing the same example I used to illustrate JPA. This will allow you to compare and contrast the two.

Example 7.4. hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/
           oss_catalog_db</property>
        <property name="connection.username">root</property>
        <property name="connection.password">specify the password here</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.
           NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="problazeds/ch07/OSSProduct.hbm.xml"/>
        <mapping resource="problazeds/ch07/Contributor.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

Now that the entities are created and configured, you could compile it; after which, you will be able to load and store objects from and to the data store.

In JPA, The EntityManager is the coordinator that interacts with loads, and stores entitites, from a transactional perspective. In Hibernate, where this concept originated, the coordinator and context manager that loads and stores data is a Session. You can create a Session using a SessionFactory. A session factory is typically an application-wide singleton. A session factory is either created by a utility class as a static singleton or registered with and accessed from a JNDI repository.

If you want to change the licenseType of an OSSProduct instance, you can use a session, within which you can carry out this operation as follows:

session.beginTransaction();
OSSProduct ossproduct = new OSSProduct();
ossproduct.setLicenseType(newLicenseType);
session.save(ossproduct);
session.getTransaction().commit();

As mentioned earlier, the session itself would be built out of the session factory.

At this stage, the rudimentary example is wired up and ready to run using Hibernate. However, all of what I have discussed so far is just the tip of a giant iceberg. There is a lot more in Hibernate, including support for complex transactions, different types of collections, sophisticated mapping strategies, a query language, interceptors, and whole lot of other features and utilities.

Consider reading the Hibernate reference documentation online at http://docs.jboss.org/hibernate/stable/core/reference/en/html and exploring the wiki at www.hibernate.org/36.html.

The focus of this chapter is on integrating Flex and JPA/Hibernate using BlazeDS. Now, with some information on JPA and Hibernate itself, you should be ready to understand the nuances of this integration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.88.54