Hibernate继承映射方式一
继承映射方式一:一个子类一张表 ,一张表对应一个配置文件
场景:父类person,子类student、teacher
Person.java
Teacher.hbm.xml
测试类:
package com.fgh.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class ExtendMappingTest {private static SessionFactory sessionFactory;static {try {sessionFactory = new Configuration().configure().buildSessionFactory();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();Student student = new Student();student.setName("zhangsan");student.setCardId("213421342");Teacher teacher = new Teacher();teacher.setName("lilaoshi");teacher.setSalary(4000);session.save(student);session.save(teacher);tx.commit();} catch (Exception e) {e.printStackTrace();if (null != tx) {tx.rollback();}} finally {session.close();}}}