Creating a JPA application

The following are the typical steps in creating a JPA application:

  1. Create a database schema (tables and relationships). Optionally, you can create tables and relationships from JPA entities. We will see an example of this. However, it should be mentioned here that although creating tables from JPA entities is fine for development, it is not recommended in the production environment; doing so may result in a non-optimized database model.
  2. Create persistence.xml and specify the database configurations.
  3. Create entities and relationships.
  4. Get an instance of EntityManagerFactory by calling Persistence.createEntityManagerFactory.
  5. Create an instance of EntityManager from EntityManagerFactory.
  6. Start a transaction on EntityManager if you are performing insert or update operations on the entity.
  7. Perform operations on the entity.
  8. Commit the transaction.

Here is an example snippet:

EntityManagerFactory factory = 
Persistance.Persistence.createEntityManagerFactory("course_management") EntityManager entityManager = factory.createEntityManager(); EntityTransaction txn = entityManager.getTransaction(); txn.begin(); entityManager. persist(course); txn.commit();

You can find a description of JPA annotations at http://www.eclipse.org/eclipselink/documentation/2.7/jpa/extensions/annotations_ref.htm.
JPA tools in Eclipse EE make adding many of the annotations very easy, as we will see in this section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111