Java Reference
In-Depth Information
In a Java EE environment, you can configure the JPA engine in a Java EE container. But in a Java SE
application, you have to set up the engine locally. The configuration of JPA is through the central XML
file persistence.xml , located in the META-INF directory of the classpath root. In this file, you can set any
vendor-specific properties for the underlying engine configuration.
Now let's create the JPA configuration file persistence.xml in the META-INF directory of the classpath
root. Each JPA configuration file contains one or more <persistence-unit> elements. A persistence unit
defines a set of persistent classes and how they should be persisted. Each persistence unit requires a
name for identification. Here, you assign the name course to this persistence unit.
<persistence xmlns=""
<persistence-unit name="course">
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />
In this JPA configuration file, you configure Hibernate as your underlying JPA engine by
referring to the Hibernate configuration file located in the classpath root. However, because
Hibernate EntityManager w ill automatically detect XML mapping files and JPA annotations
as mapping metadata, you have no need to specify them explicitly. Otherwise, you will encounter
an org.hibernate.DuplicateMappingException . Pick a strategy_annotations or XML_and stay with
it. You don't need to maintain both.
<!-- Don't need to specify mapping files and annotated classes -->
<mapping resource="com/apress/springenterpriserecipes/course/
Course.hbm.xml" />
<mapping class="com.apress.springenterpriserecipes.course.Course" />
As an alternative to referring to the Hibernate configuration file, you can also centralize all the
Hibernate configurations in persistence.xml .
<persistence ...>
<persistence-unit name="course">
<property name="hibernate.connection.driver_class"
value="org.apache.derby.jdbc.ClientDriver" />
<property name="hibernate.connection.url"
value="jdbc: derby://localhost:1527/course; create=true" />
<property name="hibernate.connection.username" value="app" />
<property name="hibernate.connection.password" value="app" />
Search WWH ::

Custom Search