博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernateday05继承关系joined-subclass映射策略
阅读量:4248 次
发布时间:2019-05-26

本文共 3146 字,大约阅读时间需要 10 分钟。

2.joined-subclass映射策略

    特点:父表的数据由父表保存,子表的数据由父表和子表共同保存。子类和父类共有的
          属性保存在父表当中,子类扩展的属性保存在子表当中。采取该策略不需要辨别列。要为子表提供一个列【主键】映射父表的主键
   表:
   create table g_product( --父表--
      p_id integer primary key,
      p_name varchar2(30),
      p_price number(4,2)
   )
   create table g_book( --子表--
     c_id integer primary key references g_product(p_id),--与父表关联--
     c_author varchar2(40)
   )
   映射文件
     <joined-subclass name="Book" table="g_book" >
    <key column="c_id"></key><!-- 外键声明 -->
    <property name="author" column="c_author"></property>
     </joined-subclass>

1.O提供对象属性实体类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.R建表

create table g_product(	p_id integer primary key,	p_name varchar2(30),	p_price number(6,2))create table g_book(	c_id integer primary key references g_product(p_id),	c_author varchar2(40))

 3.提供映射文件joined.hbm.xml

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

true
true
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:oracle
scott
tiger
org.hibernate.dialect.Oracle9iDialect

 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
tl= new ThreadLocal
(); 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.提供测试类TestJoined.java

package com.jsu.hb.test;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.jsu.hb.pojo.Book;import com.jsu.hb.pojo.Product;import com.jsu.hb.util.HibernateUtil;public class TestJoined {	@Test	public void save(){		Product p = new Product();		p.setName("小米");		p.setPrice(1999.0);				Book b = new Book();		b.setName("围城");		b.setPrice(38.5);		b.setAuthor("沈从文");				Session session = HibernateUtil.getCurrentSession();		Transaction tx = session.getTransaction();		tx.begin();		session.save(p);//保存产品		//session.save(b);//保存图书		tx.commit();	}}
 

转载地址:http://ephhi.baihongyu.com/

你可能感兴趣的文章
几个常见的关于日期的SQL
查看>>
常见约束、事务及其他查询语句
查看>>
关于jdbc
查看>>
利用jdbc做的一个简单系统(接上一篇)
查看>>
对TextField 和JTextField 等文本编辑区的监听
查看>>
详解个推java服务端集成(干货)
查看>>
常见聚合函数
查看>>
简单子查询
查看>>
联表查询
查看>>
关于WindowListener的使用
查看>>
关于KeyListener的简单使用
查看>>
关于鼠标移动监听接口:MouseMotionListener
查看>>
TCP/IP详解笔记(一)
查看>>
501. Find Mode in Binary Search Tree
查看>>
504. Base 7
查看>>
593. Valid Square
查看>>
494. Target Sum
查看>>
463. Island Perimeter
查看>>
TCP协议粗析
查看>>
653. Two Sum IV - Input is a BST
查看>>