ExtJS实战(2)-hibernate

现在我们已经看过了主要项目截图,并且利用MyEclipse加入了SSH的支持和其它框架的JAR包,搭建好了我们的应用环境。(这里因为MyEclipse自动导入的各个框架的包会存在版本冲突,所以我们建议只加入MyEclipse对各大框架的支持,完了后将它自动导入的JAR包从项目中移除,换成我们自己下载的包,这样可以更好的控制,做到所用的JAR包心中有数!),任何应用,数据库总是要先设计好。以下是本应用的SQL脚本:

       create database houseProject

go

use houseProject

go

create table [user]--用户

(

     uid int primary key identity(1,1),

     uname varchar(30),

     upass varchar(30)

)

insert into [user] values('leno','leno')

insert into [user] values('oak','oak')

insert into [user] values('java','java')

create table area--区域

(

     aid int primary key identity(1,1),

     aname varchar(30),

)

insert into area values('洪山区')

insert into area values('江汉区')

insert into area values('硚口区')

insert into area values('江岸区')

create table street--街道

(

     sid int primary key identity(1,1),

     sname varchar(30),

     aid int,

     foreign key(aid) references area(aid)

)

go

insert into street values('武珞路',1)

insert into street values('珞狮路',1)

insert into street values('新华路',2)

insert into street values('建设大道',2)

insert into street values('中山大道',3)

insert into street values('崇仁路',3)

insert into street values('临江大道',4)

insert into street values('沿江大道',4)

 

--drop table house

create table house--租房信息

(

     hid int primary key identity(1,1),

     title varchar(100),

     hireprice numeric(6,1),

     booktime datetime,

     linkman varchar(30),

     linktel varchar(20),

     housething varchar(200),

     room int,--

     ting int,--

     uid int,--用户

     sid int,--街道

     foreign key(uid) references [user](uid),

     foreign key(sid) references street(sid)

)

 

insert into house values('太好的房子!',1000,'2008-8-24','张先生','13326446513','床、柜子、沙发、天然气、暖气、灶具、热水器、煤气罐、电话、宽带、防盗门',2,1,1,1)

insert into house values('风水不错!',1800,'2007-8-21','李先生','13871546513','床、柜子、沙发、煤气罐、电话、宽带、闭路电视、防盗门',2,2,3,2)

insert into house values('不错的新房子哦!',3100,'2008-8-27','林女士','13197656513','床、柜子、沙发、天然气、暖气、灶具、热水器、煤气罐、宽带、闭路电视',3,1,8,3)

insert into house values('机会不要错过!',2700,'2006-8-21','王先生','13621546513','床、柜子、沙发、天然气、灶具、热水器、煤气罐、电话、宽带、闭路电视、防盗门',1,1,3,4)

insert into house values('不错的新房子哦!',2200,'2008-9-21','秦小姐','13029806513','床、柜子、沙发、天然气、暖气、灶具、热水器',1,1,1,5)

insert into house values('物超所值!',700,'2008-9-22','张先生','13621546513','床、柜子、沙发、天然气、暖气、热水器、煤气罐、电话、宽带、闭路电视、防盗门',3,1,1,6)

insert into house values('不错的新房子哦!',2800,'2008-10-17','张先生','13987546513','床、柜子、',2,1,1,7)

insert into house values('湖边的好房子!',1900,'2008-10-11','卫先生','13234546513','床、柜子、沙发、天然气、暖气、灶具、热水器、电话、宽带、闭路电视、防盗门',3,2,8,7)

insert into house values('不经典的新房子哦!',3000,'2008-11-3','黄先生','13112546513','床、柜子、暖气、灶具、热水器、煤气罐、宽带、闭路电视、防盗门',3,3,1,8)

 

       注意这里四张表之间的关系:

 

           

 

    利用MyEclipse反转这四张Table生成对应的POJO类和Mapping映射文件,这是机械化的工作。只是有一点要强调的是在反转之前,要先建立好MyEclipse和前面创建的数据库的连接。在反转之后,MyEclipse会自动将映射信息添加到hibernate.cfg.xml上。我们来看一下MyEclipse自动生成的对应house表的House类和它们的映射文件:

package org.leno.houseHire.dao;

 

import java.util.Date;

 

/**

 * House entity.

 *

 * @author MyEclipse Persistence Tools

 */

 

public class House implements java.io.Serializable {

 

    // Fields

 

    private Integer hid;

    private Street street;

    private User user;

    private String title;

    private Double hireprice;

    private Date booktime;

    private String linkman;

    private String linktel;

    private String housething;

    private Integer room;

    private Integer ting;

 

    // Constructors

 

    /** default constructor */

    public House() {

    }

 

    /** full constructor */

    public House(Street street, User user, String title, Double hireprice,

            Date booktime, String linkman, String linktel, String housething,

            Integer room, Integer ting) {

        this.street = street;

        this.user = user;

        this.title = title;

        this.hireprice = hireprice;

        this.booktime = booktime;

        this.linkman = linkman;

        this.linktel = linktel;

        this.housething = housething;

        this.room = room;

        this.ting = ting;

    }

 

    // Property accessors

 

    public Integer getHid() {

        return this.hid;

    }

 

    public void setHid(Integer hid) {

        this.hid = hid;

    }

 

    public Street getStreet() {

        return this.street;

    }

 

    public void setStreet(Street street) {

        this.street = street;

    }

 

