分享
 
 
 

hibernate杂记

王朝java/jsp·作者佚名  2006-05-04
窄屏简体版  字體: |||超大  

一、环境配置

path

c:\ruby\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\WBEM;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Nokia\Tools\Nokia_Developers_Suite_for_J2ME_3_0\bin;C:\Nokia\Update_Manager\bin;C:\Program Files\SecureCRT;%JAVA_HOME%\bin;C:\Python24;C:\ruby\bin;C:\ruby;C:\Python24\bin;C:\JavaServer\mysql-5.0.16-win32;C:\JavaServer\mysql-5.0.16-win32\bin;D:\Dev-Cpp\MinGW\bin;%ANT_HOME%\BIN;C:\JavaServer\MySQL Server 5.0\bin;;C:\Program Files\cvsnt;C:\PROGRA~1\ATT\Graphviz\bin;%MAVEN_HOME%\bin

MAVEN_HOME

C:\JavaServer\maven-2.0.3

TOMCAT_HOME

C:\JavaServer\apache-tomcat-5.5.14

RUBYOPT

rubygems rubygems rubygems

JAVA_HOME

C:\Program Files\Java\jdk1.5.0_02

CLASSPATH

C:\Program Files\Java\jdk1.5.0_02\lib\tools.jar;C:\Program Files\Java\jdk1.5.0_02\lib\dt.jar;C:\JavaServer\MySQLJDBC\mm.mysql-2.0.4-bin.jar;

ANT_HOME

g:\ant

二、eclipse编辑器:

1. 代码上下行移动:alt+arrow up alt+arrow down

2、行COPY:ctrl+alt+arrow up/down

3、字母大小写转换,选定代码:ctrl+shift+x/y

三、hibernate工具和插件

首先安装好JDK和ANT,然后下载相关的HIBERNATE库,以及要用到的JAVA jar文件;

HibernateTools-3.1.0.beta4.zip的使用小技巧

1、解压后把插件目录copy到ECLIPSE的安装目录下;

2、1、 创建cfg文件。Ctrl + N 打开新建窗口,创建一个hibernate configuration file(cfg.xml)

3、创建一个hibernate.cfg.xml配置文件(自己手写也可以)

文件内容如下:

<?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">

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost/yxhservice2</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

</session-factory>

</hibernate-configuration>

4、在窗口-》打开透视图的其他选择hibernage console

在窗口-》显示透视图,选择hibernate configurations,新建一个配置;

可以通过Ctrl + N 带出如下窗体中选择Hibernate Console Configuration打开创建的界面。

在configuation file里找到第2部创建的文件,在mapping files添加相关数据库表的hbm.xml文件,这些文件就是你想创建JAVA文件的表MAP文件。

在classpath添加POJO和driver的jar 文件。这里只需加入mysql-connetor-java-x.x.x-bin.jar

5、在工具栏点击hibernate code generation...

在窗口里,点击NEW,新建一个配置。

main页,在console configuration里选择第4步创建的配置;

output directory里输入生成HBM.XML的POJO代码存放的路径;

在exporter页,选择GENERATE DOMAIN CODE,GENERATE DAO CODE,GENERATE MAPPINGS,等等。这些都设好以后你就可以通过 Run button来产生代码了。

6、使用Console

我们现在可以把刚才生成的java class和mapping文件加到console中来了。

打开Hibernate console透视图。

可以查询HQL,和显示hibernate entity model等等功能;

7、注意数据库兼容性;我用最新的MYSQL5就支持的不是很好。

Middlegen-Hibernate-r5的使用小技巧:

1、修改build.xml里面hibernate3.jar的路径

修改hibernate-extension 的TOOLS路径;

如下面配置;

<pathelement path="../hibernate-3.1/hibernate3.jar"/>

<fileset dir="../hibernate-3.1/lib">

<include name="*.jar"/>

</fileset>

<pathelement path="../hibernate-extensions-2.1.3/tools/hibernate-tools.jar"/>

