16 Aralık 2018 Pazar

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();
addNamedEntityGraph metodu - (String graphName, EntityGraph entityGraph)
Açıklaması şöyle.
This method add a named copy of the EntityGraph to the EntityManagerFactory.

addNamedQuery metodu - (String name, Query query)
Açıklaması şöyle.
This method is used to define the query, typed query, or stored procedure query as a named query such that future query objects can be created from it using the createNamedQuery or createNamedStoredProcedureQuery method.

close metodu
Şöyle yaparız.
emf.close();
createEntityManager metodu
Şöyle yaparız.
EntityManager em = emf.createEntityManager();
createEntityManager metodu - Map
Örnek ver

createEntityManager metodu - SynchronizationType
Açıklaması şöyle.
This method is used to create a new JTA application-managed EntityManager with the specified synchronization type.

createEntityManager metodu - SynchronizationType + Map map
Açıklaması şöyle.
This method used to create a new JTA application-managed EntityManager with the specified synchronization type and map of properties.

getCache metodu
Şöyle yaparız.
entityManager.getEntityManagerFactory().getCache().evictAll()
getMetamodel metodu
Açıklaması şöyle.
Return an instance of the Metamodel interface for access to the metamodel of the persistence unit.
unwrap metodu - Class cls)
Açıklaması şöyle.
Return an object of the specified type to allow access to the provider-specific API.

Hiç yorum yok:

Yorum Gönder