数据模型
表person
表student
表worker
对象模型
Person private String id;private String name;private int age;private String sex;
Student extends Person private String school;
Worker extends Person private String factory;
xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.skuedu.bean"><class name="Person" table="person"><id name="id" length="32"><generator class="uuid"></generator></id><property name="name" length="30"></property><property name="age" ></property><property name="sex" length="10"></property><union-subclass name="Student" table="student"><property name="school"></property></union-subclass><union-subclass name="Worker" table="worker" ><property name="factory"></property></union-subclass></class> </hibernate-mapping>
测试代码:
@Testpublic void testSaveStudent(){Session session = HibernateUtil.getSession();Transaction transaction = session.beginTransaction();try{Student student = new Student();student.setAge(20);student.setName("小王");student.setSchool("清华大学");student.setSex("男");session.save(student);transaction.commit();}catch(Exception e){transaction.rollback(); }finally{HibernateUtil.closeSession(session);}}@Testpublic void testSaveWorker(){Session session = HibernateUtil.getSession();Transaction transaction = session.beginTransaction();try{Worker worker = new Worker();worker.setAge(20);worker.setName("小李");worker.setFactory("首钢");worker.setSex("男");session.save(worker);transaction.commit();}catch(Exception e){transaction.rollback(); }finally{HibernateUtil.closeSession(session);}}@Testpublic void testFind(){}