Hibernate学习手记

本文介绍Hibernate的基本使用流程,包括安装配置、映射文件的创建及如何使用工具自动生成POJO类和数据库表。同时展示了如何通过Ant任务自动化这些过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<Hibernate:the Develper's note book>       初次接触Hibernate,也不知道该参照哪本书,现在手上只有一本借来的《Hibernate程序高手秘笈》[Hibernate:The Developer's note book],就拿这个做教材吧。。。不过好像是因为台湾人翻译的,所以语句上有些看不懂。。下面进入正题:

一:开发前的准备:安装和设置

1. 去官网--http://www.hibernate.org/ 下载Hibernate最新版本。这里要注意,Hibernate有很多包,核心包,扩展包,工具包等等。在官网DOWNLOAD页面有一个列表,根据需要进行下载。如果只需要基本功能,则只需下载Hibernate.Core即可。

2.解压后把Hibernate.jar包COPY进自己工作区的LIB里

 

二:使用映射文件和Hibernate产生POJO和数据库DDL

Hibernate 使用XML文件来记录java class与关系型数据库表之间的关系。这种映射文件被设计成可读且可手动编辑的。你可以用图形化工具(象Rose,Together,Poseidon)来建立代表数据模型的UML图形,再利用AndroMDA将其转化成Hibernate所需的映射数据。

备注:Hibernate的扩展工具可以让你以其他方式运作,比如你已经有了classes或数据,就可以由他们生成其他的部分。

下面是一个典型的映射文件:(Track.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"
>

< hibernate-mapping >

  
< class  name ="com.oreilly.hh.Track"  table ="TRACK" >
    
< meta  attribute ="class-description" >
      Represents a single playable track in the music database.
      @author Jim Elliott (with help from Hibernate)
    
</ meta >

    
< id  name ="id"  type ="int"  column ="TRACK_ID" >
      
< meta  attribute ="scope-set" > protected </ meta >
      
< generator  class ="native" />
    
</ id >

    
< property  name ="title"  type ="string"  not-null ="true" />

    
< property  name ="filePath"  type ="string"  not-null ="true" />

    
< property  name ="playTime"  type ="time" >
      
< meta  attribute ="field-description" > Playing time </ meta >
    
</ property >

    
< property  name ="added"  type ="date" >
      
< meta  attribute ="field-description" > When the track was created </ meta >
    
</ property >

    
< property  name ="volume"  type ="short"  not-null ="true" >
      
< meta  attribute ="field-description" > How loud to play the track </ meta >
    
</ property >

  
</ class >
</ hibernate-mapping >

其中<class name="com.oreilly.hh.Track" table="TRACK">指定了类与表的映射关系。

<meta>标记是提供额外信息给其他工具,去掉后并不影响映射内容。在这里<meta attribute="class-description">是给JAVADOC使用的,告诉JAVADOC,这是一个类的说明。而<meta attribute="field-description">Playing time</meta>则告诉JAVADOC,这是一个属性的说明。

<id>标记则指定了该对象的primary key.

   生成CLASS文件    

在Hibernate Extensions包里有一个工具--hbm2java (net.sf.hibernate.tool.hbm2java包中),只需要告诉它指定要转换的.hbm.xml文件,它就可以自动的生成该文件中对应类的POJO。

     生成数据库表      

在核心包里有一个工具--schemaexport,它会根据.hbm.xml 文件的内容,自动产生关系数据库表,你需要指定的,一个是.hbm.xml ,一个是property文件,里面记录了连接数据库的一些必要信息。

下面是Hibernate连接Oracle的property文件:

三:与ant结合使用

 

< project  name ="Hibernate.project"  default =""  basedir ="." >
    
< property  name ="src.dir"  value ="src"   />
    
< property  name ="data.dir"  value ="src/data"   />
    
< property  name ="build.dir"  value ="build/app"   />
    
< property  name ="lib.dir"  value ="lib"   />
    
< property  name ="classes.dir"  value ="WebRoot/WEB-INF/classes"   />
    
< property  name ="config.dir"  value ="WebRoot/WEB-INF"   />




    
<!-- 设定编译和执行时的 class path -->
    
< path  id ="project.class.path" >
        
< pathelement  location ="${classes.dir}"   />
        
< fileset  dir ="${lib.dir}" >
            
< include  name ="*.jar"   />
        
</ fileset >
    
</ path >

    
<!-- 使用Hibernate的程序代码生成工具 -->
    
< taskdef  name ="hbm2java"  classname ="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"  classpathref ="project.class.path" >
    
</ taskdef >
 
    
< target  name ="codegen" >
        
< hbm2java  output ="${src.dir}" >
            
< fileset  dir ="${data.dir}" >
                
< include  name ="**/*.hbm.xml"   />
            
</ fileset >
        
</ hbm2java >
    
</ target >

    
<!-- 使用Hibernate的Schema生成工具 -->
    
< target  name ="schema" >
        
< taskdef  name ="schemaexport"  classname ="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"  classpathref ="project.class.path"   />
        
< schemaexport  properties ="${config.dir}/hibernate.properties" >
            
< fileset  dir ="${data.dir}" >
                
< include  name ="**/*.hbm.xml"   />
            
</ fileset >
        
</ schemaexport >
    
</ target >

</ project >

 

 

------------------------未完

                                    待续 --------------------------

补一个Hibernate Core的 结构图

 

hibernate . dialect  net . sf . hibernate . dialect . Oracle9Dialect
hibernate
. connection . driver_class oracle . jdbc . driver . OracleDriver
hibernate
. connection . username gemini
hibernate
. connection . password gemini
hibernate
. connection . url jdbc :oracle:thin :@ 222.18 . 154.78 : 1521 :orcl

 

如果要连接其他类型的数据库,只需要更改property文件即可。

在JAVA中直接使用Hibernate的部分代码以后有空再补上去。主要记录在ANT中使用Hibernate

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值