(14)Hibernate继承映射-创新互联
成都网站设计、成都网站建设介绍好的网站是理念、设计和技术的结合。创新互联公司拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。
list = 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();
}
}
网页题目:(14)Hibernate继承映射-创新互联
URL分享:http://abwzjs.com/article/gcddj.html
人们总是在失去时,才了解到事情的本质 |
需求:动物、猫、猴子
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"); List生成的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表
总结:所有的子类都写到一个映射文件;父类不对应表; 每个子类对应一张表。 |
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页题目:(14)Hibernate继承映射-创新互联
URL分享:http://abwzjs.com/article/gcddj.html