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, in order to manipulate an ObjectDB database we 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 consists of two steps. First we need to obtain an instance of EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager. 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:
EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances ofEntityManager. emf = Persistencejakarta.persistence.PersistenceBootstrap class used to obtain anEntityManagerFactoryin Java SE environments..createEntityManagerFactoryjakarta.persistence.Persistence.createEntityManagerFactory(String)Create and return anEntityManagerFactoryfor 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, 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.closejakarta.persistence.EntityManagerFactory.close()Close the factory, releasing any resources that it holds.();
Obtaining an EntityManager
Once we have an EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager. we can easily 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. 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.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 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 EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context., em, it is very easy to begin a transaction:
em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().beginjakarta.persistence.EntityTransaction.begin()Start a resource transaction.(); There is a one to one relationship between an EntityManager instance and its associated EntityTransactionjakarta.persistence.EntityTransactionInterface used to control transactions on resource-local entity managers. instances 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 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 CRUD database operations.