Hibernateday05继承关系union-subclass映射策略

 union-subclass映射策略
     特点:父表的数据保存在父表当中
           子表的数据保存在子表当中=父表的数据+子表的数据
       如果保存Product,数据保存在t_product表当中
       如果保存Book,数据保存在t_book表中,不会保存在t_product表中
      
  R.   create table g_product(
      p_id integer primary key,
      p_name varchar2(30),
      p_price number(4,2)
     )
--子表--
     create table g_book(
      p_id integer ,
      p_name varchar2(30),
      p_price number(4,2),
      c_author varchar2(40)
   );

  M:映射文件

1.在com.jsu.hb.pojo包中提供实体类Product.java和Book.java

package com.jsu.hb.pojo;

public class Product {
	private Integer id;
	private String name;
	private Double price;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	
}

    Book.java

package com.jsu.hb.pojo;

public class Book extends Product {
	private String author;

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
	
}
 

2..建表

create table g_product(
      p_id integer primary key,
      p_name varchar2(30),
      p_price number(4,2)
     )
--子表--
     create table g_book(
      p_id integer ,
      p_name varchar2(30),
      p_price number(4,2),
      c_author varchar2(40)
   );

 3.M. 提供所需要的映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jsu.hb.pojo">
	<!-- name:所操作对象的全路径 table:该对象所对应的表名 -->
	<class name="Product" table="g_product">
		<id name="id" column="p_id">
			<generator class="increment"></generator>
		</id>
		<!-- 普通属性的配置,非主键属性的配置 -->
		<property name="name" column="p_name"></property>
		<property name="price" column="p_price"></property>
		<!-- 采用union-subclass处理book子类 -->
            <union-subclass name="Book" table="g_book">
            <!---g_book的其他属性由Product提供,无须配置附表的字段,not-null="true" 不能为空-->
            <property name="author" column="c_author" not-null="true">
            </property>
         </union-subclass>
	</class>
</hibernate-mapping>

 4.在hibernate.cfg.xml文件中进行注册

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

<hibernate-configuration>
	<session-factory> 
		<!-- show_sql:是否显示hibernate执行的SQL语句,默认为false -->
		<property name="show_sql">true</property>
		<!-- show_sql:是否显示hibernate执行格式化输出的SQL语句,默认为false -->
		<property name="format_sql">true</property>
		<!-- 配置与数据库连接的参数 -->
		<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:oracle</property>
		<property name="connection.username">scott</property>
		<property name="connection.password">tiger</property>
		<!-- 2.自身属性相关的配置
			dialect:方言
			hibernate根据dialect的配置进行特定数据性能方面的调优
		 -->
		<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
		<mapping resource="com/jsu/hb/pojo/union.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>

 5.提供工具类HibernateUtil.java

package com.jsu.hb.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	private static ThreadLocal<Session> tl= new ThreadLocal<Session>();
	static{
		try{
				Configuration cfg = new Configuration();
				cfg.configure();
				sf=cfg.buildSessionFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static Session openSession(){
		return sf.openSession();
	}
	public static Session getCurrentSession(){
		Session session = tl.get();//先从储存的线程中查找
		if(session==null){
			session=openSession();
			tl.set(session);
			return session;
		}
		return session;
	}
}

 6.在测试类中

public class TestExtends {
	@Test
	public void testSubclass() {
		Product p = new Product();
		p.setName("汽车");
		p.setPrice(31.0);

		Book b = new Book();
		b.setName("月子2");
		b.setPrice(99.0);
		b.setAuthor("白云");

		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = session.getTransaction();
		tx.begin();
		 session.save(p);
		session.save(b);
		tx.commit();

	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值