hibernate中继承映射配置详细解析(二)

   
           本例模仿的是所有子类映射到一张表 (1张表)
      

         什么情况用?

             子类教多,且子类较为简单,即只有个别属性!

             好处:因为使用一个映射文件, 减少了映射文件的个数。

             缺点:(不符合数据库设计原则)

        一个映射文件: Animal.hbm.xml

1.hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- 通常,一个session-factory节点代表一个数据库 -->
	<session-factory>
		<!-- 1.数据库连接配置 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hib-demo</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<!--数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql  -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		
		<!-- 2.其他相关配置 -->
		   <!--2.1显示hibernate在运行的时候执行的sql语句  -->
		<property name="hibernate.show_sql">true</property>
		   <!-- 2.2格式化sql 
		<property name="hibernate.format_sql">true</property>-->
		<!--    2.3自动建表  -->
		<property name="hibernate.hbm2ddl.auto">create</property>
		<!-- 3.加载所有映射 
		<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>-->
	</session-factory>
</hibernate-configuration>

2.Animal

package cn.itcast.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;
	}
	
	

}

3.Cat

package cn.itcast.e_extends2;

public class Cat extends Animal{
	//抓老鼠
	private String catchMouse;

	public String getCatchMouse() {
		return catchMouse;
	}

	public void setCatchMouse(String catchMouse) {
		this.catchMouse = catchMouse;
	}

	
	
}

4.Monkey

package cn.itcast.e_extends2;

public class Monkey extends Animal {
	//吃香蕉
	private String eatBanana;

	public String getEatBanana() {
		return eatBanana;
	}

	public void setEatBanana(String eatBanana) {
		this.eatBanana = eatBanana;
	}
	
	

}

5.Animal.hbm.xml

<?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">


<!--映射文件:映射一个实体类对象,描述一个对象最终可以直接保存对象数据到数据库中  -->
<!-- package:要映射的对象所在的包(可选,如果不指定,此文件下所有的类都要指定全路径) 
    auto-import 默认为true,在写HQL的时候自动导入包名
            如果指定为false,在写HQL的时候必须要写上类的全名-->
<!-- 
	继承映射, 所有的子类都映射到一张表
 -->
<hibernate-mapping package="cn.itcast.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>


6.Junit测试

package cn.itcast.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 save(){
		Session session=sf.openSession();
		session.beginTransaction();
		//保存数据
     	Cat cat=new Cat();
	    cat.setName("大花猫");
		cat.setCatchMouse("抓小老鼠");	
		
		Monkey m=new Monkey();
		m.setEatBanana("吃10个香蕉");
	
		session.save(cat);
		session.save(m);
		
		session.getTransaction().commit();
		session.close();
	}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值