public class Staff { /** * 主键 */ private int id; /** * 员工姓名 */ private String staffName; ..... } /** * 正式工 */ public class FormalStaff extends Staff { /** * 编号 */ private String staffId; ..... } /** * 临时工 */ public class TempStaff extends Staff { /** * 期限 */ private String date; ............... }
<class name="Staff" table="hib_staff"> <id name="id" type="integer" column="sid"> <generator class="native"></generator> </id> <property name="staffName" type="string" length="20"/> <!-- 正式工 --> <joined-subclass name="FormalStaff" table="hib_formal_staff"> <key column="sid"></key> <property name="staffId"></property> </joined-subclass> <!-- 临时工 --> <joined-subclass name="TempStaff" table="hib_temp_staff"> <key column="sid"></key> <property name="date"></property> </joined-subclass>
创建表的表结构
mysql> desc hib_staff; +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | sid | int(11) | NO | PRI | NULL | auto_increment | | staffName | varchar(20) | YES | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> desc hib_formal_staff; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | sid | int(11) | NO | PRI | NULL | | | staffId | varchar(255) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> desc hib_temp_staff; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | sid | int(11) | NO | PRI | NULL | | | date | varchar(255) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+
// 测试
Session s = HibernateSessionFactory.getSession(); s.getTransaction().begin(); // Staff staff = new Staff(); // staff.setStaffName("staff_01"); // s.save(staff); // // FormalStaff formal = new FormalStaff(); // formal.setStaffName("staff_formal_01"); // formal.setStaffId("1111"); // s.save(formal); // TempStaff temp = new TempStaff(); // temp.setStaffName("staff_temp_01"); // temp.setDate("20100610"); // s.save(temp); Criteria c = s.createCriteria(FormalStaff.class); List ls = c.list(); Iterator it = ls.iterator(); while(it.hasNext()){ FormalStaff staff = (FormalStaff) it.next(); System.out.println(staff.getId()); System.out.println(staff.getStaffName()); System.out.println(staff.getStaffId()); } s.getTransaction().commit();
save 执行的sql 分别是 :
Hibernate: insert into hib_staff (staffName) values (?)
Hibernate: insert into hib_staff (staffName) values (?)
Hibernate: insert into hib_temp_staff (staffId, sid) values (?, ?)
Hibernate: insert into hib_staff (staffName) values (?)
Hibernate: insert into hib_temp_staff (date, sid) values (?, ?)
查询子类 FormalStaff 的时候:
select this_.sid as sid0_0_, this_1_.staffName as staffName0_0_, this_.staffId as staffId1_0_ from hib_formal_staff this_ inner join hib_staff this_1_ on this_.sid=this_1_.sid