论坛首页 入门技术论坛

Hibernate知识辅导(4--3)

浏览 1315 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-03  

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

<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>

 

hibernate组件映射

 

组件不会生成唯一标识,但是也需要对应实体类。

hibernate中可以把一个类的对象当作一个属性组件来使用,并且在使用时会自动创建,所以同一组件对象是不会被两次引用的。

 

:

<hibernate-mapping package="alan.hbn.rel.inherit" auto-import="false">

     <class name="Guest" table="guest">

         <id name="id" column="id" type="long" unsaved-value="0">

              <generator class="native"/>

         </id>

         <property name="userName" column="userName" type="string"/>

         <property name="password" column="pwd" type="string"/>

        <property name="birthday" column="birthday" type="date"/>

         <component name="homeAddress" class="Address">       

             <property name="street" column="street" type="string"/>                         <property name="zipcode"  column="zipcode"  type="string"/>                </component>   

     </class

</hibernate-mapping>

 

hibernateHQLhibernate Query Language

 

HQLhibernate的查询语言,他可以支持面向对象的查询。使用HQL语句,只能通过session.createQuery("...")

 

 

使用hibernate分页显示,使用Query对象的setFirstResult(int firstResult)firstResult从零开始)方法和setMaxResults(int maxResults) 方法。他会根据不同的底层数据库来显示指定数量的记录。

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics