摘要:
本文介绍了一个简单的 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 字节
7 个目录 1,531,150,336 可用字节
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 字节
3 个目录 1,528,754,176 可用字节
我们所要的 .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 .