摘要:
本文介紹了一個簡單的 hibernate+junit+Middlegen的sample.
1.準備
軟件準備
C:hibernate>dir
驅動器 C 中的卷是 本地磁盤
卷的序列號是 08DF-03E4
C:hibernate 的目錄
2005-03-22 11:23 <DIR> .
2005-03-22 11:23 <DIR> ..
2005-03-20 17:14 <DIR> apache-ant-1.6.1
2005-03-21 11:59 <DIR> hibernate-2.1
2005-03-21 08:56 <DIR> hibernate-extensions
2005-02-17 18:14 <DIR> hibernate_handbible
2005-03-21 08:52 <DIR> Middlegen-Hibernate-r5
0 個文件 0 字節(jié)
7 個目錄 1,531,150,336 可用字節(jié)
C:hibernate>
下載Hibernate、Hibernate-Extension和Middlegen-Hibernate軟件包的新版本。
http://prdownloads.sourceforge.net/hibernate/
還需要連接sqlserver的jar包,我用的是sf.net 上的 jtds-0.9.jar(放在C:hibernateMiddlegen-Hibernate-r5lib下)
set Path =%Path%;C:hibernateapache-ant-1.6.1in 為后面ant做準備。
2.數據庫和配置
數據庫為MS Sqlserver, 庫名:testbook
sql腳本:
clearcase/" target="_blank" >cccccc>if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[book]') and OBJECTPROPERTY
(id, N'IsUserTable') = 1)
drop table [dbo].[book]GO
CREATE TABLE [dbo].[book] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[book] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[sn] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON PRIMARY
GO
入MiddleGen 目錄下的configdatabase 子目錄,根據我實際采用的數據庫打開對應的配置文件。如這里我們用的是mssql數據庫,對應的是mssql.xml文件。
C:hibernateMiddlegen-Hibernate-r5configdatabase>type mssql.xml
<!-- =================================================================== -->
<!-- ant properties/targets for MS SQL Server -->
<!-- note: this is not a proper xml file (there is no root element) -->
<!-- it is intended to be imported from a *real* xml file -->
<!-- =================================================================== -->
<property name="database.script.file" value=""/>
<property name="database.driver.file" value="${lib.dir}/jtds-0.9.jar"/>
<property name="database.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="database.url" value="jdbc:jtds:sqlserver://127.0.0.1:1433/testbook"/>
<property name="database.userid" value="sa"/>
<property name="database.password" value="zh1107"/>
<property name="database.schema" value="dbo"/>
<property name="database.catalog" value="testbook"/>
<property name="jboss.datasource.mapping" value="MS SQLSERVER"/>
改build.xml 為符合自己的配置
C:hibernateMiddlegen-Hibernate-r5>type build.xml
<?xml version="1.0"?>
<!DOCTYPE project [
<!ENTITY database SYSTEM "file&:./config/database/mssql.xml">]>
<property name="name" value="HibernateSample"/>
然后
C:hibernateMiddlegen-Hibernate-r5>ant
出來圖形界面,可以不作修改,點擊上面的gen,然后退出。
C:hibernateMiddlegen-Hibernate-r5uildgen-src>dir
2005-03-22 18:50 <DIR> .
2005-03-22 18:50 <DIR> ..
2005-03-22 17:21 <DIR> HibernateSample
0 個文件 0 字節(jié)
3 個目錄 1,528,754,176 可用字節(jié)
我們所要的 .hbm.xml 出來了(在HibernateSample目錄里)
然后到
C:hibernatehibernate-extensions oolsin>type setenv.bat
@echo off
set HIBERNATE_HOME=C:hibernatehibernate-2.1 (改)
echo HIBERNATE_HOME set to %HIBERNATE_HOME%
set JDBC_DRIVER=C:hibernatehibernate-extensionslibjtds-0.9.jar (改)
echo JDBC_DRIVER set to %JDBC_DRIVER%
set HIBERNATETOOLS_HOME=%~dp0..
echo HIBERNATETOOLS_HOME set to %HIBERNATETOOLS_HOME%
if "%HIBERNATE_HOME%" == "" goto noHIBERNATEHome
set CORELIB=%HIBERNATE_HOME%lib
set LIB=%HIBERNATETOOLS_HOME%lib
set CP=%CLASSPATH%;%JDBC_DRIVER%;%HIBERNATE_HOME%hibernate2.jar;%CORELIB%commo
ns-logging-1.0.3.jar;%CORELIB%commons-lang-1.0.1.jar;%CORELIB%cglib-2.0-rc2.jar;%CORELIB%dom4j-1.4.jar;%CORELIB%odmg-3.0.ja
r;%CORELIB%xml-apis.jar;%CORELIB
%xerces-2.4.0.jar;%CORELIB%xalan-2.4.0.jar;%LIB%jdom.jar;%CORELIB%commons-co
llections-2.1.jar;%LIB%..hibernate-tools.jar;
if not "%HIBERNATE_HOME%" == "" goto end
:noHIBERNATEHome
echo HIBERNATE_HOME is not set. Please set HIBERNATE_HOME.
goto end
:end
C:hibernatehibernate-extensions oolsin>
生成PO
C:hibernateMiddlegen-Hibernate-r5uildgen-srcHibernateSamplehibernate>C:h
ibernatehibernate-extensions oolsinhbm2java.bat *.xml --output=.
HIBERNATE_HOME set to C:hibernatehibernate-2.1
JDBC_DRIVER set to C:hibernatehibernate-extensionslibjtds-0.9.jar
HIBERNATETOOLS_HOME set to C:hibernatehibernate-extensions oolsin..
2005-3-22 19:01:44 net.sf.hibernate.tool.hbm2java.Generator generate
信息: Generating 1 in .