Hibernate的Component的用法

 

    如果一张表的列太多了,那我们应该怎么做呢?可能有些人的做法就是:将一张一有拆分成两张表,那除了将表进行拆分,我们还能怎么样呢?在Hibernate对这种表的列太多,进行拆分,有一种非常好的解决方案,那就是基于Component技术,使用Component技术,我们可以将一种表,按照字段的类型进行分类,同一类的,可以使用一个对象进行封装,而这个封装的对象,就是一个Component。假如,一个用户他的信息可能包含以下几种分类,住址(包括家庭地址、办公地址、家庭邮编、办公邮编等)、电话(家庭电话、办公电话、手机、小灵通)、邮件地址(办公邮箱、私人邮箱等)、通信工具(QQ号码、MSN、雅虎帐号、网易泡泡等),对于这些属性类别,我们都可以使用对象进行封装。

    假如现在有以下这样的数据库表:


建表语句:

 程序代码
drop table if exists T_USER ;

/*==============================================================*/
/* Table: T_USER                                                */
/*==============================================================*/
create table T_USER
(
   ID                   INT not null auto_increment ,
   USER_NAME            VARCHAR (20 ) not null ,
   LOGINID              VARCHAR (20 ) not null ,
   PASSWORD             VARCHAR (32 ) not null ,
   AGE                  INT ,
   ADDRESS              VARCHAR (255 ) ,
   MOBILE_PHONE         VARCHAR (20 ) ,
   PHS                  VARCHAR (20 ) ,
   PHONE                VARCHAR (20 ) ,
   DEGREE               VARCHAR (20 ) ,
   primary key (ID )
) ;



现在假如除了ID、用户名、密码、登录号之外,所有属性都作为Hibernate的一个详细信息,那我们可以这样设计我们的类:
User.java

 程序代码
package zizz .model ;

import java .io .Serializable ;

/**
* 该对象是针对着数据库的T_USER表.
*
* <a href="http://www.suneca.com">ZIZZ</a>
*
* Create-Time:Apr 21, 2008 9:53:58 PM
*/

public class User implements Serializable {

     /**
     * serialVersionUID
     */

     private static final long serialVersionUID = 1L ;

     private int id ;
    
     private String userName ;
    
     private String loginId ;
    
     private String password ;
    
     private UserDetail userDetail ;

     public UserDetail getUserDetail ( ) {
         return userDetail ;
     }

     public void setUserDetail (UserDetail userDetail ) {
         this .userDetail = userDetail ;
     }

     public int getId ( ) {
         return id ;
     }

     public void setId ( int id ) {
         this .id = id ;
     }

     public String getUserName ( ) {
         return userName ;
     }

     public void setUserName (String userName ) {
         this .userName = userName ;
     }

     public String getLoginId ( ) {
         return loginId ;
     }

     public void setLoginId (String loginId ) {
         this .loginId = loginId ;
     }

     public String getPassword ( ) {
         return password ;
     }

     public void setPassword (String password ) {
         this .password = password ;
     }
}



UserDetail.java

 程序代码
package zizz .model ;

import java .io .Serializable ;

/**
* 用户详情信息.
*
* <a href="http://www.suneca.com">ZIZZ</a>
*
* Create-Time:Apr 23, 2008 10:39:47 PM
*/

public class UserDetail implements Serializable {

     /**
     * serialVersionUID
     */

     private static final long serialVersionUID = 1L ;

     private int age ;
    
     private String address ;
    
     private String mobilePhone ;
    
     private String phs ;
    
     private String phone ;
    
     private String degree ;

     public int getAge ( ) {
         return age ;
     }

     public void setAge ( int age ) {
         this .age = age ;
     }

     public String getAddress ( ) {
         return address ;
     }

     public void setAddress (String address ) {
         this .address = address ;
     }

     public String getMobilePhone ( ) {
         return mobilePhone ;
     }

     public void setMobilePhone (String mobilePhone ) {
         this .mobilePhone = mobilePhone ;
     }

     public String getPhs ( ) {
         return phs ;
     }

     public void setPhs (String phs ) {
         this .phs = phs ;
     }

     public String getPhone ( ) {
         return phone ;
     }

     public void setPhone (String phone ) {
         this .phone = phone ;
     }

     public String getDegree ( ) {
         return degree ;
     }

     public void setDegree (String degree ) {
         this .degree = degree ;
     }
    
}



User的hibernate mapping file
User.hbm.xml

 程序代码
< ?xml version = "1.0" encoding = "UTF-8" ? >
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package = "zizz.model" >
     <class name = "User" table = "T_USER" >
         <id name = "id" column = "ID" >
             <generator class = "increment" / >
         < /id >
         <property name = "userName" column = "USER_NAME" / >
         <property name = "loginId" column = "LOGINID" / >
         <property name = "password" column = "PASSWORD" / >
        
         <component name = "userDetail" class = "UserDetail" lazy = "true" >
             <property name = "age" column = "AGE" / >
             <property name = "address" column = "ADDRESS" / >
             <property name = "mobilePhone" column = "MOBILE_PHONE" / >
             <property name = "phs" column = "PHS" / >
             <property name = "phone" column = "PHONE" / >
             <property name = "degree" column = "DEGREE" / >
         < /component >
     < /class >
< /hibernate-mapping >



测试类:

 程序代码
package zizz .test ;

import zizz .dao .UserDAO ;
import zizz .dao .hibernate .UserDAOHibernate ;
import zizz .model .User ;
import zizz .model .UserDetail ;

/**
* 测试Hibernate Component.
* <a href="http://www.suneca.com">ZIZZ</a>
*
* Create-Time:Apr 23, 2008 11:33:30 PM
*/

public class ComponentTest {
    
     public static void main (String [ ] args ) {
        ComponentTest test = new ComponentTest ( ) ;
        test .testFind ( ) ;
     }
    
     /**
     * 测试创建一个新帐号
     */

     public void testCreateUser ( ) {
        UserDAO dao   = new UserDAOHibernate ( ) ;
        
        User user = new User ( ) ;
        user .setLoginId ( "cyz" ) ;
        user .setPassword ( "admin" ) ;
        user .setUserName ( "广州IT培训" ) ;
        
        UserDetail detail = new UserDetail ( ) ;
        detail .setAddress ( "华南师范大学计算机学院" ) ;
        detail .setAge (20 ) ;
        detail .setDegree ( "工学硕士" ) ;
        detail .setMobilePhone ( "13888888888" ) ;
        detail .setPhone ( "020-85210088" ) ;
        detail .setPhs ( "020-85210088" ) ;
        
        user .setUserDetail (detail ) ;
        
        dao .createUser (user ) ;        
     }
    
     /**
     * 测试根据ID查找用户
     */

     public void testFind ( ) {
        UserDAO dao   = new UserDAOHibernate ( ) ;
        User user = dao .findUserById (1 ) ;
        System .out .println (user .getUserDetail ( ) ) ;
     }
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页