Hibernate Ders-2 Persistence.xml Ve Hibernate.cfg.xml Tanıtım Ve İlk Kayıt
Merhaba arkadaşlar bu yazımda oluşturduğumuz hibernate projesinde oluşan persistence.xml ve hibernate.cfg.xml dosyalarının ne iş yaptığından ve kodlarından bahsetmek istiyorum. Öncelikli olarak JPA(Java Persistence Api)’den bahsetmek istiyorum.
JPA Nedir?
JPA(Java Persistence Api) isminden de anlaşılacağı üzere Java sınıflarını kalıcı hale getirir. Yani daha önceki yazımda dediğim gibi pojo sınıflarımızı tablolarımıza eşler ve verilerimizi kalıcı hale getirir. İşte bu işin dili JPA dır. Yani JPA bir kalıptır ve bu kalıbı kullanan frameworkler vardır. Bu frameworklerin en popüleri hibernate dir.
Persistence.xml nedir?
Bu xml dosyası projemizin jpa projesi olduğunu belirtir ve gerekli bağlantı ayarları bu dosya üzerinde yapılır. Şimdi temel bir persistence.xml dosyasında bulunan kodları açıklayalım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?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="Hibernate_Ders2_Tanıtım" transaction-type="RESOURCE_LOCAL"> <properties> <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/> <!-- Burada hibernate bğalntımızı yapıyoruz ve hibernate.cfg.xml dosyamızın yerini belirtiyoruz --> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernateornekler"/> <!--Veritabanı Bağlantımız --> <property name="javax.persistence.jdbc.user" value="root"/> <!-- Veritabanı kullanıcı Adımız --> <property name="javax.persistence.jdbc.password" value="19961903"/> <!-- Veritabanı bağlantı parolamız --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <!-- MySQL Driver Dosyamız --> </properties> </persistence-unit> </persistence> |
Hibernate.cfg.xml dosyası nedir?
Bu xml dosyası bizim hibernate ayarlarımızı, veritabanı ayarlarımızı oluşturmamızı sağlar. Hibernate kullanılan projelerde olmak zorundadır. Şimdi hibernate.cfg.xml dosyasındaki kodları açıklayalım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- MySQL driver dosyamız --> <property name="hibernate.connection.password">19961903</property> <!-- Veritabanı bağlantı şifremiz --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateornekler</property> <!-- Veritabanı bağlantımız --> <property name="hibernate.connection.username">root</property> <!-- Veritabanı kullanıcı adımız --> <property name="hibernate.show_sql">true</property> <!-- SQL sorgularımızın console ekranında görünmesini istiyoruz --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Eğer veritabanımızda gerekli tablo yoksa oluşturulsun varsa güncellenmesini istiyoruz --> </session-factory> </hibernate-configuration> |
Oluşturulan projenin dosya yolları şu şekildedir:
Şimdi basit bir proje yaparak yazımızı bitirelim. Yaptığımız proje personel tablosunda personel_isim,personel_id,personel_soyisim ve personel_maas kayıtlarını tutsun. Ve tablomuzu otomatik oluştursun.
Bu projemizi notasyonları kullanarak oluşturuyoruz. Öncelikli olarak sınıflarımızı oluşturuyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
package com.furkan.pojo; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity // classımızı kalıcı hale getiriyor(Kullanılması zorunlu) @Table(name="PERSONEL") // Oluşturulacak tablomuza isim atadık public class Personel implements Serializable { private Integer personel_id; private String personel_isim; private String personel_soyisim; private Integer personel_maas; @Id // Bu değişkenin id değeri olduğunu belirtiyoruz(Kullanılması zorunlu) @Column(name="PERSONEL_ID") // Kolonumuzun ismini belirledik public Integer getPersonel_id() { return personel_id; } public void setPersonel_id(Integer personel_id) { this.personel_id = personel_id; } @Column(name="PERSONEL_ISIM") // Kolonumuzun ismini belirledik public String getPersonel_isim() { return personel_isim; } public void setPersonel_isim(String personel_isim) { this.personel_isim = personel_isim; } @Column(name="PERSONEL_SOYISIM") // Kolonumuzun ismini belirledik public String getPersonel_soyisim() { return personel_soyisim; } public void setPersonel_soyisim(String personel_soyisim) { this.personel_soyisim = personel_soyisim; } @Column(name="PERSONEL_MAAS") // Kolonumuzun ismini belirledik public Integer getPersonel_maas() { return personel_maas; } public void setPersonel_maas(Integer personel_maas) { this.personel_maas = personel_maas; } } |
Bu sınıfımızdaki notasyonlara bakalım :
@Entity => Bu notasyon mutlaka kalıcı hale gelmesini istediğimiz sınıfta olmak zorundadır.Sınıfımızı kalıcı hale getirir.
@Id => Bu notasyon mutlaka kalıcı hale gelmesini istediğimiz sınıfda bir değişkende olmak zorundadır. İd değerimizi belirtir.
@Table => Tablomuza gerekli ayarlamalar yapılır
@Column => Kolonlara gerekli ayarlamalar yapılır
Projemiz içi oluşturduğumuz hibernate.cfg.xml dosyamız :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- MySQL driver dosyamız --> <property name="hibernate.connection.password">19961903</property> <!-- Veritabanı bağlantı şifremiz --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateornekler</property> <!-- Veritabanı bağlantımız --> <property name="hibernate.connection.username">root</property> <!-- Veritabanı kullanıcı adımız --> <property name="hibernate.show_sql">true</property> <!-- SQL sorgularımızın console ekranında görünmesini istiyoruz --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Eğer veritabanımızda gerekli tablo yoksa oluşturulsun varsa güncellenmesini istiyoruz --> <mapping class="com.furkan.pojo.Personel"/> <!-- Oluşturduğumuz Sınıfı Haritlanmasını İstedik --> </session-factory> </hibernate-configuration> |
Burada mapping class tagı ile haritaladığımız classlarımızı tanıtıyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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="Hibernate_Ders2_Tanıtım" transaction-type="RESOURCE_LOCAL"> <class>com.furkan.pojo.Personel</class> <properties> <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/> <!-- Burada hibernate bğalntımızı yapıyoruz ve hibernate.cfg.xml dosyamızın yerini belirtiyoruz --> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernateornekler"/> <!--Veritabanı Bağlantımız --> <property name="javax.persistence.jdbc.user" value="root"/> <!-- Veritabanı kullanıcı Adımız --> <property name="javax.persistence.jdbc.password" value="19961903"/> <!-- Veritabanı bağlantı parolamız --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <!-- MySQL Driver Dosyamız --> </properties> </persistence-unit> </persistence> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.furkan.Test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.furkan.pojo.Personel; public class Test { public static void main(String[] args) { SessionFactory factory=new Configuration().configure().buildSessionFactory(); // SessionFactory Nesnesi Üretiliyor Session session=factory.openSession(); //Session Nesnesi Üretiliyor Transaction transaction=session.getTransaction(); // Transaction Nesnesi Üretiliyor Personel personel=new Personel(); //Kalıcı Sınıfımızdan Nesne Üretiliyor //Gerekli atamalar yapılıyor personel.setPersonel_id(18); personel.setPersonel_isim("Furkan"); personel.setPersonel_soyisim("KOC"); personel.setPersonel_maas(4250); try { transaction.begin(); // İşlem başlatılıyor session.save(personel); //personel nesnemiz kalıcı hale getiriliyor tablomuza ekleniyor transaction.commit(); // işlem gerçekleştiriliyor } catch (Exception e) { transaction.rollback(); // Eğer herhangi bir hata meydana gelirse işlemler geri alınıyor System.out.println("Hata Var :"+e.toString()); } finally { session.close(); //Session nesnemizi kapatıyoruz } } } |
SessionFactory => Önbellekte tutulan nesnelerin içerdiği verilerin haritalama işlemlerinde görevlidir.
Session => Oturum nesneleri uygulama ve kalıcılık katmanları arasındaki iletişimin kurulmasını sağlar.
Transaction => Nesnelerin kalıcı hale getirilmesinin daha güvenli ve tutarlı olmasını sağlar.
Save() => Nesne kalıcı hale getiriliyor
Console ekranımız ve oluşturulan sql kodları :
Görüldüğü üzere tablomuz oluşturuldu ve veriler eklendi:
Bu dersinde sonuna geldik daha sonraki yazılarımda görüşmek üzere…