大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“Hibernate的继承关系是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Hibernate的继承关系是什么”吧!
创新互联建站成立与2013年,是专业互联网技术服务公司,拥有项目成都网站设计、网站制作、外贸营销网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元宁夏做网站,已为上家服务,为宁夏各地企业和个人服务,联系电话:18982081108
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的继承关系是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!