|
Hibernate example source code file (tutorial_jpa.xml)
This example Hibernate source code file (tutorial_jpa.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.
The Hibernate tutorial_jpa.xml source code
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<chapter id="hibernate-gsg-tutorial-jpa">
<title>Tutorial Using the Java Persistence API (JPA)
<para>
This tutorial is located within the download bundle under <filename>entitymanager.
</para>
<itemizedlist>
<title>Objectives
<listitem>
<para>
Use annotations to provide mapping information.
</para>
</listitem>
<listitem>
<para>
Use <systemitem>JPA.
</para>
</listitem>
</itemizedlist>
<section id="hibernate-gsg-tutorial-jpa-config">
<title>persistence.xml
<para>
The previous tutorials used the Hibernate-specific
<filename>hibernate.cfg.xml configuration file.
<systemitem>JPA, however, defines a different bootstrap process that uses its own configuration
file named <filename>persistence.xml. This bootstrapping process is defined by the
<systemitem>JPA specification. In Java SE environments the persistence
provider (Hibernate in this case) is required to locate all <systemitem>JPA configuration files
by classpath lookup of the <filename>META-INF/persistence.xml resource name.
</para>
<example id="hibernate-gsg-tutorial-jpa-config-pu">
<title>persistence.xml
<programlisting role="XML">
</example>
<para>
<filename>persistence.xml files should provide a unique name for each persistence
unit</phrase>. Applications use this name to reference the configuration when obtaining an
<interfacename>javax.persistence.EntityManagerFactory reference.
</para>
<para>
The settings defined in the <varname>properties element are discussed in javax.persistence-prefixed
varieties are used when possible. Notice that the remaining Hibernate-specific configuration setting names
are now prefixed with <literal>hibernate..
</para>
<para>
Additionally, the <varname>class element functions the same as in
<para>
The entity is exactly the same as in <xref linkend="hibernate-gsg-tutorial-annotations-entity"/>
</para>
</section>
<section id="hibernate-gsg-tutorial-jpa-test">
<title>Example code
<para>
The previous tutorials used the Hibernate APIs. This tutorial uses the <systemitem>JPA APIs.
</para>
<example id="hibernate-gsg-tutorial-jpa-test-setUp">
<title>Obtaining the javax.persistence.EntityManagerFactory
<programlisting role="JAVA">protected void setUp() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.jpa" );
}</programlisting>
</example>
<para>
Notice again that the persistence unit name is <literal>org.hibernate.tutorial.jpa, which matches
<xref linkend="hibernate-gsg-tutorial-jpa-config-pu"/>
</para>
<example id="hibernate-gsg-tutorial-jpa-test-saving">
<title>Saving (persisting) entities
<programlisting role="JAVA">EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( new Event( "Our very first event!", new Date() ) );
entityManager.persist( new Event( "A follow up event", new Date() ) );
entityManager.getTransaction().commit();
entityManager.close();</programlisting>
</example>
<para>
The code is similar to <xref linkend="hibernate-gsg-tutorial-basic-test-saving"/>. An
<interfacename>javax.persistence.EntityManager interface is used instead of a
<interfacename>org.hibernate.Session interface. JPA calls this
operation <methodname>persist instead of save.
</para>
<example id="hibernate-gsg-tutorial-jpa-test-list">
<title>Obtaining a list of entities
<programlisting role="JAVA">
</example>
<para>
Again, the code is pretty similar to <xref linkend="hibernate-gsg-tutorial-basic-test-list"/>.
</para>
</section>
<section id="hibernate-gsg-tutorial-annotations-further">
<title>Take it further!
<itemizedlist>
<title>Practice Exercises
<listitem>
<para>
Develop an EJB Session bean to investigate implications of using a container-managed
persistence context (<interfacename>javax.persistence.EntityManager). Try both
stateless and stateful use-cases.
</para>
</listitem>
</itemizedlist>
</section>
</chapter>
Other Hibernate examples (source code examples)
Here is a short list of links related to this Hibernate tutorial_jpa.xml source code file:
|