hbm2ddl工具位于Hibernate核心软件包中,而hbm2java工具位于Hibernate工具包中,因此需要下载Hibernate工具包,文件形式为HibernateTools-X.zip。

Hibernate提供了从映射文件到数据库Schema的转换工具,名为hbm2ddl工具。使hbm2ddl工具时,必须在Hibernate的配置文件中设置hibernate。dialect属性,显式指定底层数据库的SQL方言。 
<target name="schema" depends="compile"> 
  <taskdef name="hbm2ddlTask" 
           classname="org.hibernate.tool.ant.HibernateToolTask" 
           classpathref="project.class.path" /> 
  <hbm2ddlTask destdir="${schema.dir}"> 
       <configuration 
            configurationfile="${class.root}/hibernate.cfg.xml"  /> 
  <hbm2ddl export="true"  console="true"  create="true" 
           drop="true"  outputfilename="dbName.sql"  /> 
  </hbm2ddlTask> 
</target> 
hbm2ddl任务的属性 
export:          若为true,表示在数据库中执行所生产的ddl脚本。默认为false。 
drop:            若为true,表示会生成删除数据库中表的ddl脚本。默认为false。 
create:          若为true,表示会生成创建数据库中表的ddl脚本。默认为false。 
outputfilename: 指定存放ddl脚本的文件。 
update:          若为true,表示会对比现有数据库及映射文件,生成用于增量更新数据库的ddl。默认为false。 
haltonerror:    若为true,表示遇到错误时会终止运行ant工程。默认为false。 
format:          设定ddl脚本中SQL语句的格式。 
delimiter:       为ddl脚本设置行结束符。 
console:         若为true,表示会在控制台显示生成的ddl脚本。默认为true。

使用xml配置hibernate。通常命名为hibernate.cfg.xml 
<hibernate-configuration> 
  <session-factory> 
     <property name="dialect"> 
        org.hibernate.dialect.MySQLDialect</property>

<property name="connection.driver_class"> 
         com.mysql.jdbc.Driver</property>

<property name="connection.url"> 
         jdbc:mysql://localhost:3306/dbName</property> 
     
     <property name="connection.username">root</property> 
     <property name="connection.password">tiger</property> 
     <property name="show_sql">true</property>

<mapping resource="xxx.hbm.xml"  />

</session-factory> 
</hibernate-configuration>

若Hibernate的配置文件为java属性文件,必须以编程方式声明需要加载的映射文件,xml则不必调用。 
    SessionFactory sessionFactory = new Configuration() 
          .addClass(xx.class).buildSessionFactory(); 
默认情况下,Hibernate不会加载hibernate.cfg.xml文件,必须通过Configuration的configure()方法来显式加载hibernate.cfg.xml文件。  
    SessionFactroy sessionFactory = new Configuration() 
          .configure().buildSessionFactory();

持久化类使用JavaBean的风格,为需要被访问的属性提供get和set方法,这两个方法也称为持久化类的访问方法。

为什么hibernate中持久化对象要有set和get方法? 
java应用程序调用对象的get方法读取外界信息后调用set方法,把信息存到对象中;Hibernate调用对象的get方法读取对象信息后存到数据库调用set方法把数据从数据库中独取出来后存到对象中。Hibernate可以访问任何级别的get、set方法。

基本数据类型的缺点在与无法表达null值;包装类型的默认值是null。 
在对象-关系映射文件中,<property>元素的access属性用于指定Hibernate访问持久化类的属性方式。access有一下两个值可选:property:默认值,表明hibernate通过相应的get和set方法来访问类的属性。field属性表明Hibernate运用java反射机制直接访问类的属性,不需get和set方法。

在SQL语法中,标识符是指用于为数据库表、视图、字段或索引等明名的字符串,常规标识符不包含空格,也不包含特殊字符;若数据库表名或字段名中包含空格,或包含特殊字符,则可以使用引用标识符。'xxx xx'

