Hibernate 继承映射

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值