Örnek

Persistence Bilgileri Map Olarak Veren Bir Örnek

persistence.xml içindeki özellikleri programatik olarak verebilirsiniz :
Map map = new HashMap();    
map.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
map.put("hibernate.hbm2ddl.auto", "create-drop");
map.put("hibernate.show_sql", "true");
map.put("javax.persistence.jdbc.driver", "org.hsqldb.jdbcDriver");
map.put("javax.persistence.jdbc.url", "jdbc:hsqldb:mem:unittests");
map.put("javax.persistence.jdbc.user", "sa");
map.put("javax.persistence.jdbc.password", "sa");

EntityManagerFactory entityManagerFactory=
	Persistence.createEntityManagerFactory("TestJPA",map);
		   
EntityManager entityManager=entityManagerFactory.createEntityManager();

Test t=new Test();
t.setId(1);
t.setName("Ali");

entityManager.getTransaction().begin();

entityManager.persist(t);

entityManager.getTransaction().commit();

entityManager.close();
		   
entityManagerFactory.close();
Bu örnekte persistence.xml aşağıdaki gibi olması yeterlidir:
<?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="TestJPA"
                            transaction-type="RESOURCE_LOCAL">
                            <description>JPA Test</description>
                            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
                            <class>com.test.entity.Test</class>
              </persistence-unit>
</persistence>
provider ve transactipon type de eklenebilir :
 
map.put("javax.persistence.provider", "org.hibernate.jpa.HibernatePersistenceProvider");
map.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
ve persistence.xml sadece aşağıdaki gibi olabilir:
<?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="TestJPA">
		<class>com.test.entity.Test</class>
	</persistence-unit>
</persistence>
zafer.teker , 09.02.2017

Bu Sayfayı Paylaş:

Fibiler Üyelerinin Yorumları


Tüm üyeler içeriklere yorum ekleyerek katkıda bulunabilir : Yorum Gir

Misafir Yorumları




Bu Sayfayı Paylaş:

İletişim Bilgileri

Takip Et

Her Hakkı Saklıdır. Bu sitede yayınlanan tüm bilgi ve fikirlerin kullanımından fibiler.com sorumlu değildir. Bu sitede üretilmiş , derlenmiş içerikleri, fibiler.com'u kaynak göstermek koşuluyla kendi sitenizde kullanılabilirsiniz. Ancak telif hakkı olan içeriklerin hakları sahiplerine aittir