Obtaining a JPA Database Connection

In JPA a database connection is represented by the EntityManagerjavax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context. interface. Therefore, in order to manipulate an ObjectDB database we need an EntityManager instance. Operations that modify database content also require an EntityTransactionjavax.persistence.EntityTransaction - JPA InterfaceInterface used to control transactions on resource-local entity managers. instance.

Obtaining an EntityManagerFactory

Obtaining an EntityManagerjavax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context. instance consists of two steps. First we need to obtain an instance of EntityManagerFactoryjavax.persistence.EntityManagerFactory - JPA InterfaceInterface used to interact with the entity manager factory for the persistence unit. that represents the relevant database and then we can use that factory instance to get an EntityManager instance.

JPA requires the definition of a persistence unit in an XML file in order to be able to generate an EntityManagerFactory. But when using ObjectDB you can either define a standard persistence unit in an XML file or you can simply provide the file path of the ObjectDB database instead:

  EntityManagerFactoryjavax.persistence.EntityManagerFactory - JPA InterfaceInterface used to interact with the entity manager factory
 for the persistence unit. emf =
     Persistencejavax.persistence.Persistence - JPA ClassBootstrap class that is used to obtain an {@link EntityManagerFactory}
 in Java SE environments..createEntityManagerFactoryPersistence.createEntityManagerFactory(persistenceUnitName) - JPA Static MethodCreate and return an EntityManagerFactory for the named
 persistence unit.(
          "objectdb:$objectdb/db/points.odb");

The createEntityManagerFactoryPersistence.createEntityManagerFactory(persistenceUnitName) - JPA Static MethodCreate and return an EntityManagerFactory for the named persistence unit. static method expects a persistence unit name as an argument, but when using ObjectDB, any valid database file path (absolute or relative) is also accepted. Any string that starts with the prefix objectdb: or ends with .odb or .objectdb is considered by ObjectDB to be a database URL rather than a persistence unit name.
The $objectdb variable represents the ObjectDB home directory (by default - the directory in which ObjectDB is installed). If no database file exists yet at the given path ObjectDB will try to create one.

The EntityManagerFactory is also used to close the database once we are finished using it:

  emf.closeEntityManagerFactory.close() - JPA MethodClose the factory, releasing any resources that it holds.();

Obtaining an EntityManager

Once we have an EntityManagerFactoryjavax.persistence.EntityManagerFactory - JPA InterfaceInterface used to interact with the entity manager factory for the persistence unit. we can easily obtain an EntityManagerjavax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context. instance:

  EntityManagerjavax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context. em = emf.createEntityManagerEntityManagerFactory.createEntityManager() - JPA MethodCreate a new application-managed EntityManager.();

The EntityManager instance represents a connection to the database. When using JPA, every operation on a database is associated with an EntityManager. Further, in a multithreaded application every thread usually has its own EntityManager instance while at the same time sharing a single application-wide EntityManagerFactory.

When the connection to the database is no longer needed the EntityManager can be closed:

  em.closeEntityManager.close() - JPA MethodClose an application-managed entity manager.();

Closing an EntityManager does not close the database itself (that is the job of the factory as previously explained). Once the EntityManager object is closed it cannot be reused. However, the owning EntityManagerFactory instance may preserve the EntityManager's resources (such as a database file pointer or a socket to a remote server) in a connection pool and use them to speed up future EntityManager construction.

Using an EntityTransaction

Operations that modify database content, such as store, update, and delete should only be performed within an active transaction.

Given an EntityManagerjavax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context., em, it is very easy to begin a transaction:

  em.getTransactionEntityManager.getTransaction() - JPA MethodReturn the resource-level EntityTransaction object.().beginEntityTransaction.begin() - JPA MethodStart a resource transaction.();

There is a one to one relationship between an EntityManager instance and its associated EntityTransactionjavax.persistence.EntityTransaction - JPA InterfaceInterface used to control transactions on resource-local entity managers. instances that the getTransactionEntityManager.getTransaction() - JPA MethodReturn the resource-level EntityTransaction object. method returns.

When a transaction is active you can invoke EntityManager methods that modify the database content, such as persistEntityManager.persist(entity) - JPA MethodMake an instance managed and persistent. and removeEntityManager.remove(entity) - JPA MethodRemove the entity instance.. Database updates are collected and managed in memory and applied to the database when the transaction is committed:

  em.getTransactionEntityManager.getTransaction() - JPA MethodReturn the resource-level EntityTransaction object.().commitEntityTransaction.commit() - JPA MethodCommit the current resource transaction, writing any
 unflushed changes to the database.();

The next section explains in more detail how to use the EntityManager and transactions for CRUD database operations.