标签:des c style class blog code
使用 subclass 进行映射:将域模型中的每一个实体对象映射到一个独立的表中,也就是说不用在关系数据模型中考虑域模型中的继承关系和多态。
使用 joined-subclass 进行映射: 对于继承关系中的子类使用同一个表,这就需要在数据库表中增加额外的区分子类类型的字段。
使用 union-subclass 进行映射:域模型中的每个类映射到一个表,通过关系数据模型中的外键来描述表之间的继承关系。这也就相当于按照域模型的结构来建立数据库中的表,并通过外键来建立表之间的继承关系。
采用 subclass 元素的继承映射
package com.atguigu.hibernate.subclass; public class Person { private Integer id; private String name; private int age; public Integer getId() { return id; } public void setId(Integer 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; } }
package com.atguigu.hibernate.subclass; public class Student extends Person{ private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.atguigu.hibernate.subclass"> <class name="Person" table="PERSONS" discriminator-value="PERSON"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <!-- 配置辨别者列 --> <discriminator column="TYPE" type="string"></discriminator> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <!-- 映射子类 Student, 使用 subclass 进行映射 --> <subclass name="Student" discriminator-value="STUDENT"> <property name="school" type="string" column="SCHOOL"></property> </subclass> </class> </hibernate-mapping>
package com.atguigu.hibernate.subclass; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import java.util.List; import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.jdbc.Work; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; public class HibernateTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init(){ Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } @After public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } /** * 缺点: * 1. 使用了辨别者列. * 2. 子类独有的字段不能添加非空约束. * 3. 若继承层次较深, 则数据表的字段也会较多. */ /** * 查询: * 1. 查询父类记录, 只需要查询一张数据表 * 2. 对于子类记录, 也只需要查询一张数据表 */ @Test public void testQuery(){ List<Person> persons = session.createQuery("FROM Person").list(); System.out.println(persons.size()); List<Student> stus = session.createQuery("FROM Student").list(); System.out.println(stus.size()); } /** * 插入操作: * 1. 对于子类对象只需把记录插入到一张数据表中. * 2. 辨别者列有 Hibernate 自动维护. */ @Test public void testSave(){ Person person = new Person(); person.setAge(11); person.setName("AA"); session.save(person); Student stu = new Student(); stu.setAge(22); stu.setName("BB"); stu.setSchool("ATGUIGU"); session.save(stu); } }
package com.atguigu.hibernate.joined.subclass; public class Person { private Integer id; private String name; private int age; public Integer getId() { return id; } public void setId(Integer 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; } }
package com.atguigu.hibernate.joined.subclass; public class Student extends Person{ private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.atguigu.hibernate.joined.subclass"> <class name="Person" table="PERSONS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <joined-subclass name="Student" table="STUDENTS"> <key column="STUDENT_id"></key> <property name="school" type="string" column="SCHOOL"></property> </joined-subclass> </class> </hibernate-mapping>
package com.atguigu.hibernate.joined.subclass; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; public class HibernateTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init(){ Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } @After public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } /** * 优点: * 1. 不需要使用了辨别者列. * 2. 子类独有的字段能添加非空约束. * 3. 没有冗余的字段. */ /** * 查询: * 1. 查询父类记录, 做一个左外连接查询 * 2. 对于子类记录, 做一个内连接查询. */ @Test public void testQuery(){ List<Person> persons = session.createQuery("FROM Person").list(); System.out.println(persons.size()); List<Student> stus = session.createQuery("FROM Student").list(); System.out.println(stus.size()); } /** * 插入操作: * 1. 对于子类对象至少需要插入到两张数据表中. */ @Test public void testSave(){ Person person = new Person(); person.setAge(11); person.setName("AA"); session.save(person); Student stu = new Student(); stu.setAge(22); stu.setName("BB"); stu.setSchool("ATGUIGU"); session.save(stu); } }
package com.atguigu.hibernate.union.subclass; public class Person { private Integer id; private String name; private int age; public Integer getId() { return id; } public void setId(Integer 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; } }
package com.atguigu.hibernate.union.subclass; public class Student extends Person{ private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.atguigu.hibernate.union.subclass"> <class name="Person" table="PERSONS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="hilo" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <union-subclass name="Student" table="STUDENTS"> <property name="school" column="SCHOOL" type="string"></property> </union-subclass> </class> </hibernate-mapping>
package com.atguigu.hibernate.union.subclass; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; public class HibernateTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init(){ Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } @After public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } @Test public void testUpdate(){ String hql = "UPDATE Person p SET p.age = 20"; session.createQuery(hql).executeUpdate(); } /** * 优点: * 1. 无需使用辨别者列. * 2. 子类独有的字段能添加非空约束. * * 缺点: * 1. 存在冗余的字段 * 2. 若更新父表的字段, 则更新的效率较低 */ /** * 查询: * 1. 查询父类记录, 需把父表和子表记录汇总到一起再做查询. 性能稍差. * 2. 对于子类记录, 也只需要查询一张数据表 */ @Test public void testQuery(){ List<Person> persons = session.createQuery("FROM Person").list(); System.out.println(persons.size()); List<Student> stus = session.createQuery("FROM Student").list(); System.out.println(stus.size()); } /** * 插入操作: * 1. 对于子类对象只需把记录插入到一张数据表中. */ @Test public void testSave(){ Person person = new Person(); person.setAge(11); person.setName("AA"); session.save(person); Student stu = new Student(); stu.setAge(22); stu.setName("BB"); stu.setSchool("ATGUIGU"); session.save(stu); } }
标签:des c style class blog code
原文地址:http://blog.csdn.net/qilixiang012/article/details/27966029