Hibernate3 零碎笔记(二)

employee:
两个子类:Skiller Sales
一张表:增加一个type字段
type skill sell


skill,sell字段得允许为空
鉴别器:
<hibernate-mapping package="com.ethan.domain">
<class name="Employee" discriminator-value="0">
<id name="id">
<generator class="native"/>
</id>

<discriminator column="tyep" type="java.lang.Integer"></discriminator>
<!-- name unique -->
<property name="name"/>

<!-- departId外键参考department表 -->
<many-to-one name="depart" column="departId"/>

<subclass name="Skiller" discriminator-value="1">
<property name="skill"></property>
</subclass>

<subclass name="Sales" discriminator-value="2">
<property name="sell"></property>
</subclass>
</class>
</hibernate-mapping>

查询语句:只牵扯到一张表
Hibernate: 
    select
        employee0_.id as id0_0_,
        employee0_.name as name0_0_,
        employee0_.departId as departId0_0_,
        employee0_.skill as skill0_0_,
        employee0_.sell as sell0_0_,
        employee0_.tyep as tyep0_0_ 
    from
        Employee employee0_ 
    where
        employee0_.id=?
class com.ethan.domain.Skiller


Employee emp = (Employee) s.get(Skiller.class, empId);
from
        Employee skiller0_ 
    where
        skiller0_.id=? 
        and skiller0_.tyep=1


如果增加子类,就得改变表结构,有些字段不能为非空


2.把公共字段放在一张表,特有字段放在各自的表中,加外键
每个子类一张表,子类属性差异比较大,不太注重效率
joined-subclass:
<joined-subclass name="Skiller" table="skiller">
<key column="emp_id"/><!-- 引用的是id -->
<property name="skill"></property>
</joined-subclass>


<joined-subclass name="Sales" table="sales">
<key column="emp_id"/><!-- 引用的是id -->
<property name="sell"></property>
</joined-subclass>
employee  pk id sales   pk,fk employee_id
sell
 name
 depart_id


查询会用到多张表
id,没有逻辑意义,不用担心修改问题


下边会查三张表,效率上不高
Employee emp = (Employee) s.get(Employee.class, empId);


from
        Employee employee0_ 
    left outer join
        skiller employee0_1_ 
            on employee0_.id=employee0_1_.emp_id 
    left outer join
        sales employee0_2_ 
            on employee0_.id=employee0_2_.emp_id 
    where
        employee0_.id=?
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值