Hibernate Ders07 - Inheritance Mapping - IV
Sınıf başına bir tablo örneğini notasyonla yapalım.
Main.java
Personel.java
Maas.java
Araba.java
hibernate.cfg.xml
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; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.DiscriminatorType; import javax.persistence.Table; @Entity @Table(name = "perstab2") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Personel { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private int id; @Column(name = "ad") 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; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name="maas") @AttributeOverrides({ @AttributeOverride(name="id", column=@Column(name="idsi")), @AttributeOverride(name="ad", column=@Column(name="adi")) }) public class Maas extends Personel { // @Column(name="maas") demesem de olur. private int maas; public int getMaas() { return maas; } public void setMaas(int maas) { this.maas = maas; } }
Araba.java
package test; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name="araba") @AttributeOverrides({ @AttributeOverride(name="id", column=@Column(name="idsi")), @AttributeOverride(name="ad", column=@Column(name="adi")) }) 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.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 class="test.Personel"/> <mapping class="test.Maas"/> <mapping class="test.Araba"/> </session-factory> </hibernate-configuration>
Yorumlar
Yorum Gönder