Java Reference
In-Depth Information
The core programming elements of Hibernate, JPA, and other ORM frameworks resemble those of
JDBC. They are summarized in Table 3-3.
Table 3-3.
Core Programming Elements for Different Data Access Strategies
Concept
JDBC
Hibernate
JPA
Connection
Session
EntityManager
Resource
DataSource
SessionFactory
EntityManagerFactory
Resource factory
SQLException
HibernateException
PersistenceException
Exception
In Hibernate, the core interface for object persistence is
Session
, whose instances can be obtained
from a
SessionFactory
instance. In JPA, the corresponding interface is
EntityManager
, whose instances
can be obtained from an
EntityManagerFactory
instance. The exceptions thrown by Hibernate are of
type
HibernateException
, while those thrown by JPA may be of type
PersistenceException
or other Java
SE exceptions like
IllegalArgumentException
and
IllegalStateException
. Note that all these exceptions
are subclasses of
RuntimeException
, which you are not forced to catch and handle.
Persisting Objects Using the Hibernate API with Hibernate XML Mappings
To map entity classes with Hibernate XML mappings, you can provide a single mapping file for each
class or a large file for several classes. Practically, you should define one for each class by joining the
class name with
.hbm.xml
as the file extension for ease of maintenance. The middle extension
hbm
stands
for Hibernate metadata.
The mapping file for the
Course
class should be named
Course.hbm.xml
and put in the same package
as the entity class.
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.apress.springenterpriserecipes.course">
<class name="Course" table="COURSE">
<id name="id" type="long" column="ID">
<generator class="identity" />
</id>
<property name="title" type="string">
<column name="TITLE" length="100" not-null="true" />
</property>
<property name="beginDate" type="date" column="BEGIN_DATE" />
<property name="endDate" type="date" column="END_DATE" />
<property name="fee" type="int" column="FEE" />
</class>
</hibernate-mapping>
Search WWH ::
Custom Search