    public User getUser() {

        return this.user;

    }

 

    public void setUser(User user) {

        this.user = user;

    }

 

    public String getTitle() {

        return this.title;

    }

 

    public void setTitle(String title) {

        this.title = title;

    }

 

    public Double getHireprice() {

        return this.hireprice;

    }

 

    public void setHireprice(Double hireprice) {

        this.hireprice = hireprice;

    }

 

    public Date getBooktime() {

        return this.booktime;

    }

 

    public void setBooktime(Date booktime) {

        this.booktime = booktime;

    }

 

    public String getLinkman() {

        return this.linkman;

    }

 

    public void setLinkman(String linkman) {

        this.linkman = linkman;

    }

 

    public String getLinktel() {

        return this.linktel;

    }

 

    public void setLinktel(String linktel) {

        this.linktel = linktel;

    }

 

    public String getHousething() {

        return this.housething;

    }

 

    public void setHousething(String housething) {

        this.housething = housething;

    }

 

    public Integer getRoom() {

        return this.room;

    }

 

    public void setRoom(Integer room) {

        this.room = room;

    }

 

    public Integer getTing() {

        return this.ting;

    }

 

    public void setTing(Integer ting) {

        this.ting = ting;

    }

 

}

 

House.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">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.leno.houseHire.dao.House" table="house" schema="dbo" catalog="houseProject">

        <id name="hid" type="java.lang.Integer">

            <column name="hid" />

            <generator class="native" />

        </id>

        <many-to-one name="street" class="org.leno.houseHire.dao.Street" fetch="select" lazy="false">

            <column name="sid" />

        </many-to-one>

        <many-to-one name="user" class="org.leno.houseHire.dao.User" fetch="select" lazy="false">

            <column name="uid" />

        </many-to-one>

        <property name="title" type="java.lang.String">

            <column name="title" length="100" />

        </property>

        <property name="hireprice" type="java.lang.Double">

            <column name="hireprice" precision="6" scale="1" />

       </property>

        <property name="booktime" type="java.util.Date">

            <column name="booktime" length="23" />

        </property>

        <property name="linkman" type="java.lang.String">

            <column name="linkman" length="30" />

       </property>

        <property name="linktel" type="java.lang.String">

            <column name="linktel" length="20" />

        </property>

        <property name="housething" type="java.lang.String">

            <column name="housething" length="200" />

        </property>

        <property name="room" type="java.lang.Integer">

            <column name="room" />

        </property>

        <property name="ting" type="java.lang.Integer">

            <column name="ting" />

        </property>

    </class>

</hibernate-mapping>

 

       这些ORMObject-Relational Mapping)的关系都是Hibernate的基础知识,又是工具自动生成的代码,我就不贴出来赘述了。需要强调的地方有3点:

 

(1)        因为userSQL的关键字,所以在User.hbm.xml上要注意table="[user]",也就是在表名上加一对中括号。这一点MyEclipse帮我们自动生成的时候没有做好。

(2)        我们在获得House对象的时候,如果想要级联地获得它所关联的User对象和Street对象,我们就需要在关联级别上设置lazy="false",这样在session关闭之后就依然能够利用House对象导航到它所关联的其它对象上去。

(3)        因为我们这四张表的ID都是SQLServer自增长的,所以ID生成器的配置<generator class="native" />可以为identity或者native(充分利用本地数据库自身特性).

 

好了,hibernate是我们的应用和数据库之间的桥梁,它封装了对数据库的访问细节。而且,有了MyEclipsehibernate的支持,大大地提高了我们的开发效率。下面是它的核心配置文件:

 

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

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

 

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

 

    <session-factory>

        <!-- configure datasource -->

        <property name="connection.driver_class">

            com.microsoft.jdbc.sqlserver.SQLServerDriver

        </property>

        <property name="connection.url">

            jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=houseProject;SelectMethod=cursor

        </property>

        <property name="connection.username">sa</property>

        <property name="connection.password">123</property>

        <property name="hibernate.connection.pool_size">20</property>

        <property name="hibernate.c3p0.min_size">1</property>

        <property name="hibernate.c3p0.max_size">20</property>

        <property name="hibernate.c3p0.timeout">1800</property>

        <property name="hibernate.c3p0.max_statements">50</property>

        <!-- configure fetch_depth -->

        <property name="hibernate.max.fetch_depth">5</property>

        <!-- configure dialect -->

        <property name="dialect">

            org.hibernate.dialect.SQLServerDialect

        </property>

        <!--auto generate datatable-->

        <!-- <property name="hibernate.hbm2ddl.auto">create</property> -->

        <!-- configure show_sql -->

        <property name="hibernate.show_sql">false</property>

        <!-- configure transactionFactory -->

        <property name="hibernate.transaction.factory_class">

            org.hibernate.transaction.JDBCTransactionFactory

        </property>

        <mapping resource="org/leno/houseHire/dao/House.hbm.xml" />

        <mapping resource="org/leno/houseHire/dao/Area.hbm.xml" />

        <mapping resource="org/leno/houseHire/dao/User.hbm.xml" />

        <mapping resource="org/leno/houseHire/dao/Street.hbm.xml" />

 

    </session-factory>

 

</hibernate-configuration>

 

最后看看我们整个工程的目录结构:

 

posted @ 2010-12-02 22:07  hannover  阅读(594)  评论(0编辑  收藏  举报