組件(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"
?"
<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將假定整個組件為空。 在大多數情況下,這樣假定應該是沒有問題的。