这篇文章主要讲解了“Hibernate的继承关系是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Hibernate的继承关系是什么”吧!
Hibernate的继承关系分为三种:一张表对应一整棵类继承树、一个类对应一张表、每一个具体类对应一张表。
三个java类Person、Student、Teacher,Student和Teacher继承Person类并且Student和Teacher没有任何关系
代码如下:
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的继承关系表:一张表对应一整棵类继承树时
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" /> < !-- 指定描述符对应的列明,注意discrimination标签只能紧跟在id标签后面 --> < property name="name" type="java.lang.String" /> < !-- 公用的name属性,被两个sbuclass标签所用 --> < property name="age" type="java.lang.Integer" /> < !-- 公用的age属性,被两个sbuclass标签所用 --> < subclass name="Student" discriminator-value="student"> < !-- 映射子类Student,描述符是student,此时已经映射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>
一个类对应一张表
< ?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的继承关系测试文件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的继承关系是什么”的内容了,经过本文的学习后,相信大家对Hibernate的继承关系是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。