(14)Hibernate继承映射
人们总是在失去时,才了解到事情的本质 |
赤峰林西网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。成都创新互联公司2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
需求:动物、猫、猴子
1、简单继承映射
Animal.java
package com.rk.hibernate.n_inheritance1; public abstract class Animal { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Cat.java
package com.rk.hibernate.n_inheritance1; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }
Cat.hbm.xml
App.java
package com.rk.hibernate.n_inheritance1; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Cat.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //保存 session.save(cat); session.getTransaction().commit(); session.close(); } @Test public void testGetCat() { Session session = sf.openSession(); session.beginTransaction(); //通过HQL查询Cat Query q = session.createQuery("from Cat"); Listlist = q.list(); System.out.println(list); session.getTransaction().commit(); session.close(); } @Test public void testGetAnimal() { Session session = sf.openSession(); session.beginTransaction(); // 获取时候注意:当写hql查询的使用,通过父类查询必须写上类的全名 Query q = session.createQuery("from com.rk.hibernate.n_inheritance1.Animal"); List list = q.list(); System.out.println(list); session.getTransaction().commit(); session.close(); } }
生成的T_Cat表
总结:简单继承映射,有多少个子类,写多少个映射文件! |
2、继承映射
下面3种方法都是只使用一个映射文件(Animal.hbm.xml文件),但是不同的方法生成的数据库表的数量不同。
2.1、所有子类映射到同一张表(1张表)
什么情况用?
子类教多,且子类较为简单,即只有个别属性!
好处:因为使用一个映射文件, 减少了映射文件的个数。
缺点:(不符合数据库设计原则)
Animal.java
package com.rk.hibernate.n_inheritance2; public abstract class Animal { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Cat.java
package com.rk.hibernate.n_inheritance2; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }
Monkey.java
package com.rk.hibernate.n_inheritance2; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }
Animal.hbm.xml
App.java
package com.rk.hibernate.n_inheritance2; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //猴子 Monkey monkey = new Monkey(); monkey.setName("淘气猴"); monkey.setEatBanana("吃黄香蕉"); //保存 session.save(cat); session.save(monkey); session.getTransaction().commit(); session.close(); } }
生成的T_Animals表
总结:写法较为简单:所有子类用一个映射文件,且映射到一张表!但数据库设计不合理!不推荐用。 |
2.2、每个类映射一张表(3张表)
Animal.java
package com.rk.hibernate.n_inheritance3; public abstract class Animal { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Cat.java
package com.rk.hibernate.n_inheritance3; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }
Monkey.java
package com.rk.hibernate.n_inheritance3; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }
Animal.hbm.xml
App.java
package com.rk.hibernate.n_inheritance3; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //猴子 Monkey monkey = new Monkey(); monkey.setName("淘气猴"); monkey.setEatBanana("吃黄香蕉"); //保存 session.save(cat); session.save(monkey); session.getTransaction().commit(); session.close(); } }
生成的T_Animal_Each表
生成的T_Cat_Each表
生成的T_Monkey_Each表
总结:一个映射文件,存储所有的子类; 子类父类都对应表; 缺点:表结构比较负责,插入一条子类信息,需要用2条sql: 往父类插入、往子类插入! |
2.3、(推荐)每个子类映射一张表, 父类不对应表(2张表)
Animal.java
package com.rk.hibernate.n_inheritance4; public abstract class Animal { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Cat.java
package com.rk.hibernate.n_inheritance4; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }
Monkey.java
package com.rk.hibernate.n_inheritance4; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }
Animal.hbm.xml
App.java
package com.rk.hibernate.n_inheritance4; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //猴子 Monkey monkey = new Monkey(); monkey.setName("淘气猴"); monkey.setEatBanana("吃黄香蕉"); //保存 session.save(cat); session.save(monkey); session.getTransaction().commit(); session.close(); } }
生成的T_Animal_Final表,虽然会生成这个表,但是不会存储数据
生成的T_Cat_Final表
生成的T_Monkey_Final表
总结:所有的子类都写到一个映射文件;父类不对应表; 每个子类对应一张表。 |
文章名称:(14)Hibernate继承映射
本文来源:http://pwwzsj.com/article/jhgphj.html