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

    本例模仿猫和动物的关系,实现用猫继承动物,属于简单继承。

     简单继承的特点:有多少个子类,就有多少个映射文件!

     下面是实现代码

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">update</property>
		<!-- 3.加载所有映射 
		<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>-->
	</session-factory>
</hibernate-configuration>

2.Animal

package cn.itcast.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;
	}
	@Override
	public String toString() {
		return "Animal [id=" + id + ", name=" + name + "]";
	}
	
	

}

3.Cat

package cn.itcast.e_extends1;

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 [catchMouse=" + catchMouse + "]";
	}
	
}

4.Cat.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_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>

5.Junit测试

package cn.itcast.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 save(){
		Session session=sf.openSession();
		session.beginTransaction();
		//保存数据
	/*	Cat cat=new Cat();
	    cat.setName("大花猫");
		cat.setCatchMouse("抓小老鼠");	
		session.save(cat);*/
		//获取数据,当写HQL查询的时候,通过父类查询必须写上类的全名
		Query q=session.createQuery("from cn.itcast.e_extends1.Animal");
		List<Animal> list=q.list();
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		
	
		session.getTransaction().commit();
		session.close();
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值