這篇文章主要講解了“Hibernate的繼承關(guān)系是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Hibernate的繼承關(guān)系是什么”吧!
創(chuàng)新互聯(lián)建站成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元寧夏做網(wǎng)站,已為上家服務(wù),為寧夏各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
Hibernate的繼承關(guān)系分為三種:一張表對(duì)應(yīng)一整棵類繼承樹、一個(gè)類對(duì)應(yīng)一張表、每一個(gè)具體類對(duì)應(yīng)一張表。
三個(gè)java類Person、Student、Teacher,Student和Teacher繼承Person類并且Student和Teacher沒有任何關(guān)系
代碼如下:
Person.java
package com.zzn.hibernate.model; public class Person { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Student.java
package com.zzn.hibernate.model; public class Student extends Person { private String cardId; public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } }
Teacher.java
package com.zzn.hibernate.model; public class Teacher extends Person { private int salary; public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
Hibernate的繼承關(guān)系表:一張表對(duì)應(yīng)一整棵類繼承樹時(shí)
Person.hbm.xml
< ?xml version="1.0" encoding='gb2312'?> < !DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> < hibernate-mapping package="com.zzn.hibernate.model"> < class name="Person" table="person"> < id name="id" column="id"> < generator class="identity" /> < /id> < discriminator column="personType" type="java.lang.String" /> < !-- 指定描述符對(duì)應(yīng)的列明,注意discrimination標(biāo)簽只能緊跟在id標(biāo)簽后面 --> < property name="name" type="java.lang.String" /> < !-- 公用的name屬性,被兩個(gè)sbuclass標(biāo)簽所用 --> < property name="age" type="java.lang.Integer" /> < !-- 公用的age屬性,被兩個(gè)sbuclass標(biāo)簽所用 --> < subclass name="Student" discriminator-value="student"> < !-- 映射子類Student,描述符是student,此時(shí)已經(jīng)映射Student類,所以不必再寫Student.hbm.xml --> < property name="cardId" type="java.lang.String" /> < !-- Student私有的屬性 --> < /subclass> < subclass name="Teacher" discriminator-value="teacher" > < property name="salary" type="java.lang.Integer" /> < !-- Teacher私有的屬性 --> < /subclass> < /class> < /hibernate-mapping>
一個(gè)類對(duì)應(yīng)一張表
< ?xml version="1.0" encoding='gb2312'?> < !DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> < hibernate-mapping package="com.zzn.hibernate.model"> < class name="Person" table="person"> < id name="id" column="id"> < generator class="identity" /> < /id> < property name="name" type="java.lang.String" /> < property name="age" type="java.lang.Integer" /> < joined-subclass name="Student" table="student"> < key column="id" /> < property name="cardId" type="java.lang.String" /> < /joined-subclass> < joined-subclass name="Teacher" table="teacher"> < key column="id"/> < property name="salary" type="java.lang.Integer" /> < /joined-subclass> < /class> < /hibernate-mapping>
hibernate.hbm.xml
< ?xml version='1.0' encoding='UTF-8'?> < !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> < hibernate-configuration> < session-factory> < property name="show_sql">true< /property> < property name="hibernate.hbm2ddl.auto">update< /property> < property name="connection.username">sa< /property> < property name="connection.password">< /property> < property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;databasename=hibernate_test< /property> < property name="dialect">org.hibernate.dialect.SQLServerDialect< /property> < property name="myeclipse.connection.profile">SQL2005< /property> < property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver< /property> < mapping resource="com/zzn/hibernate/model/Person.hbm.xml"/> < /session-factory> < /hibernate-configuration>
Hibernate的繼承關(guān)系測(cè)試文件SubclassTest.java
package com.zzn.hibernate.test; import java.util.Iterator; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.zzn.hibernate.model.Person; import com.zzn.hibernate.model.Student; import com.zzn.hibernate.model.Teacher; public class SubClassTest { public static void main(String[] args) { add(); select(); } public static void add() { Configuration configuration = null; SessionFactory sessionFactory = null; Session session = null; Transaction transaction = null; Student student = new Student(); Teacher teacher = new Teacher(); try { configuration = new Configuration(); sessionFactory = configuration.configure().buildSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); student.setName("shanshan"); student.setAge(25); student.setCardId("123456"); teacher.setName("xiaolan"); teacher.setAge(25); teacher.setSalary(5000); session.save(student); session.save(teacher); } finally { if (session != null) { transaction.commit(); session.close(); } } } public static void select(){ Configuration configuration = null; SessionFactory sessionFactory = null; Session session = null; try { configuration = new Configuration(); sessionFactory = configuration.configure().buildSessionFactory(); session = sessionFactory.openSession(); Query query = session.createQuery("from Person"); Iterator iterator = query.list().iterator(); while (iterator.hasNext()){ Person person = (Person)iterator.next(); System.out.println(person.getName()); } } finally { if (session != null) { session.close(); } } } }
感謝各位的閱讀,以上就是“Hibernate的繼承關(guān)系是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Hibernate的繼承關(guān)系是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!