22 Mart 2018 Perşembe

JPA EntityManagerFactory Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.persistence.EntityManagerFactory;
Açıklaması şöyle. Container içinde çalışmayan kodlarda kullanılır.
The JPA specification defines two kinds of entity managers:

Application-managed—Entity managers are created when an application directly requests one from an entity manager factory. With application-managed entity managers, the application is responsible for opening or closing entity managers and involving the entity manager in transactions. This type of entity manager is most appropriate for use in standalone applications that don’t run in a Java EE container.

Container-managed—Entity managers are created and managed by a Java EE container. The application doesn’t interact with the entity manager factory at all. Instead, entity managers are obtained directly through injection or from JNDI. The container is responsible for configuring the entity manager factories. This type of entity manager is most appropriate for use by a Java EE container that wants to maintain some control over JPA configuration beyond what’s specified in persistence.xml.

Application-managed EntityManagers are created by an EntityManagerFactory obtained by calling the createEntityManagerFactory() method of the PersistenceProvider.
Meanwhile, container-managed EntityManagerFactorys are obtained through PersistenceProvider’s createContainerEntityManagerfactory() method.
Bu sınıf Persistence nesnesi tarafından yaratılır.

Hibernate bu arayüzü HibernateEntityManagerFactory sınıfı ile gerçekleştirir.
Spring bu arayüzü EntityManagerFactoryInfo sınıfı ile gerçekleştirir.

constructor
Şöyle yaparız
EntityManagerFactory emf = Persistence.createEntityManagerFactory("demojpa");
EntityManager em = emf.createEntityManager();
close metodu
Şöyle yaparız.
emf.close();
createEntityManager metodu
Şöyle yaparız.
EntityManager em = emf.createEntityManager();
getCache metodu
Şöyle yaparız.
entityManager.getEntityManagerFactory().getCache().evictAll()

Hiç yorum yok:

Yorum Gönder