Java Reference
In-Depth Information
TIP
The code examples use parts of the API used that are required in client apps; when you
run your code in a Java EE server, a lot of details—things like security, transactions, con-
figuration, dependency injection (see Using Dependency Injection ) of needed re-
sources)—are taken care of for you by the EE server.
Easy Database Access with JPA and/or Hibernate
Problem
You want an easy, high-level way to access your relational database.
Solution
Use the Java Persistence API (JPA) Entity annotations, and either the JPA or Hibernate APIs.
IAN'S BASIC STEPS: JPA
To set up a JPA application:
1. Write, annotate, and compile the POJO data classes.
2. Create an XML file persistence.xml (this name is mandatory), containing the database ac-
cess informtion, and listing the entity classes.
3. Obtain an EntityManagerFactory from the Persistence factory class.
4. Obtain an EntityManager from the EntityManagerFactory .
5. To save data, get the transaction, make the objects persistent by calling, e.g.,
yourEntityManager.save(yourObject) , and commit.
6. To load data objects, load them by primary key or use a query.
Search WWH ::




Custom Search