Obtaining a JPA Database Connection
In JPA, a database connection is represented by the EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context. interface. Therefore, to manipulate an ObjectDB database, you need an EntityManager instance. Operations that modify database content also require an EntityTransactionjakarta.persistence.EntityTransactionInterface used to control transactions on resource-local entity managers. instance.
Obtaining an EntityManagerFactory
Obtaining an EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context. instance is a two-step process. First, you obtain an EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager . instance that represents the database. Then, you use that factory to get an EntityManager instance.
JPA requires you to define a persistence unit generate an EntityManagerFactory. However, with ObjectDB, you can either define a standard persistence unit or provide the file path of the ObjectDB database directly:
EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager . emf = Persistencejakarta.persistence.PersistenceBootstrap class used to obtain an EntityManagerFactory in Java SE environments..createEntityManagerFactoryjakarta.persistence.Persistence.createEntityManagerFactory(String)Create and return an EntityManagerFactory for the named persistence unit.( "objectdb:$objectdb/db/points.odb");
The createEntityManagerFactoryjakarta.persistence.Persistence.createEntityManagerFactory(String)Create and return an EntityManagerFactory for the named persistence unit. static method expects a persistence unit name as an argument. However, with ObjectDB, any valid database file path (absolute or relative) is also accepted. ObjectDB considers any string that starts with the objectdb: prefix or ends with .odb or .objectdb to be a database URL rather than a persistence unit name.
The $objectdb variable represents the ObjectDB home directory, which by default is the directory where ObjectDB is installed. If a database file does not already exist at the specified path, ObjectDB creates one.
You also use the EntityManagerFactory to close the database when you are finished with it:
emf.closejakarta.persistence.EntityManagerFactory.close()Close the factory, releasing any resources that it holds.();
Obtaining an EntityManager
After you have an EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager ., you can obtain an EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context. instance:
EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context. em = emf.createEntityManagerjakarta.persistence.EntityManagerFactory.createEntityManager()Create 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. Furthermore, in a multithreaded application, each thread usually has its own EntityManager instance while sharing a single, application-wide EntityManagerFactory.
When the connection to the database is no longer needed, close the EntityManager:
em.closejakarta.persistence.EntityManager.close()Close an application-managed entity manager.();
Closing an EntityManager does not close the database itself; that is the job of the factory, as explained previously. After an EntityManager object is closed, it cannot be reused. However, the parent EntityManagerFactory instance might preserve the EntityManager resources (such as a database file pointer or a socket to a remote server) in a connection pool to speed up the creation of future EntityManager instances.
Using an EntityTransaction
Operations that modify database content, such as storing, updating, and deleting entities, must be performed within an active transaction.
Given an EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context. instance named em, you can begin a transaction:
em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().beginjakarta.persistence.EntityTransaction.begin()Start a resource transaction.();
A one-to-one relationship exists between an EntityManager instance and the EntityTransactionjakarta.persistence.EntityTransactionInterface used to control transactions on resource-local entity managers. instance that the getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object. method returns.
When a transaction is active, you can invoke EntityManager methods that modify the database content, such as persistjakarta.persistence.EntityManager.persist(Object)Make a new entity instance managed and persistent, resulting in its insertion in the database when the persistence context is synchronized with the database, or make a removed entity managed, undoing the effect of a previous call to EntityManager.remove . and removejakarta.persistence.EntityManager.remove(Object)Mark a managed entity instance as removed, resulting in its deletion from the database when the persistence context is synchronized with the database.. Database updates are collected and managed in memory and are then applied to the database when the transaction is committed:
em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().commitjakarta.persistence.EntityTransaction.commit()Commit 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 create, read, update, and delete (CRUD) database operations.