18 Temmuz 2019 Perşembe

JPA persistence.xml

Giriş
Bu xml javax.persistence.Persistence sınfı tarafından kullanılır. Bu dosya genellikle src/main/resources/META-INF/ dizininde bulunur

persistence Tag
persistence/xmlns Alanı
Şöyle yaparız.
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
persistence//xmlns::xsi Alanı
şöyle yaparız.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
persistence/xsi::schemaLocation Alanı
Şöyle yaparız.
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence  
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
persistence/version Alanı
Sürüm 2.1 için şöyle yaparız.
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    ...
</persistence>
persistence-unit Tag
persistence-unit bir tane olmalıdır. Aynı xml içinde iki tane tanımlanamaz.
<persistence...>
  <persistence-unit name="phase1-pu">...</persistence-unit>
  <persistence-unit name="phase2-pu">...</persistence-unit>
</persistence>  
İçinde
1. description,
2. provider,
3.properties
tag'leri bulunur.

persistence-unit/name Alanı
Açıklaması şöyle.
Here, the persistence unit name must be unique. Also, we can add multiple persistence-units for different units. 
persistence-unit/transaction-type Alanı
Açıklaması şöyle.
There are two types of transactions:

- JTA: The transactions will be managed by the Application Server
- RESOURCE_LOCAL: The transaction will be managed by the JPA Provider Implementation in use.
Örnek
Şöyle yaparız.
transaction-type="RESOURCE_LOCAL"
class Tag
Hibernate @Entity olarak işaretli sınıfları otomatik bulur. Eğer kullandığımız provider otomatik bulmuyorsa sınıfları bu tag ile belirtmek gerekir.
Açıklaması şöyle.
Also, specify the class name for entities in this configuration file. 
Örnek
Şöyle yaparız.
<persistence-unit name="Database" transaction-type="RESOURCE_LOCAL">
  <class>models.Employee</class>
  <provider>...</provider>
  <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa"/>
    <property name="javax.persistence.jdbc.user" value="root"/>
    <property name="javax.persistence.jdbc.password" value="root"/>
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  </properties>
</persistence-unit>
provider tag
Örnek
Şöyle yaparız.
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
Örnek
Şöyle 
yaparız.
<provider>org.hibernate.ejb.HibernatePersistence</provider>
Örnek
Eclipse için persistence.xml dosyası 
şöyledir.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 
  xmlns="..."   
  xmlns:xsi="..."    xsi:schemaLocation="...">
  <persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL">
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
  <class>com.tutorialspoint.eclipselink.entity.Employee</class>
  <properties>
   <property name="javax.persistence.jdbc.url" value="jdbc:mysql://..."/>
   <property name="javax.persistence.jdbc.user" value="root"/>
   <property name="javax.persistence.jdbc.password" value="removed"/>
   <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  </properties>
 </persistence-unit>
</persistence>
properties Tag
Açıklaması şöyle. Bu alandaki değerler XML içinde olmak zorunda değil. Persistence sınıfı içinde de belirtilebilir.
There is one more configuration used for the database connection. But for dynamic configuration of database connection, we add that configuration in Java code instead of this XML file.
Örnek
Şöyle yaparız.
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1"
  xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                      http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="dest" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.quaternion.destination.OtherThing</class>
      <-- lot of other entity classes created by the IDE/Hibernates JPA Model Gen-->
      <properties>
        <property name="javax.persistence.jdbc.url"
          value="jdbc:postgresql://localhost:5432/dest"/>
        <property name="javax.persistence.jdbc.user" value="a_user"/>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.cache.provider_class"
          value="org.hibernate.cache.NoCacheProvider"/>
        <property name="javax.persistence.schema-generation.database.action"
          value="none"/>
        <property name="spring.jpa.database-platform"
          value="org.hibernate.dialect.PostgreSQLDialect"/>
         <property name = "hibernate.show_sql" value = "true" />
      </properties>
  </persistence-unit>
  <persistence-unit name="source" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.quaternion.source.Thing</class>
      <-- lot of other entity classes created by the IDE/Hibernates JPA Model Gen-->
      <properties>
        <property name="javax.persistence.jdbc.url"
         value="jdbc:postgresql://localhost:5432/source"/>
        <property name="javax.persistence.jdbc.user" value="a_user"/>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.cache.provider_class"
         value="org.hibernate.cache.NoCacheProvider"/>
        <property name="javax.persistence.schema-generation.database.action"
         value="none"/>
        <property name="spring.jpa.database-platform"
         value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name = "hibernate.show_sql" value = "true" />
    </properties>
  </persistence-unit>
 </persistence>
Örnek
Jakarta ile şöyle yaparız
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0">
  <persistence-unit name="jpa-demo-local" transaction-type="RESOURCE_LOCAL">
    <properties>
      <property name="jakarta.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/jpa_demo"/>
      <property name="jakarta.persistence.jdbc.user" value="user"/>
      <property name="jakarta.persistence.jdbc.password" value="password"/>
      <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

Hiç yorum yok:

Yorum Gönder