在MySQL中新建数据表User:

  1. CREATE TABLE user (
  2.     id INT(11) NOT NULL auto_increment PRIMARY KEY,
  3.     name VARCHAR(100) NOT NULL default '',
  4.     age INT,
  5.     email VARCHAR(100) NOT NULL
  6. );

按照最基本的映射策略的话,应该把User类设计成:

  1. package onlyfun.caterpillar; 

  2. public class User { 
  3.     private Integer id; 
  4.     private String name; 
  5.     private int age; 
  6.     private String email; 

  7.     ........ 
  8. }

然而由于业务上的需要,要把email字段升格成MailAddress类,例如:

  1. package com.hb3.pack_05.model;

  2. public class MailAddress {

  3.     private String email;

  4.     public MailAddress() {       
  5.     }
  6.     
  7.     public String getEmail() {
  8.         return email;
  9.     }

  10.     public void setEmail(String email) {
  11.         this.email = email;
  12.     }
  13.     
  14.     public void sendMail() {
  15.         System.out.println("Send mail to " + email);
  16.     }
  17. }

User.java

  1. package com.hb3.pack_05.model;

  2. public class User {

  3.     private Integer id;
  4.     private String name;
  5.     private Integer age;
  6.     private MailAddress mailAddress;

  7.     public User() {
  8.     }

  9.     public Integer getId() {
  10.         return id;
  11.     }

  12.     public void setId(Integer id) {
  13.         this.id = id;
  14.     }

  15.     public String getName() {
  16.         return name;
  17.     }

  18.     public void setName(String name) {
  19.         this.name = name;
  20.     }
  21.     
  22.     public Integer getAge() {
  23.         return age;
  24.     }

  25.     public void setAge(Integer age) {
  26.         this.age = age;
  27.     }

  28.     public MailAddress getMailAddress() {
  29.         return mailAddress;
  30.     }

  31.     public void setMailAddress(MailAddress mailAddress) {
  32.         this.mailAddress = mailAddress;
  33.     }
  34. }

User.hbm.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  5. <hibernate-mapping>

  6.     <class name="com.hb3.pack_05.model.User" table="user">
  7.     
  8.         <id name="id" column="id" type="java.lang.Integer"> 
  9.             <generator class="native"/> 
  10.         </id>
  11.         
  12.         <property name="name" column="name" type="java.lang.String"/>
  13.         
  14.         <property name="age" column="age" type="java.lang.Integer"/>
  15.         
  16.         <component name="mailAddress" class="com.hb3.pack_05.model.MailAddress"> 
  17.             <property name="email" 
  18.                       column="email" 
  19.                       type="java.lang.String" 
  20.                       not-null="true"/> 
  21.         </component> 
  22.     </class> 
  23. </hibernate-mapping>

修改hibernate.cfg.xml文件:

......

<mapping resource="com/hb3/pack_05/model/User.hbm.xml" />

......

 

测试代码如下:

  1. package com.hb3.pack_05;

  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;

  6. import com.hb3.pack_05.model.MailAddress;
  7. import com.hb3.pack_05.model.User;

  8. public class BusinessService {

  9.     public static void main(String[] args) {
  10.         
  11.         Configuration config = new Configuration().configure();
  12.         SessionFactory sessionFactory = config.buildSessionFactory();      
  13.         Session session = sessionFactory.openSession();

  14.         MailAddress mailAddress = new MailAddress();
  15.         mailAddress.setEmail("bluelazysb@hotmail.com");
  16.                 
  17.         User user = new User();
  18.         user.setName("shenbin");
  19.         user.setAge(new Integer(28));
  20.         user.setMailAddress(mailAddress);

  21.         Transaction tx = session.beginTransaction();            
  22.         session.save(user);
  23.         session.flush();
  24.         tx.commit();

  25.         session.close();
  26.         
  27.         session = sessionFactory.openSession();
  28.         
  29.         user = (User) session.load(User.classnew Integer(1));
  30.         System.out.println(user.getAge()  + "\t" +
  31.                            user.getName() + "\t" +
  32.                            user.getMailAddress().getEmail());
  33.         user.getMailAddress().sendMail();
  34.         
  35.         
  36.         session.close();
  37.         sessionFactory.close();
  38.     }
  39. }


ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程