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

准备:
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 >


 

你可能感兴趣的:(hibernate3.2由hbm文件生成pojo和ddl)