hibernate component

Component

本次做MySNS时也用到了Component,在School.java和Address.java以及school.hbm.xml

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

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

建表语句:

程序代码

droptableifexistsT_USER;

/*==============================================================*/

/*Table:T_USER*/

/*==============================================================*/

createtableT_USER

(

IDINTnotnullauto_increment,

USER_NAMEVARCHAR(20)notnull,

LOGINIDVARCHAR(20)notnull,

PASSWORDVARCHAR(32)notnull,

AGEINT,

ADDRESSVARCHAR(255),

MOBILE_PHONEVARCHAR(20),

PHSVARCHAR(20),

PHONEVARCHAR(20),

DEGREEVARCHAR(20),

primarykey(ID)

);

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

User.java

程序代码

packagezizz.model;

importjava.io.Serializable;

/**

*该对象是针对着数据库的T_USER表.

*

*<ahref="http://www.suneca.com">ZIZZ</a>

*

*Create-Time:Apr21,20089:53:58PM

*/

publicclassUserimplementsSerializable{

/**

*serialVersionUID

*/

privatestaticfinallongserialVersionUID=1L;

privateintid;

privateStringuserName;

privateStringloginId;

privateStringpassword;

privateUserDetailuserDetail;

publicUserDetailgetUserDetail(){

returnuserDetail;

}

publicvoidsetUserDetail(UserDetailuserDetail){

this.userDetail=userDetail;

}

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetUserName(){

returnuserName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

publicStringgetLoginId(){

returnloginId;

}

publicvoidsetLoginId(StringloginId){

this.loginId=loginId;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

}

UserDetail.java

程序代码

packagezizz.model;

importjava.io.Serializable;

/**

*用户详情信息.

*

*<ahref="http://www.suneca.com">ZIZZ</a>

*

*Create-Time:Apr23,200810:39:47PM

*/

publicclassUserDetailimplementsSerializable{

/**

*serialVersionUID

*/

privatestaticfinallongserialVersionUID=1L;

privateintage;

privateStringaddress;

privateStringmobilePhone;

privateStringphs;

privateStringphone;

privateStringdegree;

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

publicStringgetAddress(){

returnaddress;

}

publicvoidsetAddress(Stringaddress){

this.address=address;

}

publicStringgetMobilePhone(){

returnmobilePhone;

}

publicvoidsetMobilePhone(StringmobilePhone){

this.mobilePhone=mobilePhone;

}

publicStringgetPhs(){

returnphs;

}

publicvoidsetPhs(Stringphs){

this.phs=phs;

}

publicStringgetPhone(){

returnphone;

}

publicvoidsetPhone(Stringphone){

this.phone=phone;

}

publicStringgetDegree(){

returndegree;

}

publicvoidsetDegree(Stringdegree){

this.degree=degree;

}

}

User的hibernatemappingfile

User.hbm.xml

程序代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="zizz.model">

<classname="User"table="T_USER">

<idname="id"column="ID">

<generatorclass="increment"/>

</id>

<propertyname="userName"column="USER_NAME"/>

<propertyname="loginId"column="LOGINID"/>

<propertyname="password"column="PASSWORD"/>

<componentname="userDetail"class="UserDetail"lazy="true">

<propertyname="age"column="AGE"/>

<propertyname="address"column="ADDRESS"/>

<propertyname="mobilePhone"column="MOBILE_PHONE"/>

<propertyname="phs"column="PHS"/>

<propertyname="phone"column="PHONE"/>

<propertyname="degree"column="DEGREE"/>

</component>

</class>

</hibernate-mapping>

测试类:

程序代码

packagezizz.test;

importzizz.dao.UserDAO;

importzizz.dao.hibernate.UserDAOHibernate;

importzizz.model.User;

importzizz.model.UserDetail;

/**

*测试HibernateComponent.

*<ahref="http://www.suneca.com">ZIZZ</a>

*

*Create-Time:Apr23,200811:33:30PM

*/

publicclassComponentTest{

publicstaticvoidmain(String[]args){

ComponentTesttest=newComponentTest();

test.testFind();

}

/**

*测试创建一个新帐号

*/

publicvoidtestCreateUser(){

UserDAOdao=newUserDAOHibernate();

Useruser=newUser();

user.setLoginId("cyz");

user.setPassword("admin");

user.setUserName("");

UserDetaildetail=newUserDetail();

detail.setAddress("");

detail.setAge(20);

detail.setDegree("");

detail.setMobilePhone("");

detail.setPhone("");

detail.setPhs("");

user.setUserDetail(detail);

dao.createUser(user);

}

/**

*测试根据ID查找用户

*/

publicvoidtestFind(){

UserDAOdao=newUserDAOHibernate();

Useruser=dao.findUserById(1);

System.out.println(user.getUserDetail());

}

}

相关推荐