人们总是在失去时,才了解到事情的本质 |
需求:动物、猫、猴子
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
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.hibernate.n_inheritance1" auto-import="true"> <class name="Cat" table="T_Cat"> <!-- 简单继承映射: 父类属性直接写 --> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <property name="catchMouse" column="catchMouse"></property> </class> </hibernate-mapping>
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<Cat> 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<Cat> list = q.list(); System.out.println(list); session.getTransaction().commit(); session.close(); } }
生成的T_Cat表
总结:简单继承映射,有多少个子类,写多少个映射文件! |
下面3种方法都是只使用一个映射文件(Animal.hbm.xml文件),但是不同的方法生成的数据库表的数量不同。
什么情况用?
子类教多,且子类较为简单,即只有个别属性!
好处:因为使用一个映射文件, 减少了映射文件的个数。
缺点:(不符合数据库设计原则)
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
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 继承映射, 所有的子类都映射到一张表 --> <hibernate-mapping package="com.rk.hibernate.n_inheritance2" auto-import="true"> <class name="Animal" table="T_Animals"> <!-- 简单继承映射: 父类属性直接写 --> <id name="id" column="id"> <generator class="native"></generator> </id> <!-- 指定鉴别器字段(区分不同的子类) --> <discriminator column="animal_Type"></discriminator> <property name="name" column="name"></property> <!-- 子类:猫 每个子类都用subclass节点映射 注意:一定要指定鉴别器字段,否则报错! 鉴别器字段:作用是在数据库中区别每一个子类的信息, 就是一个列(column) discriminator-value="cat" 指定鉴别器字段,即animal_Type字段的值 如果不指定,默认为当前子类的全名 --> <subclass name="Cat" discriminator-value="cat"> <property name="catchMouse" column="catchMouse"></property> </subclass> <!-- 子类:猴子 --> <subclass name="Monkey" discriminator-value="monkey"> <property name="eatBanana" column="eatBanana"></property> </subclass> </class> </hibernate-mapping>
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表
总结:写法较为简单:所有子类用一个映射文件,且映射到一张表!但数据库设计不合理!不推荐用。 |
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
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 继承映射, 每个类对应一张表(父类也对应表) --> <hibernate-mapping package="com.rk.hibernate.n_inheritance3" auto-import="true"> <class name="Animal" table="T_Animal_Each"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <!-- 子类:猫 t_cat key 指定_cat表的外键字段 --> <joined-subclass name="Cat" table="T_Cat_Each"> <key column="animal_Id"></key> <property name="catchMouse" column="catchMouse"></property> </joined-subclass> <!-- 子类:猴子 t_monkey --> <joined-subclass name="Monkey" table="T_Monkey_Each"> <key column="animal_Id"></key> <property name="eatBanana" column="eatBanana"></property> </joined-subclass> </class> </hibernate-mapping>
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: 往父类插入、往子类插入! |
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
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 继承映射, 每个类对应一张表(父类不对应表) --> <hibernate-mapping package="com.rk.hibernate.n_inheritance4" auto-import="true"> <!-- abstract="true" 指定实体类对象不对应表,即在数据库内不生成表 --> <class name="Animal" table="T_Animal_Final"> <!-- 如果用union-subclass节点,主键生成策略不能为自增长! --> <id name="id" column="id"> <generator class="uuid"></generator> </id> <property name="name" column="name"></property> <!-- 子类:猫 union-subclass table 指定为表名, 表的主键即为id列 --> <union-subclass name="Cat" table="T_Cat_Final"> <property name="catchMouse" column="catchMouse"></property> </union-subclass> <!-- 子类:猴子 --> <union-subclass name="Monkey" table="T_Monkey_Final"> <property name="eatBanana" column="eatBanana"></property> </union-subclass> </class> </hibernate-mapping>
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表
总结:所有的子类都写到一个映射文件;父类不对应表; 每个子类对应一张表。 |
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。