本系列博客汇总在这里:Hibernate 汇总
一、单表继承映射源码工程文件为:hibernate4.3_11
1、配置
<?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:指定当前映射文件的实体类model所在的包
-->
<hibernate-mapping package="com.wyx.hiber.model">
<class name="Animal" table="t_animal">
<!-- id
是主键映射配置
-->
<id name="anId" column="an_id">
<!--
generator:主键的映射策略
-->
<generator class="native"></generator>
</id>
<!-- 鉴别器,在单表中加入一列来区分子类的 -->
<discriminator column="type" type="string"></discriminator>
<property name="anName" column="an_name"></property>
<property name="gender"></property>
<!-- 子类配置
name:子类的类名
discriminator-value:区分子类的值
-->
<subclass name="Pig" discriminator-value="p">
<!-- 子类中的属性映射 -->
<property name="weight"></property>
</subclass>
<subclass name="Bird" discriminator-value="b">
<property name="height"></property>
</subclass>
</class>
</hibernate-mapping>
2、保存
public void save()
{
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
try
{
Pig pig = new Pig();
pig.setAnName("天蓬元帅");
pig.setGender(1);
pig.setWeight(200);
session.save(pig);
Bird b = new Bird();
b.setAnName("大鹏");
b.setGender(1);
b.setHeight(10000);
session.save(b);
tx.commit();
} catch (Exception e)
{
e.printStackTrace();
tx.rollback();
} finally
{
HibernateUtils.closeResource(session);
}
}
3、子类查询
public void query()
{
Session session = HibernateUtils.getSession();
try
{
Pig pig = (Pig) session.load(Pig.class, 1);
System.out.println(pig.getAnName() + " " + pig.getWeight());
} catch (Exception e)
{
e.printStackTrace();
} finally
{
HibernateUtils.closeResource(session);
}
}
4、父类查询
/**
* load的延迟加载的方式返回的是代理类,所以无法做多态查询
*/
@Test
public void query1()
{
Session session = HibernateUtils.getSession();
try
{
Animal animal = (Animal) session.load(Animal.class, 1);
if (animal instanceof Pig)
{
System.out.println("是猪");
} else
{
System.out.println("不是猪");
}
} catch (Exception e)
{
e.printStackTrace();
} finally
{
HibernateUtils.closeResource(session);
}
}
/**
* get支持多态查询
*/
@Test
public void query2()
{
Session session = HibernateUtils.getSession();
try
{
Animal animal = (Animal) session.get(Animal.class, 1);
if (animal instanceof Pig)
{
System.out.println("是猪");
Pig pig = (Pig) animal;
System.out.println(pig.getAnName() + " " + pig.getWeight());
} else
{
System.out.println("不是猪");
}
} catch (Exception e)
{
e.printStackTrace();
} finally
{
HibernateUtils.closeResource(session);
}
}
二、父子表继承映射
配置
<?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:指定当前映射文件的实体类model所在的包
-->
<hibernate-mapping package="com.wyx.hiber.model">
<class name="Animal" table="t_animal" >
<!-- id
是主键映射配置
-->
<id name="anId" column="an_id">
<!--
generator:主键的映射策略
-->
<generator class="native"></generator>
</id>
<property name="anName" column="an_name"></property>
<property name="gender"></property>
<!--
子类映射配置
name:子类的名字
table:子类映射表名
-->
<joined-subclass name="Pig" table="t_pig">
<!--
key:字表的主键设置
column:主键名称
-->
<key column="pid"></key>
<!-- 子类的属性映射 -->
<property name="weight"></property>
</joined-subclass>
<joined-subclass name="Bird" table="t_bird">
<!--
key:字表的主键设置
column:主键名称
-->
<key column="bid"></key>
<!-- 子类的属性映射 -->
<property name="height"></property>
</joined-subclass>
</class>
</hibernate-mapping>
父子表继承映射,父类产生父类的表,子类产生子类的表。
父子表的映射因为生成的表多张,查询的时候我们需要多表连接查询,所以效率没有单表继承映射高。
配置
<?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:指定当前映射文件的实体类model所在的包
-->
<hibernate-mapping package="com.wyx.hiber.model">
<!-- 字表映射,需要把父类的映射设置成抽象的(不会产生父表)
abstract="true"
-->
<class name="Animal" table="t_animal" abstract="true">
<!-- id
是主键映射配置
-->
<id name="anId" column="an_id">
<!--
generator:主键的映射策略
-->
<generator class="uuid"></generator>
</id>
<property name="anName" column="an_name"></property>
<property name="gender"></property>
<!-- 子表映射 -->
<union-subclass name="Pig" table="t_pig">
<property name="weight"></property>
</union-subclass>
<union-subclass name="Bird" table="t_bird">
<property name="height"></property>
</union-subclass>
</class>
</hibernate-mapping>
导出表后,产生两张子表,字表中包含父类的映射字段,存取方式一样不再复述。
如有错误,欢迎指正!