Java Reference
In-Depth Information
Each table is represented by one Java POJO , known as an entity. In your case you only
have one table, so you only declare one entity.
Listing 3.2
The META-INF/persistence.xml file
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
Defines new
persistence unit
<persistence-unit name="fancyfoods">
<jta-data-source>
osgi:service/jdbc/xafancyfoodsdb
</jta-data-source>
<non-jta-data-source>
osgi:service/jdbc/fancyfoodsdb
</non-jta-data-source>
<class>fancyfoods.persistence.FoodImpl</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property
name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
The persistence unit must declare both a JTA and a non- JTA datasource (see figure 3.8).
JPA -managed database access usually requires a transactional datasource, but transac-
tions can be expensive, so in some cases the JPA container optimizes operations by using
a nontransactional datasource. We'll come back to transactions in section 3.3. Use the
JNDI names you configured in listing 3.1.
The persistence unit also declares one or more class files, each of which contains
an entity that's mapped to a database table. Finally, you don't want to have to worry
about SQL and table creation, so add an extra property that requests that the database
tables are generated on the fly, based on the Java entities. The combination of this and
the setting to create the database in section 3.3 means that you can now do database
programming without ever having to create or configure a database.
Datasource
JNDI names
Entity
implementation class
Generates schema
from Java entities
JTA
JNDI
Fancy Foods
datasource
Fancy Foods
persistence
JNDI
Non-JTA
Figure 3.8 The persistence bundle uses the services exposed by the datasource bundle. It
accesses them by JNDI lookup.
 
Search WWH ::




Custom Search