Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

ibernate 组件映射--->张国亮总结第三季

$
0
0
1.  组件映射概述

    组件类是值类型的,它没有对象标识符属性,在数据库中没有对应的表,它只隶属于另一个持久化类的实例。

  把组件类作为持久化类的单个属性来使用

  把组件类作为持久化类的集合属性来使用

2.组件映射(User-Name)

关联的属性是个复杂类型的持久化类,但不是实体即:数据库中没有表与该属性对应,但该类的属性要永久保存的。

<component name=”name” class=”com.test.hibernate.domain.Name”>

      <property name=”initial”/>

      <property name=”first”/>

      <property name=”last”/>

</component>

当组件的属性不能和表中的字段简单对应的时候可以选择实现:

3. 案例分析

把组件类作为持久化类的单个属性来使用

Customers.hbm.xml

<?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.hbsi.component">

 

    <class name="Customers" table="customers">

        <id name="id">

           <generator class="native"/>

        </id>

        <property name="name" column="name"/>

        

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

         <property name="province" column="home_pro"/>

         <property name="city" column="home_city"/>

         <property name="zipcode" column="home_zip"/>

         <property name="telephone" column="home_tele"/>

       </component>

       

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

         <property name="province" column="deliver_pro"/>

         <property name="city" column="deliver_city"/>

         <property name="zipcode" column="deliver_zip"/>

         <property name="telephone" column="deliver_tele"/>

       </component>

    </class>

    

</hibernate-mapping>

测试类:TestCustomers.Java

package com.hbsi.component;

 

import org.hibernate.Session;

import org.junit.Test;

 

import com.hbsi.utils.HibernateSessionFactory;

 

public class TestCustomers {

   

   @Test

   public void add(){

      Customers c = new Customers();

      c.setName("张三");

      

      Address homeAddress = new Address();

      homeAddress.setProvince("河北省");

      homeAddress.setCity("保定市");

      homeAddress.setZipcode("071000");

      homeAddress.setTelephone("1235345");

      

      c.setHomeAddress(homeAddress);

      

      Address deliverAddress = new Address();

      deliverAddress.setProvince("四川省");

      deliverAddress.setCity("成都市");

      deliverAddress.setZipcode("032700");

      deliverAddress.setTelephone("1454655645");

      

      c.setDeliverAddress(deliverAddress);

      

      Session session = HibernateSessionFactory.getSession();

      session.beginTransaction();

      session.save(c);

      session.getTransaction().commit();

      HibernateSessionFactory.closeSession();

   }

}

把组件类作为持久化类的集合属性来使用

Album.hbm.xml

<?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.hbsi.component">

 

    <class name="Album" table="ablum">

        <id name="id">

           <generator class="native"/>

        </id>

        <property name="title" column="title"/>

        

        <bag name="photos" table="photos">

           <key column="ablum_id"/>

           <composite-element class="Photos">

              <property name="name"/>

              <property name="filePath" column="file_path"/>

              <property name="describtion"/>

           </composite-element>  

        </bag>

       

    </class>

    

</hibernate-mapping>

  测试类:AblumTest.Java

package com.hbsi.component;

 

import java.util.ArrayList;

import java.util.List;

 

import org.hibernate.Session;

import org.junit.Test;

 

import com.hbsi.utils.HibernateSessionFactory;

 

public class TestAlbum {

   

   @Test

   public void add(){

      

      Album album = new Album();

      album.setTitle("大学时光");

      

      List<Photos> photos = new ArrayList<Photos>();

      

      Photos p1 = new Photos(); 

      p1.setName("p1");

      p1.setFilePath("d:\\picture");

      p1.setDescribtion("那一年");

      

      Photos p2 = new Photos(); 

      p2.setName("p2");

      p2.setFilePath("d:\\picture");

      p2.setDescribtion("这些年");

      

      photos.add(p1);

      photos.add(p2);

      album.setPhotos(photos);

      

      Session session = HibernateSessionFactory.getSession();

      session.beginTransaction();

      session.save(album);

      session.getTransaction().commit();

      HibernateSessionFactory.closeSession();

   }

}

作者:zhangguoliang521 发表于2012-12-25 20:11:02 原文链接
阅读:31 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>