When deploying outside of a container, use the createEntityManagerFactory method of the jakarta.persistence.Persistence class to create an entity manager factory. This method accepts a Map of properties and the name of the persistence unit. The properties that you pass to this method are combined with those specified in the persistence.xml file. They may be additional properties or they may override the value of a property that you specified previously in the persistence.xml file.
|
Tip: This is a convenient way to set properties obtained from program input, such as the command line. |
The EntityManager is the access point for persisting an entity bean, loading it from the database. Usually, the Jakarta Persistence API (JPA) container manages interaction with the data source. However, if you are using a JTA data source for your JPA persistence unit, you can access the JDBC connection from the Jakarta EE program container's data source. Because the managed data source is unavailable, you can pass properties to createEntityManagerFactory to change the transaction type from JTA to RESOURCE_LOCAL and to define JDBC connection information, as shown here:
Example 17-1 Changing transaction type and defining connection information
import static org.eclipse.persistence.jpa.config.PersistenceUnitProperties.*; ... Map properties = new HashMap(); // Ensure RESOURCE_LOCAL transactions is used. properties.put(TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name()); // Configure the internal EclipseLink connection pool properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver"); properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL"); properties.put(JDBC_USER, "user-name"); properties.put(JDBC_PASSWORD, "password"); properties.put(JDBC_READ_CONNECTIONS_MIN, "1"); properties.put(JDBC_WRITE_CONNECTIONS_MIN, "1"); // Configure logging. FINE ensures all SQL is shown properties.put(LOGGING_LEVEL, "FINE"); // Ensure that no server-platform is configured properties.put(TARGET_SERVER, TargetServer.None);
You also have access to the EclipseLink extensions to the EntityManager.