创建数据库的命名策略:实现Hibernate的org.hibernate.cfg.ImprovedNamingStragegy接口。它提供了两个参考实现类:org.hibernate.cfg.DefaultNamingStragegy类(默认实现类);org.hibernate.cfg.ImproveNamingStragegy类(高级实现类)。

hbm2ddl的更多相关文章

  1. hibernate.hbm2ddl.auto配置详解

    hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下:<properties><property name="hibernate. ...

  2. Hibernate hbm2ddl.auto DDL语句 控制台输出的配置

    在开发中我们需要知道hbm2ddl.auto生成的SQL语句,来判断代码的正确性,现在记录配置的过程. Hibernate的DDL语句在控制台的输出配置: 一.在lib中确保只有下面的三个相关包:1) ...

  3. Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】

    原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...

  4. hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据

    参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...

  5. hibernate hbm2ddl auto 不能创建表的问题

    http://fuckgis.blog.sohu.com/148751122.html ________________________________________________________ ...

  6. hibernate篇章四-- Hibernate配置文件中hiberante.hbm2ddl.auto四个参数的配置

    我们在搭建环境的时候,在配置文件中有一个属性标签为: <property name="hibernate.hbm2ddl.auto">     </propert ...

  7. Hibernate配置文件中hiberante.hbm2ddl.auto四个参数的配置

    我们在搭建环境的时候,在配置文件中有一个属性标签为: <property name="hibernate.hbm2ddl.auto">     </propert ...

  8. Hibernate配置文件的hbm2ddl.auto属性

    今天遇到一个有意思的问题,我目前做的一个网站采用Spring MVC + Spring + Hibernate的架构,我通过页面插入了一些数据到数据库,可是每次重启tomcat之后,数据都莫名其妙地丢 ...

  9. (转)hibernate 之hiberante.hbm2ddl.auto 参数的配置

    我们在搭建环境的时候,在配置文件中有一个属性标签为: 完整配置如下 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernat ...

随机推荐

  1. 51nod 1021 石子归并(dp)

    51nod 1021 石子归并 题解:从i到j合并的最小值:dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]); 最 ...

  2. 修改Linux系统时区

    修改配置文件来修改时区1.修改/etc/sysconfig/clock         ZONE=Asia/Shanghai 2.rm /etc/localtime3.链接到上海时区文件 ln -sf ...

  3. 分支语句switch case

    Switch case必须与break一起使用 Break 是跳转语句.与switch case连用的时候是跳出最近的{}. static void Main(string[]args ) { //s ...

  4. OpenTSDB介绍——基于Hbase的分布式的,可伸缩的时间序列数据库,而Hbase本质是列存储

    原文链接:http://www.jianshu.com/p/0bafd0168647 OpenTSDB介绍 1.1.OpenTSDB是什么?主要用途是什么? 官方文档这样描述:OpenTSDB is ...

  5. C#WPF做FTP上传下载获取文件列表

    Xaml.cs: using Microsoft.Win32;using System;using System.Collections.Generic;using System.IO;using S ...

  6. [转]Java FileInputStream与FileReader的区别

    在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...

  7. Ubuntu 设置su密码

    如果之前安装时没有设置root密码,可以如下设置: 命令窗口中输入:sudo passwd [sudo] password for 用户名:  这里输入你sudo 的密码输入新的 UNIX 密码: 重 ...

  8. Git 提交大文件提示 fatal: The remote end hung up unexpectedly

    使用gitlab搭建的git server,如果直接使用http的方式去提交的话,提交小文件不会有问题,但是提交大文件时,会出错: fatal: The remote end hung up unex ...

  9. Could not find artifact com.sun:tools:jar:1.5.0

    问题: Failed to execute goal on project petroleum: Could not resolve dependencies for project petroleu ...

  10. ASP.NET的运行原理与运行机制

    在Asp.net4和4.5中,新增了WebPages Framework,编写页面代码使用了新的Razor语法,代码更加的简洁和符合Web标准,编写方式更接近于PHP和以前的Asp,和使用WebFor ...