Persistence etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Persistence etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

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.




23 Ocak 2019 Çarşamba

JPA Generating DB Schema Parametreleri

Giriş
Parametreler şöyle.

javax.persistence.schema-generation.database.action
none, create, drop-and-create, drop değerleri olabilir.

javax.persistence.schema-generation.scripts.action
Örnek - create
create ise veritabanı yaratılır. Metadatan ddl cümlesi üretip dosyaya yazmak için şöyle yaparız
javax.persistence.schema-generation.scripts.action=create
javax.persistence.schema-generation.scripts.create-source=metadata
javax.persistence.schema-generation.scripts.create-target=create.sql
Örnek - drop-and-create
Şöyle yaparız
javax.persistence.schema-generation.scripts.action=drop-and-create
javax.persistence.sql-load-script-source=table-records.sql

javax.persistence.schema-generation.create-source
metadata, script, metadata-then-script, script-then-metadata değerleri olabilir.

- metadata ise kaynak koda bakarak ddl üretilir

- script ise create-script-source dosyası çalıştırılır

javax.persistence.schema-generation.drop-source
metadata, script, metadata-then-script, script-then-metadata olabilir.
script ise drop-script-source dosyası çalıştırılır

javax.persistence.schema-generation.create-database-schemas
Açıklaması şöyle. tablolar yaratmadan önce schema yaratılmalı. Bu komut ile önce schema yaratılır.
The JPA variant of hibernate.hbm2dll.create_namespaces. Specifies whether the persistence provider is to create the database schema(s) in addition to creating database objects (tables, sequences, constraints, etc). The value of this boolean property should be set to true if the persistence provider is to create schemas in the database or to generate DDL that contains "CREATE SCHEMA" commands. If this property is not supplied (or is explicitly false), the provider should not attempt to create database schemas.

javax.persistence.schema-generation.scripts.create-target
Örnek
Şöyle yaparız
javax.persistence.schema-generation.scripts.create-target=create.sql
ddl cümlelerini dosyaya yazar.

javax.persistence.schema-generation.scripts.drop-target
Metadataya göre drop edilmesi gereken nesneleri belirten dosyayı oluşturur.

javax.persistence.database-product-name
Örnek ver

javax.persistence.database-major-version
Örnek ver

javax.persistence.database-minor-version
Örnek ver

javax.persistence.schema-generation.create-script-source
Veritabanında tablo yaratmak için çalıştırılmasını istenen script dosyası

javax.persistence.schema-generation.drop-script-source
Veritabanında tablo silmek için çalıştırılmasını istenen script dosyası

javax.persistence.schema-generation.connection
Örnek ver

javax.persistence.sql-load-script-source
Veritabanı yaratıldıktan sonra doldurmak için çalıştırılacak dosyayı belirtir.