The configuration for an entity manager is bound tothe factory that created it. Whether application or container managed, the factory needs a persistence unit from which to create an entity manager. A persistence unit dictates the settings to connect to the database and the list of entities that can be managed in a persistence context. The persistence unit is defined in a
This definition of the
persistence.xml
file located in the META-INF
directory.This definition of the
persistence.xml
file is different depending on whether we are in an application-managed environment or container-managed environment. Below are showed two examples of configuration.
1. Configuring Application-managed environment (Java SE)
Transactions are managed by the application (transaction-type =”RESOURCE_LOCAL”). Generally used for standalone application.
Persistence.xml
<persistence-unit name="MyUnitName" transaction-type="RESOURCE_LOCAL"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="toplink.logging.level" value="INFO"/> <property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/> <property name="toplink.jdbc.url" value="jdbc:oracle:thin:@myhost:l521:MYSID"/> <property name="toplink.jdbc.password" value="tiger"/> <property name="toplink.jdbc.user" value="scott"/> </properties> </persistence-unit>
DAO
public class Main { public static void main(String[] args) { // Gets an entity manager and a transaction EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyUnitName"); EntityManager em = emf.createEntityManager(); // Persists the entity to the database EntityTransactiontx = em.getTransaction(); try{ tx.begin(); em.persist(entity); tx.commit(); } catch (Exception e) { ... tx.rollback(); } finally { em.close(); emf.close(); } } }
2. Configuring container-managed environment (Java EE)
In a container-managed environment, EJBs and transactions are managed by the container, not by the application, so transaction-type of the persistent unit is commonly set to JTA. When managing transactions declaratively, you delegate the demarcation policy to the container. You don’t have to explicitly use JTA in your code (even if JTA is used underneath); you can leave the container to demarcate transaction boundaries by automatically beginning and committing transactions based on metadata. The EJB container provides transaction management services to session beans.
Persistence.xml
<persistence-unit name="MyUnitName" transaction-type="JTA"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <jta-data-source>jdbc/MyDataSource</jta-data-source> <properties> <property name="toplink.logging.level" value="INFO"/> </properties> </persistence-unit>
DAO (Session bean)
@Stateless @LocalBean public class EntityEJB implements EntityEJBRemote{ @PersistenceContext(unitName = "MyUnitName") private EntityManager em; ... public Entity createEntity(Entity entity) { em.persist(entity); return entity ; } }