组件(Component)是一个被包含的对象,在持久化的过程中,它被当作值类型,而并非一个实体的引用。在这篇文档中,组件这一术语指的是面向对象的合成概念.
·user.java

User.java

package com.jason.component;


public class User{
 private Integer id;
 private String name;
 private String password;
 private Contact contact;


 /**
  * @return Returns the id.
  */
 public Integer getId() {
  return id;
 }
 /**
  * @param id The id to set.
  */
 public void setId(Integer id) {
  this.id = id;
 }
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return Returns the password.
  */
 public String getPassword() {
  return password;
 }
 /**
  * @param password The password to set.
  */
 public void setPassword(String password) {
  this.password = password;
 }
 /**
  * @return Returns the contact.
  */
 public Contact getContact() {
  return contact;
 }
 /**
  * @param contact The contact to set.
  */
 public void setContact(Contact contact) {
  this.contact = contact;
 }
}



· contact.java

Contact.java

package com.jason.component;

public class Contact {
 private String email;
 private String address;

 /**
  * @return Returns the address.
  */
 public String getAddress() {
  return address;
 }
 /**
  * @param address The address to set.
  */
 public void setAddress(String address) {
  this.address = address;
 }
 /**
  * @return Returns the email.
  */
 public String getEmail() {
  return email;
 }
 /**
  * @param email The email to set.
  */
 public void setEmail(String email) {
  this.email = email;
 }
}

 
· User.hbm.xml

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.jason.component">
 <class
  name="User"
  table="user"
 >
  <meta attribute="sync-DAO">false</meta>
  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>
  <property
   name="Name"
   column="name"
   type="string"
   not-null="true"
   length="20"
  />
  <property
   name="Password"
   column="password"
   type="string"
   not-null="true"
   length="20"
  />
  <component class="Contact" name="Contact">
   <property
    column="email"
    length="50"
    name="Email"
    not-null="false"
    type="string"
    />
   <property
    column="address"
    length="100"
    name="Address"
    not-null="false"
    type="string"
    />
  </component>
 </class> 
</hibernate-mapping>


testComponent.java

package com.jason.component;

import java.util.List;
import java.util.ListIterator;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class testComponent {
    public static void main(String[] args) throws HibernateException {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       
        testInsert(sessionFactory);
        testQuery(sessionFactory);
                
        sessionFactory.close();
       
    }
    public static void testInsert( SessionFactory sessionFactory )throws HibernateException {
    
      Session session = sessionFactory.openSession();
        Transaction tx= session.beginTransaction();
        User user = new User();
        Contact contact=new Contact();
        contact.setEmail("gm_jing@yahoo.com.cn");
        contact.setAddress("beijing");
       
        user.setName("gm_jing");
        user.setPassword("1111111");
        user.setContact(contact);
       
        session.save(user);
        tx.commit();
        session.close();
        System.out.println("OK!");
   }
   
    public static void testQuery( SessionFactory sessionFactory )throws HibernateException {
    
     Session session = sessionFactory.openSession();
        Transaction tx= session.beginTransaction();
        User user = new User();
                 
        Query query=session.createQuery("from User as user");
        //query.setCharacter(1, 'M');
        List names =query.list();
        for(ListIterator it=names.listIterator();it.hasNext();){
           user= (User)it.next();
           System.out.println("Id: " + user.getId());
            System.out.println("name: " + user.getName());
            System.out.println("password: " + user.getPassword());
            if(user.getContact()!=null){
            
             if(user.getContact().getEmail()!=null){
              System.out.println("Email: " + user.getContact().getEmail());
             }
             if(user.getContact().getAddress()!=null){
              System.out.println("Address: " + user.getContact().getAddress());
               
             }
            }
   
        }
     
        tx.commit();
        session.close();
    
    }
}

 



就像所有的值类型一样, 组件不支持共享引用。 换句话说,两个Contact可能相同,但是两个User对象应该包含两个独立的Contact对象,只不过这两个Contact对象具有“同样”的值。 组件的值可以为空,其定义如下。 每当Hibernate重新加载一个包含组件的对象,如果该组件的所有字段为空,Hibernate将假定整个组件为空。 在大多数情况下,这样假定应该是没有问题的。