以动物为主题 定义一些属性,省略了公有方法 1.Animal private Integer id; private String name; private Character sex; private String type; 2.鸟类,继承了Animal public class Bird extends Animal { /** * */ private static final long serialVersionUID = 761910783876550018L; private Integer height; public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } } 3.猪类,继承了Animal public class Pig extends Animal { /** * */ private static final long serialVersionUID = -3994770594103154054L; private Integer weight; public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } } 4.Extend.hbm.xml映射文件 <hibernate-mapping package="com.sing.hibernate.entity"> <class name="Animal" table="s_animal"> <id name="id" column="a_id"> <generator class="sequence"> <param name="sequence">a_animal_seq</param> </generator> </id> <!-- 鉴别器,在这里是为了区分到底是猪还是鸟 --> <discriminator column="a_type" type="string" /> <property name="name" column="a_name" /> <!-- discriminator-value为鉴别器的值 --> <subclass name="Pig" discriminator-value="1"> <!-- 映射扩展属性 --> <property name="weight" column="a_weight" /> </subclass> <!-- discriminator-value为鉴别器的值 --> <subclass name="Bird" discriminator-value="2"> <!-- 映射扩展属性 --> <property name="height" column="a_height" /> </subclass> </class> <!-- 继承映射也可以采用下面这种方式 <subclass name="Pig" extends="Animal"> </subclass> --> </hibernate-mapping> 5.单元测试类 @Test public void save() { Session session = HibernateUtil.getSession(); try { session.beginTransaction(); Pig pig = new Pig(); pig.setName("猪八戒"); pig.setSex('女'); pig.setWeight(100); Bird bird = new Bird(); bird.setName("小鸟"); bird.setSex('男'); bird.setHeight(500); session.save(pig); session.save(bird); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); } finally { HibernateUtil.closeSession(); } } @Test public void load() { Session session = HibernateUtil.getSession(); try { session.beginTransaction(); Pig pig = (Pig) session.load(Pig.class, 1); System.out.println(pig.getName()); System.out.println(pig.getWeight()); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); } finally { HibernateUtil.closeSession(); } } @Test public void load2() { Session session = HibernateUtil.getSession(); try { session.beginTransaction(); Animal animal = (Animal) session.get(Animal.class, 1); System.out.println(animal.getName()); // 因为load方法加载,Animal返回的是一个代理类对象 // load默认是不支持多态查询,可以采用把<class>标签上面的lazy属性设置为false即可 // get直接支持多态查询 if (animal instanceof Pig) { System.out.println("-------animal--pig" + animal.getClass()); } else { System.out.println("-------animal--bird"); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); } finally { HibernateUtil.closeSession(); } } @Test public void load3() { Session session = HibernateUtil.getSession(); try { session.beginTransaction(); List<Animal> animals = session.createQuery("from Animal").list(); for (Animal animal : animals) { System.out.println("-------animal--pig" + animal.getClass()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); } finally { HibernateUtil.closeSession(); } }