Hibernate Ders06 - Inheritance Mapping - III
Sınıf başına bir tablo örneği iki şekilde yapılabilir.
Main.java
Personel.java
Maas.java
Araba.java
hibernate.hbm.xml
hibernate.cfg.xml
- Union altsınıf elemanları ile
- Her sınıf için kendi tablosu ile
Main.java
package test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Main { public static void main(String[] args) { Configuration cfg=new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Transaction t=session.beginTransaction(); Personel p = new Personel(); p.setAd("enes"); Maas m = new Maas(); m.setAd("enes2"); m.setMaas(3500); Araba a = new Araba(); a.setAd("enes3"); a.setMarka("opel"); a.setModel("astra"); session.persist(p); session.persist(m); session.persist(a); t.commit(); session.close(); System.out.println("basarili islem !"); } }
Personel.java
package test; public class Personel { private int id; private String ad; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAd() { return ad; } public void setAd(String ad) { this.ad = ad; } }
Maas.java
package test; public class Maas extends Personel { private int maas; public int getMaas() { return maas; } public void setMaas(int maas) { this.maas = maas; } }
Araba.java
package test; public class Araba extends Personel { private String marka; private String model; public String getMarka() { return marka; } public void setMarka(String marka) { this.marka = marka; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } }
hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="test.Personel" table="pers"> <id name="id"> <generator class="increment"></generator> </id> <property name="ad"></property> <union-subclass name="test.Maas" table="maas"> <property name="maas"></property> </union-subclass> <union-subclass name="test.Araba" table="araba"> <property name="marka"></property> <property name="model"></property> </union-subclass> </class> </hibernate-mapping>
hibernate.cfg.xml
<?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="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">**kullaniciAdiniz**</property> <property name="connection.password">**şifreniz**</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <mapping resource="hibernate.hbm.xml"/> </session-factory> </hibernate-configuration>
Yorumlar
Yorum Gönder