11 Aralık 2018 Salı

JPA persistence.xml

Giriş
JPA ile kullanılan persistence.xml  içinde genel ayarlar ve Hibernate'e öze ayarlar bulunur.

JPA ayarlarının bir kısmı javax.persistence.jdbc.abc = def şeklindedir.
Hibernate'e özel ayarlar hibernate.abc = def şeklindedir.

İskelet olarak şöyle yaparız.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="..." xmlns:xsi="..." xsi:schemaLocation="...">
 <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:postgresql:DATABASE"/>
    <property name="javax.persistence.jdbc.user" value="USERNAME"/>
    <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="..."/>
    <property name="hibernate.hbm2ddl.auto" value="validate"/>
  </properties>
 </persistence-unit>
</persistence>
Ayarların açıklamaları için hibernate.cfg.xml yazısına da bakabilirsiniz.

Hibernate ve MySQL
Şöyle yaparız.
<?xml version="1.0" encoding="UTF-8"?>
  <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="demo">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.archive.autodetection" value="class"/>
      <property name="hibernate.format_sql" value="true"/>

      <!-- Configuration-->
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/demo"/>
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MYSQLDialect"/>
      <property name="hbm2ddl.auto" value="create"/>

    </properties>
  </persistence-unit>
</persistence>
Hibernate ve PostgreSQL
Şöyle yaparız.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="..." xmlns:xsi="..." xsi:schemaLocation="...">
 <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:postgresql:DATABASE"/>
    <property name="javax.persistence.jdbc.user" value="USERNAME"/>
    <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="..."/>
    <property name="hibernate.hbm2ddl.auto" value="validate"/>
  </properties>
 </persistence-unit>
</persistence>
hibernate.generate_statistics
hibernate.cfg.xml Diğer Ayarlar yazısına taşıdım.

hibernate.hbm2ddl.import_files_sql_extractor
hibernate.cfg.xml Diğer Ayarlar yazısına taşıdım.

hibernate.integrator_provider
Şöyle yaparız.
<property name="hibernate.integrator_provider" value="com.mystuff.MyIntegratorProvider" />
hibernate.temp.use_jdbc_metadata_defaults
Şöyle yaparız.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="entityManager">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <properties>
        <property name="hibernate.dialect" 
                  value="org.hibernate.dialect.PostgreSQL9Dialect"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
      </properties>
  </persistence-unit>

  <persistence-unit name="sqlEntityManager">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <properties>
        <property name="hibernate.dialect"
                  value="org.hibernate.dialect.SQLServerDialect"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
      </properties>
    </persistence-unit>
</persistence>

Hiç yorum yok:

Yorum Gönder