<fileset dir="../hibernate-extensions-2.1.3/tools/lib">

<include name="*.jar"/>

</fileset>

2、定义生成xml文件的路径;

<property name="build.gen-src.dir" value="${build.dir}/gen-src"/>

3、定义使用的数据库配置,配置文件在config\database目录下:

4、如果使用hibernate3,那么生成的hbm.xml文件的内容需要作一下修改:

生成的hbm.xml文件头:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

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

修改为:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

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

<!DOCTYPE project [

<!ENTITY database SYSTEM "file:./config/database/mysql.xml">

]>

附文件一:

build.xml文件内容:

<?xml version="1.0"?>

<!--

This is a testing platform for the middlegen hibernate plugin. It is a stripped

down version of the samples application. At the moment it is only concerned

with the generation of the mapping documents so that various tests etc can be

done on the generated package.

David Channon

The DOCTYPE declaration declares the location of product-specific parts of the

Ant build file. You can change the values of these declarations to point to

other available files. This gives you an opportunity to choose what database.

Note that this syntax has nothing to do with Ant or Middlegen. This

is the standard way defined by w3c to dynamically include external

files in an XML document. Any decent XML parser will do the include

transparently. Try to open this file in Internet Explorer and see

for yourself.

-->

<!DOCTYPE project [

<!ENTITY database SYSTEM "file:./config/database/mysql.xml">

]>

<project name="Middlegen Hibernate" default="all" basedir=".">

<!-- project name="Middlegen Hibernate" default="all" basedir="." -->

<property file="${basedir}/build.properties"/>

<property name="name" value="service2"/>

<!-- This was added because we were several people (in a course) deploying to same app server>

<property environment="env"/>

<property name="unique.name" value="${name}.${env.COMPUTERNAME}"/-->

<property name="gui" value="true"/>

<property name="unique.name" value="${name}"/>

<property name="appxml.src.file" value="${basedir}/src/application.xml"/>

<property name="lib.dir" value="${basedir}/lib"/>

<property name="src.dir" value="${basedir}/src"/>

<property name="java.src.dir" value="${src.dir}/java"/>

<property name="web.src.dir" value="${src.dir}/web"/>

<property name="build.dir" value="${basedir}/build"/>

<property name="build.java.dir" value="${build.dir}/java"/>

<property name="build.gen-src.dir" value="${build.dir}/gen-src"/>

<property name="build.classes.dir" value="${build.dir}/classes"/>

&database;

<!-- define the datasource.jndi.name in case the imported ejb file doesn't -->

<property name="datasource.jndi.name" value="${name}/datasource"/>

<path id="lib.class.path">

<pathelement path="${database.driver.classpath}"/>

<fileset dir="${lib.dir}">

<include name="*.jar"/>

</fileset>

<!-- The middlegen jars -->

<!--fileset dir="${basedir}/.."-->

<fileset dir="${basedir}/middlegen-lib">

<include name="*.jar"/>

</fileset>

<pathelement path="../hibernate-3.1/hibernate3.jar"/>

<fileset dir="../hibernate-3.1/lib">

<include name="*.jar"/>

</fileset>

<pathelement path="../hibernate-extensions-2.1.3/tools/hibernate-tools.jar"/>

<fileset dir="../hibernate-extensions-2.1.3/tools/lib">

<include name="*.jar"/>

</fileset>

</path>

<target name="init">

<available property="xdoclet1.2+" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="lib.class.path"/>

</target>

<!-- =================================================================== -->

<!-- Fails if XDoclet 1.2.x is not on classpath -->

<!-- =================================================================== -->

<target name="fail-if-no-xdoclet-1.2" unless="xdoclet1.2+">

<fail>

You must download several jar files before you can build Middlegen.

</fail>

</target>

<!-- =================================================================== -->

<!-- Create tables -->

<!-- =================================================================== -->

<target

name="create-tables"

depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"

description="Create tables"

>

<echo>Creating tables using URL ${database.url}</echo>

<sql

classpath="${database.driver.classpath}"

driver="${database.driver}"

url="${database.url}"

userid="${database.userid}"

password="${database.password}"

src="${database.script.file}"

print="true"

output="result.txt"

/>

</target>

<target name="check-driver-present">

<available file="${database.driver.file}" type="file" property="driver.present"/>

</target>

<target name="panic-if-driver-not-present" unless="driver.present">

<fail>

The JDBC driver you have specified by including one of the files in ${basedir}/config/database

doesn't exist. You have to download this driver separately and put it in ${database.driver.file}

Please make sure you're using a version that is equal or superior to the one we looked for.

If you name the driver jar file differently, please update the database.driver.file property

in the ${basedir}/config/database/xxx.xml file accordingly.

</fail>

</target>

<!-- =================================================================== -->

<!-- Run Middlegen -->

<!-- =================================================================== -->

<target

name="middlegen"

description="Run Middlegen"

unless="middlegen.skip"

depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"

>

<mkdir dir="${build.gen-src.dir}"/>

<echo message="Class path = ${basedir}"/>

<taskdef

name="middlegen"

classname="middlegen.MiddlegenTask"

classpathref="lib.class.path"

/>

<middlegen

appname="${name}"

prefsdir="${src.dir}"

gui="${gui}"

databaseurl="${database.url}"

initialContextFactory="${java.naming.factory.initial}"

providerURL="${java.naming.provider.url}"

datasourceJNDIName="${datasource.jndi.name}"

driver="${database.driver}"

username="${database.userid}"

password="${database.password}"

schema="${database.schema}"

catalog="${database.catalog}"

>

<!--

We can specify what tables we want Data generated for.

If none are specified, Data will be generated for all tables.

Comment out the <table> elements if you want to generate for all tables.

Also note that table names are CASE SENSITIVE for certain databases,

so on e.g. Oracle you should specify table names in upper case.

-->

<!--table generate="true" name="flights" pktable="flights_pk"/>

<table name="reservations"/-->

<!--

If you want m:n relations, they must be specified like this.

Note that tables declare in multiple locations must all have

the same value of the generate attribute.

-->

<!--many2many>

<tablea generate="true" name="persons"/>

<jointable name="reservations" generate="false"/>

<tableb generate="true" name="flights"/>

</many2many-->

<!-- Plugins - Only Hibernate Plugin has been included with this special distribution -->

<!--

If you want to generate XDoclet markup for hbm2java to include in the POJOs then

set genXDocletTags to true. Also, composite keys are generated as external classes which is

recommended. If you wish to keep them internal then set genIntergratedCompositeKeys to true.

Since r4 the ability to customise the selection of JavaTypes is now provided. The is a

recommended type mapper provided as shown. It is optional - if not provided then Middlegen

itself will select the Java mapping (as it did previously).

These settings are optional thus if they are not define here values default to false.

-->

<hibernate

destination="${build.gen-src.dir}"

package="com.yxh"

genXDocletTags="true"

genIntergratedCompositeKeys="false"

javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"

/>

</middlegen>

<mkdir dir="${build.classes.dir}"/>

</target>

<!-- =================================================================== -->

<!-- Compile business logic (hibernate) -->

<!-- =================================================================== -->

<target name="compile-hibernate" depends="middlegen" description="Compile hibernate Business Domain Model">

<javac

srcdir="${build.gen-src.dir}"

destdir="${build.classes.dir}"

classpathref="lib.class.path"

>

<include name="**/hibernate/**/*"/>

</javac>

</target>

<!-- =================================================================== -->

<!-- Run hbm2java depends="middlegen" -->

<!-- =================================================================== -->

<target name="hbm2java" description="Generate .java from .hbm files.">

<taskdef

name="hbm2java"

classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"

classpathref="lib.class.path"

/>

