hibernate正向工程生成数据库

hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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">
 
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
 <session-factory>
  <!-- 指定连接数据库所用的驱动 -->
  <property
name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <!-- 指定连接数据库的url,hibernate连接的数据库名 -->
  <property
name="connection.url">jdbc:mysql://localhost:3306/wsnsp</property>
  <property
name="connection.useUnicode">true</property>
  <property
name="connection.characterEncoding">gbk</property>
  <!-- 指定连接数据库的用户名 -->
  <property
name="connection.username">root</property>
  <!-- 指定连接数据库的密码 -->
  <property
name="connection.password">1111</property>
   
  <!-- 指定数据库方言 -->
  <property
name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  <!-- 根据需要自动创建数据库 -->
  <property
name="hbm2ddl.auto">create</property>
  <!-- 显示Hibernate持久化操作所生成的SQL -->
        <property
name="show_sql">true</property>
  <!-- 将SQL脚本进行格式化后再输出-->
        <property
name="hibernate.format_sql">true</property>
  <!-- 罗列所有的映射文件-->
  <mapping
resource="ty/change/wsn/entity/Coordinator.hbm.xml"/>
  <mapping
resource="ty/change/wsn/entity/EndDevice.hbm.xml"/>
  <mapping
resource="ty/change/wsn/entity/Router.hbm.xml"/>
  <mapping
resource="ty/change/wsn/entity/User.hbm.xml"/>
  <mapping
resource="ty/change/wsn/entity/ZigBeeNode.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

CreateDB.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package
ty.change.wsn.action;
 
import
org.hibernate.cfg.Configuration;
import
org.hibernate.tool.hbm2ddl.SchemaExport;
 
