Hibernate继承映射总结
Hibernate继承映射三种策略中:
每个类分层结构是一张表:
??? 表现在数据库中父类与子类在同一张表中,这样查询起来方便.
每个子类是一张表:
?? 表现在数据库中父类与子类分别在不同的表中,但子类只有子类定义的属性所对应的字段,这样做查询的时候会用到inner join或者是left join等.
?
最后一种是每个具体类是一张表:
?? 表现在数据库中也是父类与子类分别在不同的表中,不过子类也包括了父类的所有字段.这样如果直接从子类load就比较快,而如果从父类load的话sql就会比较麻烦.
比如第三种策略,如果我只load pig对象:
Pig p = (Pig)session.load(Pig.class, 1);
?
则发出sql如下:
Hibernate: /* load com.lwf.hibernate.extend1.Pig */ select pig0_.id as id0_0_, pig0_.name as name0_0_, pig0_.sex as sex0_0_, pig0_.weight as weight1_0_ from t_pig pig0_ where pig0_.id=?
?
?
而如果我load animal对象:
Animal a = (Animal)session.load(Animal.class, 1);
?
则发出sql如下:
Hibernate: /* load com.lwf.hibernate.extend1.Animal */ select animal0_.id as id0_0_, animal0_.name as name0_0_, animal0_.sex as sex0_0_, animal0_.weight as weight1_0_, animal0_.height as height2_0_, animal0_.clazz_ as clazz_0_ from ( select id, name, sex, weight, null as height, 1 as clazz_ from t_pig union select id, name, sex, null as weight, height, 2 as clazz_ from t_bird ) animal0_ where animal0_.id=?
?
看看就知道谁要方便...