Hibernate知识辅导(3--3)

例:

 

Computer类,是抽象类,是父类

public abstract class Computer{

    private int id;

    private int price;

    private String manufacturer;

   

public int getId(){

        return id;

}

 

    public void setId(int id){

        this.id = id;

}              

 

    public String getManufacturer(){

        return manufacturer;

    }

 

    public void setManufacturer(String manufacturer){

        this.manufacturer = manufacturer;

    }

 

    public int getPrice(){

        return price;

    }

 

    public void setPrice(int price){

        this.price = price;

    }

}

 

子类台式机 Desktop

public class Desktop extends Computer{

    private boolean isLCD;

    public boolean isLCD(){

        return isLCD;

    }

    public void setLCD(boolean isLCD){

        this.isLCD = isLCD;

    }

}

 

子类笔记本电脑 Notepad

 

 public class Notepad extends Computer{

    private float weight;

    private float thickness;

    public float getThickness(){

        return thickness;

    }

 

    public void setThickness(float thickness){

        this.thickness = thickness;

    }

 

    public float getWeight(){

        return weight;

    }

 

    public void setWeight(float weight){

        this.weight = weight;

    }

}

 

所有类建一个表的映射文件

<hibernate-mapping package="alan.hbn.inheritance.entity">

  <class name="Computer" table="computer_tph" discriminator-value="c">

    <id name="id" column="id" type="integer">

      <generator class="native" />

    </id>

<discriminator column="category" type="character" />

<!--用来表识父子类的标记字段-->

    <property name="price" column="price" type="integer" not-null="true"/>

    <property name="manufacturer" column="manufacturer" type="string" length="30" not-null="true"/>

<!--subclass是定义子类中特有部分的标签-->

<subclass name="Desktop" discriminator-value="d">

<!--discriminator-value属性是指定本类的标识符的-->

      <property name="LCD" column="islcd" type="yes_no" />

      <!--

指定子类的特有属性 type=”yes_no”是把布尔值转成单字节的存储数据库的类型,如果为true就会在字段中写入“Y”,反之为“N

-->

    </subclass>

    <subclass name="Notepad" discriminator-value="n">

      <property name="weight" column="weight" type="float" />

      <property name="thickness" column="thickness" type="float" />

    </subclass>

  </class>

</hibernate-mapping>

 

只为具体类建表的配置文件,还可以将这两个类的配置文件分开来写,这样就可单表的映射文件没有区别了。

<hibernate-mapping package="alan.hbn.inheritance.entity">

  <class name="Computer" abstract="true">

    <id name="id" column="id" type="integer">

      <generator class="increment" />

    </id>

    <property name="price" column="price" type="integer" not-null="true"/>

<property name="manufacturer" column="manufacturer" type="string" length="30" not-null="true"/>

<!--union-subcalss标签是指定子类对应的表和子类特有的属性-->

    <union-subclass name="Desktop" table="desktop">

      <property name="LCD" column="islcd" type="yes_no" />

    </union-subclass>

    <union-subclass name="Notepad" table="notepad">

      <property name="weight" column="weight" type="float" />

      <property name="thickness" column="thickness" type="float" />

    </union-subclass>

  </class>

</hibernate-mapping>

 

每个类建一格表的配置文件

<hibernate-mapping package="alan.hbn.inheritance.entity">

  <class name="Computer" table="computer_tpc">

    <id name="id" column="id" type="integer">

      <generator class="native" />

    </id>

    <property name="price" column="price" type="integer" not-null="true"/>

<property name="manufacturer" column="manufacturer" type="string" length="30" not-null="true"/>

<!--union-subcalss标签是指定子类对应的表和子类特有的属性-->

    <joined-subclass name="Desktop" table="desktop_tpc">

      <key column="computerid" />

       <!--指定使用父类的Id生成策略-->

      <property name="LCD" column="islcd" type="yes_no" />

</joined-subclass>

 

    <joined-subclass name="Notepad" table="notepad_tpc">

      <key column="computerid" />

      <property name="weight" column="weight" type="float" />

      <property name="thickness" column="thickness" type="float" />

    </joined-subclass>

  </class>

</hibernate-mapping>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值