public
class
CreateDB { 
    public
static void
main(String[] args) { 
        //装载配置文件 
        Configuration cfg =
new Configuration().configure(); 
        SchemaExport export =
new SchemaExport(cfg); 
        export.create(true,
true); 
    
}

运行CreateDB.java即可。

第一次运行程序时,将Hibernte中的hibernate.hbm2ddl.auto设置成create,让Hibernate帮助自动建表,但不成功,报了如下信息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1。

后来,网上查找一番,发现是因为type=InnoDB在5.0以前是可以使用的,但5.1之后就不行了。如果我们把type=InnoDB改为engine=InnoDB就不会有这个问题。但是,我想使用Hibernate,自动帮我建表,怎么办呢。这就与我们指定的数据库方言(dialect)有关了。

之前我的配置是:

1
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>

现在改为:

1
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>

好了,这样问题就解决了。总结下:

Using

'MySQL5InnoDBDialect'
 works with 
5.1
 and 
5.5
.

如果没有hbm.xml映射文件,而采用的是hibernate annotation方式,并且有hibernate配置文件cfg,只需要将hibernate.cfg.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<strong><?xml
version='1.0'
encoding='utf-8'?>
 
<!DOCTYPE
hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
 <hibernate-configuration>
     <session-factory>
         <!-- hibernate配置 -->
        <property
name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property
name="connection.url">jdbc:mysql://localhost:3306/db_shome</property>
        <property
name="connection.username">root</property>
        <property
name="connection.password">1111</property>
        <property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property
name="hibernate.connection.pool.size">20</property>       
        <property
name="show_sql">true</property>
        <property
name="hbm2ddl.auto">create</property>
        <property
name="current_session_context_class">thread</property>
  
        <mapping
class="com.java1234.model.User"/>
        <mapping
class="com.java1234.model.Grade"/>
        <mapping
class="com.java1234.model.Student"/>
         
    </session-factory>
</hibernate-configuration></strong>

注意:

1
<strong><property
name="hbm2ddl.auto">create</property></strong>

在数据库中新建对应的数据库名启动工程后查询数据库(比如登录验证用户名密码)就可以生成数据库表了

hibernate正向工程生成数据库的更多相关文章

  1. 懒要懒到底,能自动的就不要手动,Hibernate正向工程完成Oracle数据库到MySql数据库转换(含字段转换、注释)

    需求描述 需求是这样的:因为我们目前的一个老项目是Oracle数据库的,这个库呢,数据库是没有注释的,而且字段名和表名都是大写风格,比如 在代码层面的po呢,以前也是没有任何注释的,但是经过这些年,大 ...

  2. Hibernate正向工程(实体类-->数据库)

    1,新建实体类News.java package com.hanqi.dao; import java.util.Date; public class News { private Integer i ...

  3. eclipse下如何使用Hibernate反转工程生与数据库对应的实体类和映射文件(以MySQL为例)

    首先需要为eclipse添加对Hibernate的支持(也就是下载的Hibernate中的jar包),下载方法另查,这里不多做阐述. 想要使用反转工程,首先要下载Hibernate反转工程的插件Jbo ...

  4. hibernate+mysql 自动生成数据库问题

    Hibernate Entity类 表名注解大写时,在windows下mysql自动生成的表都为小写(不区分大小写),在linux下mysql自动生成区分大小写.导致数据库问题. 原因(window下 ...

  5. hibernate通过配置文件生成数据库信息

    hibernate可以通过配置文件在数据库生成相应的数据库信息.也可以把数据库的信息生成相应的代码(实体类操作类和映射文件) 下面是通过代码默认对hibernate.cfg.xml信息在数据库生成信息 ...

  6. powerDesigner 正向工程生成sql注释

    找到script-->objects-->column-->add value内容如下: %:COLUMN% %:DATATYPE%[.Z:[%Compressed%? compre ...

  7. hibernate笔记--通过SchemaExport生成数据库表

    方法比较简单,项目中只需要两个java类(一个实体类,如User,一个工具类),两个配置文件(hibernate必须的两个配置文件hibernate.cfg.xml,与User.hbm.xml),即可 ...

  8. 用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean)

    用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean) 安装: 在help中eclise marksplace中查询JBo ...

  9. Hibernate 由实体类与配置文件的配置关系生成数据库中的表

    import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ...

随机推荐

  1. 2018年Java面试题搜集

    2018年Java面试题搜集 一.Servlet执行流程(浏览器访问servlet的过程容器) 客户端发起http请求,web服务器将请求发送到servlet容器,servlet容器解析url并根据w ...

  2. Python中用format函数格式化字符串的用法(2.7版本讲解哦!)

    语法 它通过{}和:来代替%.“映射”示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.forma ...

  3. shell-最近7天目录

    #采用将最近7天的日期放入到数组中,遍历整个目录,将这7天的目录连接成一个字符串paths. #注意: .日期目录里面的文件只是做了简单的以part开头的匹配. # .path路径是日期的上一层,以/ ...

  4. Linux下多线程下载工具MWget和Axel使用介绍

    linux运维在操作linux过程中,用得最多的linux下载工具想必一定是wget,没有看到哪一台服务器没装过wget的,或许有人使用ftp下载,也有人使用多线程的axel以及ProZilla,毫无 ...

  5. 华丽的使用sublime写lua~ sublime lua相关必装插件推荐~~

    缘起 lua脚本语言虽好,代码写得飞快,可是写错了调试起来却很困难,lua使用者经常容易犯得一个错误是--写错变量名了,if end 嵌套太多没匹配~,多打了一个逗号, --假设定义了一个变量 loc ...

  6. 【转载】User notification 的实现方法

    原帖请看:http://cocoathings.blogspot.com/2013/01/introduction-to-user-notifications-in.html 想要实现如图这样的not ...

  7. DATASTAGE中ODBC连接的配置

    修改2个配置文件: cat /mistel/IBM/InformationServer/Server/DSEngine/.odbc.ini cat /mistel/IBM/InformationSer ...

  8. 一个Elasticsearch嵌套nested查询的实例

    创建索引和数据准备 PUT course PUT course/_mapping/course { "properties": { "course":{ &qu ...

  9. MFC clist 学习设计

    最近想设计一款WEBSHELL的综合破解工具. 然后设计到了日志输出那儿,因为MFC不熟悉,刚学.所以一直在想用edit控件好还是clist比较好. 今天设计了一下日志输出界面,然后记录一下学习笔记. ...

  10. 因磁盘爆满而导致NameNode HA无法启动

    场景回顾: 测试集群节点分配:35,36是namenode且开启HA,37,38,39即作为datanode,又作为journalnode. 某时间 38节点磁盘爆满,集群中hdfs及依赖的服务全部宕 ...