18 Temmuz 2019 Perşembe

JPA Persistence Sınıfı - EntityManagerFactory Yaratır

Giriş
Şu satırı dahil ederiz.
import javax.persistence.Persistence;
Java SE ortamında Hibernate gibi ORM'leri ilklendirmek için kullanılır. src/main/resources/META-INF/persistence.xml dosyasını okur. Açıklaması şöyle. EntityManagerFactory nesnesi yaratır.
This class contains static methods used to obtain a EntityManagerFactory instance.
Unit Test
Eğer bu sınıfı Unit Test için kullanacaksak xml dosyasının yolu bu sefer şöyle
src/test/resources/META-INF/persistence.xml

createEntityManagerFactory metodu - XML
Belirtilen string ile persistance-unit tag'i içindeki name alanı aynı olmalıdır.
Örnek
Şöyle yaparız.
EntityManagerFactory emf = Persistence.createEntityManagerFactory ("myJPA");
Örnek
Şöyle yaparız.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("demo");
EntityManager em = emf.createEntityManager();
createEntityManagerFactory metodu - String + Map
Eğer veritabanı bağlantısı ile ilgili tüm ayarlar XML içinde belirtmez istemezsek bu metodu kullanıırz.

Örnek
Şöyle yaparız
public class JpaEntityManager {

  private EntityManagerFactory emFactory;

  private final String PERSISTENCE_UNIT_NAME = "JPATest";

  private final Map<String, String> properties=new HashMap<String, String>();

  // This Method Is Used To Retrieve The 'EntityManager' Object

  public EntityManager getEntityManager() {

    String url = String.format("%s/%s", DBConstants.DB_HOST, DBConstants.DB_NAME);

    properties.put("javax.persistence.jdbc.driver",DBConstants.DB_DRIVER);

    properties.put("javax.persistence.jdbc.url", url);

    properties.put("javax.persistence.jdbc.user", DBConstants.DB_USER);

    properties.put("javax.persistence.jdbc.password",DBConstants.DB_PASSWORD);

    properties.put("useSSL", DBConstants.DB_USE_SSL);

    properties.put("requireSSL", DBConstants.DB_REQUIRED_SSL);

    properties.put("serverTimezone",DBConstants.DB_SERVER_TIMEZONE);

    emFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME,properties);

    return emFactory.createEntityManager();

 }

}
Örnek
Şöyle yaparız.
EntityManagerFactory getEntityManagerFactory() {
  return Persistence.createEntityManagerFactory( getPersistenceUnitName(),
    getProperties() );
}

Map getProperties() {
  Map result = new HashMap();

  // Read the properties from a file instead of hard-coding it here.
  // Or pass the password in from the command-line.
  result.put( "javax.persistence.jdbc.password", "PASSWORD" );

  return result;
}
generateScheme metodu
Açıklaması şöyle
Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties.
Diğer Notlar
Bazı işlemler Persitence Context'i pas geçer. Açıklaması şöyle.

4.10 Bulk Update and Delete Operations

...
Bulk update maps directly to a database update operation, bypassing optimistic locking checks. Portable applications must manually update the value of the version column, if desired, and/or manually validate the value of the version column.
The persistence context is not synchronized with the result of the bulk update or delete.
Caution should be used when executing bulk update or delete operations because they may result in inconsistencies between the database and the entities in the active persistence context. In general, bulk update and delete operations should only be performed within a transaction in a new persistence context or before fetching or accessing entities whose state might be affected by such operations.




Hiç yorum yok:

Yorum Gönder