<hbm2java output="${build.gen-src.dir}">

<fileset dir="${build.gen-src.dir}">

<include name="**/*.hbm.xml"/>

</fileset>

</hbm2java>

</target>

<!-- =================================================================== -->

<!-- Build everything -->

<!-- =================================================================== -->

<target name="all" description="Build everything" depends="compile-hibernate"/>

<!-- =================================================================== -->

<!-- Clean everything -->

<!-- =================================================================== -->

<target name="clean" description="Clean all generated stuff">

<delete dir="${build.dir}"/>

</target>

<!-- =================================================================== -->

<!-- Brings up the hsqldb admin tool -->

<!-- =================================================================== -->

<target name="hsqldb-gui" description="Brings up the hsqldb admin tool">

<property name="database.urlparams" value="?user=${database.userid}&amp;password=${database.password}"/>

<java

classname="org.hsqldb.util.DatabaseManager"

fork="yes"

classpath="${lib.dir}/hsqldb-1.7.1.jar;${database.driver.classpath}"

failonerror="true"

>

<arg value="-url"/>

<arg value="${database.url}${database.urlparams}"/>

<arg value="-driver"/>

<arg value="${database.driver}"/>

</java>

</target>

<!-- ==================================================================== -->

<!-- Validate the generated xml mapping documents -->

<!-- ==================================================================== -->

<target name="validate">

<xmlvalidate failonerror="no" lenient="no" warn="yes">

<fileset dir="${build.gen-src.dir}/airline/hibernate" includes="*.xml" />

</xmlvalidate>

</target>

</project>

附文件二

mysql.xml文件内容:

<!-- =================================================================== -->

<!-- ant properties/targets for mysql -->

<!-- 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="${src.dir}/sql/${name}-mysql.sql"/>

<property name="database.driver.file" value="${lib.dir}/mysql-connector-java-5.0.0-beta-bin.jar"/>

<property name="database.driver.classpath" value="${database.driver.file}"/>

<!-- property name="database.driver" value="org.gjt.mm.mysql.Driver"/ -->

<property name="database.driver" value="com.mysql.jdbc.Driver"/>

<property name="database.url" value="jdbc:mysql://localhost/yxhservice2"/>

<property name="database.userid" value="root"/>

<property name="database.password" value=""/>

<property name="database.schema" value=""/>

<property name="database.catalog" value=""/>

<property name="jboss.datasource.mapping" value="mySQL"/>

maven-2.0.3的使用技巧

1、下载maven,然后设置好系统环境变量和系统路径:

MAVEN_HOME

C:\JavaServer\maven-2.0.3

path

c:\ruby\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\WBEM;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Nokia\Tools\Nokia_Developers_Suite_for_J2ME_3_0\bin;C:\Nokia\Update_Manager\bin;C:\Program Files\SecureCRT;%JAVA_HOME%\bin;C:\Python24;C:\ruby\bin;C:\ruby;C:\Python24\bin;C:\JavaServer\mysql-5.0.16-win32;C:\JavaServer\mysql-5.0.16-win32\bin;D:\Dev-Cpp\MinGW\bin;%ANT_HOME%\BIN;C:\JavaServer\MySQL Server 5.0\bin;;C:\Program Files\cvsnt;C:\PROGRA~1\ATT\Graphviz\bin;%MAVEN_HOME%\bin

2、如果不修改也可以使用,如果需要个性化的设置,打开maven-2.0.3\conf下的setting.xml文件

修改本地库的存储路径:

<localRepository>C:\JavaServer\maven-2.0.3\Repository</localRepository>

3、在<profiles>节修改好路径:

<profile>

<id>tomcat-home</id>

<properties>

<tomcat.home>C:\JavaServer\apache-tomcat-5.5.14</tomcat.home>

</properties>

</profile>

4、在activeProfiles节增加

<activeProfile>tomcat-home</activeProfile>

5、设置其他的设置例如,proxy等等。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有