hibernate3.2由hbm文件生成pojo和ddl

本文介绍如何使用Hibernate框架进行数据库操作,包括项目搭建步骤、配置文件详解及自动化生成Java源代码和数据库模式。

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

准备:
1.apache-ant-1.7.0.
2.hibernate-3.2.1.ga.zip,HibernateTools-3.2.1.ga.zip.
3.数据库的jdbc驱动程序,我使用的是mysql,驱动程序为mysql-connector-java-5.1.6。

项目布置:
1.建立Project-Name文件夹,其下建立:config,java,schema,lib文件夹以及build.xml文件.
2.加入jar包:hibernate3.jar,hibernate-tools.jar,freemarker.jar,mysql-connector-java.jar,...
3.src/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="connection.username">root</property>
  
<property name="connection.password">cosmo</property><!--Your DB password here.-->
  
<property name="connection.url">jdbc:mysql://localhost:3306/MasteryHibernate</property>
  
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  
<mapping resource="chapter3/Customer.hbm.xml" />
 
</session-factory>
</hibernate-configuration>

4.src/your-package/Customer.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"
>

<hibernate-mapping>
    
<class name="chapter3.Customer" table="CUSTOMERS">
        
<meta attribute="class-description">
            Represents a single customer.
            @author Cosmo
        
</meta>

        
<meta attribute="class-scope">public</meta>

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

        
<property name="name" type="string">
            
<meta attribute="use-in-tostring">true</meta>
            
<column name="NAME" length="15" not-null="true" unique="true" />
        
</property>

        
<property name="registeredTime" type="timestamp">
            
<meta attribute="field-description">When the customer was registered</meta>
            
<meta attribute="use-in-tostring">true</meta>
            
<column name="REGISTERED_TIME" index="IDX_REGISTERED_TIME"
                sql-type
="timestamp" />
        
</property>
        
        
<property name="age" type="int">
            
<meta attribute="field-description">How old is the customer</meta>
            
<meta attribute="use-in-tostring">true</meta>
            
<column name="AGE" check="AGE>10" not-null="true" />
        
</property>
        
        
<property name="sex" type="char" column="SEX" />
        
        
<property name="married" type="boolean" column="IS_MARRIED">
            
<meta attribute="field-description">Is the customer married</meta>
            
<meta attribute="use-in-tostring">true</meta>
        
</property>
        
        
<property name="description" type="string">
            
<meta attribute="use-in-tostring">true</meta>
            
<column name="DESCRIPTION" sql-type="text" />
        
</property>
    
</class>
</hibernate-mapping>

5.build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     2008-12-12 下午02:47:47                                                        

     Hbm2javaAndHbm2ddl    
     Useing hibernate-tools
                   
     Cosmo                                                                
     ====================================================================== 
-->
<project name="AboutHibernateTools" default="compile">
    
<description>
            AboutHibernateTools
    
</description>

    
<property name="source.root" value="./src" />
    
<property name="class.root" value="./classes" />
    
<property name="lib.dir" value="./lib" />
    
<property name="schema.dir" value="./schema" />

    
<path id="project.class.path">
        
<pathelement location="${class.root}" />
        
<fileset dir="${lib.dir}">
            
<include name="*.jar" />
        
</fileset>
    
</path>

    
<!-- ================================= 
          target: run              
         ================================= 
-->
    
<target name="run" depends="hbm2ddl" description="Run a Hibernate sample">
        
<java classname="chapter3.BusinessService" fork="true">
            
<classpath refid="project.class.path" />
        
</java>
    
</target>


    
<!-- ================================= 
          target: hbm2ddl              
         ================================= 
-->
    
<target name="hbm2ddl" depends="compile" description="Generate DB schema from the O/R mapping files">
        
<mkdir dir="${schema.dir}" />
        
<taskdef name="hbm2ddl" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="project.class.path" />
        
<hbm2ddl destdir="${schema.dir}">
            
<configuration configurationfile="${class.root}/hibernate.cfg.xml" />
            
<hbm2ddl export="true" console="false" create="true" update="false" drop="false" outputfilename="schema.sql" />
        
</hbm2ddl>
    
</target>


    
<!-- ================================= 
          target: compile              
         ================================= 
-->
    
<target name="compile" depends="hbm2java" description="Chapter3">
        
<javac srcdir="${source.root}" destdir="${class.root}" debug="on" optimize="off" deprecation="on">
            
<classpath refid="project.class.path" />
        
</javac>
    
</target>

    
<!-- ================================= 
          target: hbm2java              
         ================================= 
-->
    
<target name="hbm2java" depends="prepare" description="Generate Java source from the O/R mapping files">
        
<taskdef name="hbm2java" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="project.class.path" />
        
<hbm2java destdir="${source.root}">
            
<configuration configurationfile="${class.root}/hibernate.cfg.xml" />
            
<hbm2java jdk5="true" />
        
</hbm2java>
    
</target>


    
<!-- - - - - - - - - - - - - - - - - - 
          target: prepare                      
         - - - - - - - - - - - - - - - - - 
-->
    
<target name="prepare">
        
<delete dir="${class.root}" />
        
<mkdir dir="${class.root}" />

        
<copy todir="${class.root}">
            
<fileset dir="${source.root}">
                
<include name="**/*.properties" />
                
<include name="**/*.hbm.xml" />
                
<include name="**/*.cfg.xml" />
            
</fileset>
        
</copy>
    
</target>

</project>


 

转载于:https://www.cnblogs.com/wllyy189/archive/2008/12/12/1353853.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值