`

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

阅读更多

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

<?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>
		<!-- 采用joined-subclass处理book子类 -->
		<joined-subclass name="Book" table="g_book">
			<key column="c_id"></key>
			<property name="author" column="c_author"></property>
		</joined-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/joined.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.提供测试类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();
	}
}
 
分享到:
评论

相关推荐

    Ansible-netbox-joined-inventory.zip

    Ansible-netbox-joined-inventory.zip,netbox_joined_inventory是一个python脚本,它从netbox真实源收集数据,并将其存储为ansible inventory、group_vars和host_vars files.netbox_joined_inventory(用于网络设备...

    jq-joined-text-entries:jQuery 插件虚拟连接 input-s 和 textarea-s 以进行导航

    #jq-joined-text-entries 在垂直导航中虚拟连接 input-s 和 textarea-s 的 jQuery 插件查看或检查test/simple.html 、 test/textarea.html 、 test/two-sets.html 用法: $ ( 'form' ) . find ( '.text-entry' ) . ...

    Hibernate3.1_学习源码

    配置文件:只配置父类的映射文件,在其中加入joined-subclass将两个子类实体映射关系添加 2) 数据库表:一张表,包括公共字段、特有字段、区分字段 实体层设计:与第一种方法设计一样,设计三个实体类,分父类和...

    NHibernate中文帮助手册API

    连接的子类(joined-subclass)  5.1.15. 联合子类(union-subclass)  5.1.16. 连接  5.1.17. map, set, list, bag  5.1.18. 引用(import)  5.2. NHibernate 的类型  5.2.1. 实体(Entities)和值(values) ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any ...

    HibernateAPI中文版.chm

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any ...

    hibernate3.2中文文档(chm格式)

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any ...

    Hibernate3的帮助文档

    连接的子类(joined-subclass) 6.1.16. 联合子类(union-subclass) 6.1.17. 连接(join) 6.1.18. 键(key) 6.1.19. 字段和规则元素(column and formula elements) 6.1.20. 引用(import) 6.1.21. any 6.2. ...

    netbox-joined-inventory:Netbox_joined_inventory是一个Python脚本,它从Netbox的真实来源收集数据并将其存储为Ansible清单,group_vars和host_vars文件

    Netbox_joined_inventory(用于网络设备) Netbox_joined_inventory加入作为网络配置的事实来源,并加入作为自动化协调器。 Netbox_joined_inventory是一个脚本,该脚本从Netbox收集数据作为事实来源,并将数据存储...

    NHibernate中文帮组文档(2008.11月更新)

    5.1.14. 连接的子类(joined-subclass) 5.1.15. 联合子类(union-subclass) 5.1.16. 连接 5.1.17. map, set, list, bag 5.1.18. 引用(import) 5.2. NHibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本...

    Hibernate+中文文档

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any ...

    Hibernate中文详细学习文档

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any ...

    hibernate 框架详解

    连接的子类(joined-subclass) 6.1.16. 联合子类(union-subclass) 6.1.17. 连接(join) 6.1.18. 键(key) 6.1.19. 字段和规则元素(column and formula elements) 6.1.20. 引用(import) 6.1.21. any 6.2. ...

    Hibernate参考文档

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. ...

    hibernate3.04中文文档.chm

    连接的子类(joined-subclass) 6.1.16. 联合子类(union-subclass) 6.1.17. 连接(join) 6.1.18. 键(key) 6.1.19. 字段和规则元素(column and formula elements) 6.1.20. 引用(import) 6.1.21. any 6.2. ...

    hibernate 体系结构与配置 参考文档(html)

    连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. ...

    Hibernate教程

    连接的子类(joined-subclass) 6.1.16. 联合子类(union-subclass) 6.1.17. 连接(join) 6.1.18. 键(key) 6.1.19. 字段和规则元素(column and formula elements) 6.1.20. 引用(import) 6.1.21. any 6.2. ...

    NHibernate参考文档 2.0.0 chm

    5.1.14. 连接的子类(joined-subclass) 5.1.15. 联合子类(union-subclass) 5.1.16. 连接 5.1.17. map, set, list, bag 5.1.18. 引用(import) 5.2. NHibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本...

    Hibernate 中文 html 帮助文档

    5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. ...

    最全Hibernate 参考文档

    5.1.15. 连接的子类(joined-subclass) 5.1.16. 联合子类(union-subclass) 5.1.17. 连接(join) 5.1.18. 键(key) 5.1.19. 字段和规则元素(column and formula elements) 5.1.20. 引用(import) 5.1.21. any 5.2. ...

Global site tag (gtag.js) - Google Analytics