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();
}
}
本文介绍了一种使用 Hibernate 的 Union-Subclass 映射策略来处理继承关系的方法。通过这种方式,父类数据存储在父表中,而子类数据则额外存储在子表中。文中详细解释了如何在 Java 中实现这种策略,并提供了具体的代码实例。
4495

被折叠的 条评论
为什么被折叠?



