@Test public void testSavePerson(){ //插入操作对于子类对象只需包记录擦人到一张表中。 // 辨别者列由hibernate自动维护。 Person person = new Person(); person.setAge(18); person.setName("pppp");
Student student = new Student() ; student.setSchool("zz"); student.setAge(128); student.setName("sss");
Hibernate: select person0_.hb_id as hb1_9_, person0_.hb_name as hb3_9_, person0_.hb_age as hb4_9_, person0_.hb_school as hb5_9_, person0_.type as type9_ from mage.hb_person person0_ [ Person{id=1, name='pppp', age=18}, Person{id=2, name='sss', age=128}, Person{id=3, name='pppp', age=18}, Person{id=4, name='sss', age=128} ]
Hibernate: select student0_.hb_id as hb1_9_, student0_.hb_name as hb3_9_, student0_.hb_age as hb4_9_, student0_.hb_school as hb5_9_ from mage.hb_person student0_ where student0_.type='student' [ Person{id=2, name='sss', age=128}, Person{id=4, name='sss', age=128} ] =destroy=
@Test public void testSavePerson(){ //插入操作对于子类对象只需包记录擦人到一张表中。 // 辨别者列由hibernate自动维护。 Person person = new Person(); person.setAge(18); person.setName("pppp");
Student student = new Student() ; student.setSchool("zz"); student.setAge(128); student.setName("sss");
Hibernate: select person0_.hb_id as hb1_9_, person0_.hb_name as hb2_9_, person0_.hb_age as hb3_9_, person0_1_.hb_school as hb2_10_, case when person0_1_.hb_id is not null then 1 when person0_.hb_id is not null then 0 end as clazz_ from mage.hb_person person0_ left outer join hb_student person0_1_ on person0_.hb_id=person0_1_.hb_id [Person{id=2, name='sss', age=128}, Person{id=1, name='pppp', age=18}] Hibernate: select student0_.hb_id as hb1_9_, student0_1_.hb_name as hb2_9_, student0_1_.hb_age as hb3_9_, student0_.hb_school as hb2_10_ from hb_student student0_ inner join mage.hb_person student0_1_ on student0_.hb_id=student0_1_.hb_id [Person{id=2, name='sss', age=128}] =destroy=
@Test public void testSavePerson(){ //插入操作对于子类对象只需包记录擦人到一张表中。 // 辨别者列由hibernate自动维护。 Person person = new Person(); person.setAge(18); person.setName("pppp");
Student student = new Student() ; student.setSchool("zz"); student.setAge(128); student.setName("sss");
Hibernate: select person0_.hb_id as hb1_9_, person0_.hb_name as hb2_9_, person0_.hb_age as hb3_9_, person0_.hb_school as hb1_10_, person0_.clazz_ as clazz_ from ( select hb_id, hb_name, hb_age, null as hb_school, 0 as clazz_ from mage.hb_person union select hb_id, hb_name, hb_age, hb_school, 1 as clazz_ from hb_student ) person0_ [Person{id=1, name='pppp', age=18}, Person{id=2, name='sss', age=128}] Hibernate: select student0_.hb_id as hb1_9_, student0_.hb_name as hb2_9_, student0_.hb_age as hb3_9_, student0_.hb_school as hb1_10_ from hb_student student0_ [Person{id=2, name='sss', age=128}] =destroy=
Process finished with exit code 0
无需使用辨别者列。 子类可以添加非空约束。 多了冗余字段。
更新操作
1 2 3 4 5
@Test public void testUpdatePerson(){ String hql = "update Person p set p.age = 120"; session.createQuery(hql).executeUpdate(); }
Hibernate: create temporary table if not exists HT_hb_person (hb_id integer not null) Hibernate: insert into HT_hb_person select person0_.hb_id as hb_id from ( select hb_id, hb_name, hb_age, null as hb_school, 0 as clazz_ from mage.hb_person union select hb_id, hb_name, hb_age, hb_school, 1 as clazz_ from hb_student ) person0_ Hibernate: update mage.hb_person set hb_age=120 where ( hb_id ) IN ( select hb_id from HT_hb_person ) Hibernate: update mage.hb_person set hb_age=120 where (hb_id ) IN ( select hb_id from HT_hb_person ) Hibernate: update hb_student set hb_age=120 where (hb_id) IN ( select hb_id from HT_hb_person)