hibernate继承映射
继承关系
一个类继承另外一个类。这2个类中就是继承关系。
需求:动物
猫
猴子
案例一
package loaderman.e_extends1; // 动物类 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; } }
package loaderman.e_extends1; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Cat.class) .buildSessionFactory(); } @Test public void getSave() { Session session = sf.openSession(); session.beginTransaction(); // 保存 // Cat cat = new Cat(); // cat.setName("大花猫"); // cat.setCatchMouse("抓小老鼠"); // session.save(cat); // 获取时候注意:当写hql查询的使用,通过父类查询必须写上类的全名 Query q = session.createQuery("from loaderman.e_extends1.Animal"); List<Animal> list = q.list(); System.out.println(list); session.getTransaction().commit(); session.close(); } }
package loaderman.e_extends1; public class Cat extends Animal{ // 抓老鼠 private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } }
package loaderman.e_extends1; public class Monkey { // 吃香蕉 private String eatBanana; }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 简单继承 --> <hibernate-mapping package="loaderman.e_extends1"> <class name="Cat" table="t_Cat"> <!-- 简单继承映射: 父类属性直接写 --> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="catchMouse"></property> </class> </hibernate-mapping>
案例二
package loaderman.e_extends2; 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; } }
package loaderman.e_extends2; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void getSave() { Session session = sf.openSession(); session.beginTransaction(); // 保存 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); Monkey m = new Monkey(); m.setName("猴子"); m.setEatBanana("吃10个香蕉"); // 保存 session.save(cat); session.save(m); session.getTransaction().commit(); session.close(); } }
package loaderman.e_extends2; public class Cat extends Animal{ private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } }
package loaderman.e_extends2; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 继承映射, 所有的子类都映射到一张表 --> <hibernate-mapping package="loaderman.e_extends2"> <class name="Animal" table="t_animal"> <id name="id"> <generator class="native"></generator> </id> <!-- 指定鉴别器字段(区分不同的子类) --> <discriminator column="type_"></discriminator> <property name="name"></property> <!-- 子类:猫 每个子类都用subclass节点映射 注意:一定要指定鉴别器字段,否则报错! 鉴别器字段:作用是在数据库中区别每一个子类的信息, 就是一个列 discriminator-value="cat_" 指定鉴别器字段,即type_字段的值 如果不指定,默认为当前子类的全名 --> <subclass name="Cat" discriminator-value="cat_"> <property name="catchMouse"></property> </subclass> <!-- 子类:猴子 --> <subclass name="Monkey" discriminator-value="monkey_"> <property name="eatBanana"></property> </subclass> </class> </hibernate-mapping>
案例三
package loaderman.e_extends3; 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; } }
package loaderman.e_extends3; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void getSave() { Session session = sf.openSession(); session.beginTransaction(); // 保存 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); Monkey m = new Monkey(); m.setName("猴子"); m.setEatBanana("吃10个香蕉"); // 保存 session.save(cat); session.save(m); session.getTransaction().commit(); session.close(); } }
package loaderman.e_extends3; public class Cat extends Animal{ private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } }
package loaderman.e_extends3; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 继承映射, 每个类对应一张表(父类也对应表) --> <hibernate-mapping package="loaderman.e_extends3"> <class name="Animal" table="t_animal"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <!-- 子类:猫 t_cat key 指定_cat表的外键字段 --> <joined-subclass name="Cat" table="t_cat"> <key column="t_animal_id"></key> <property name="catchMouse"></property> </joined-subclass> <!-- 子类:猴子 t_monkey --> <joined-subclass name="Monkey" table="t_monkey"> <key column="t_animal_id"></key> <property name="eatBanana"></property> </joined-subclass> </class> </hibernate-mapping>
案例四
package loaderman.e_extends4; public 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; } }
package loaderman.e_extends4; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void getSave() { Session session = sf.openSession(); session.beginTransaction(); // 保存 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); Monkey m = new Monkey(); m.setName("猴子"); m.setEatBanana("吃10个香蕉"); // 保存 session.save(cat); session.save(m); session.getTransaction().commit(); session.close(); } }
package loaderman.e_extends4; public class Cat extends Animal{ private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } }
package loaderman.e_extends4; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 继承映射, 每个类对应一张表(父类不对应表) --> <hibernate-mapping package="loaderman.e_extends4"> <!-- abstract="true" 指定实体类对象不对应表,即在数据库段不生成表 --> <class name="Animal" abstract="true"> <!-- 如果用union-subclass节点,主键生成策略不能为自增长! --> <id name="id"> <generator class="uuid"></generator> </id> <property name="name"></property> <!-- 子类:猫 t_cat union-subclass table 指定为表名, 表的主键即为id列 --> <union-subclass name="Cat" table="t_cat"> <property name="catchMouse"></property> </union-subclass> <!-- 子类:猴子 t_monkey --> <union-subclass name="Monkey" table="t_monkey"> <property name="eatBanana"></property> </union-subclass> </class> </hibernate-mapping>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!