hibernate正向工程生成数据库
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
import
import
public CreateDB { public static void
//装载配置文件 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正向工程生成数据库的更多相关文章
- 懒要懒到底,能自动的就不要手动,Hibernate正向工程完成Oracle数据库到MySql数据库转换(含字段转换、注释)
需求描述 需求是这样的:因为我们目前的一个老项目是Oracle数据库的,这个库呢,数据库是没有注释的,而且字段名和表名都是大写风格,比如 在代码层面的po呢,以前也是没有任何注释的,但是经过这些年,大 ...
- Hibernate正向工程(实体类-->数据库)
1,新建实体类News.java package com.hanqi.dao; import java.util.Date; public class News { private Integer i ...
- eclipse下如何使用Hibernate反转工程生与数据库对应的实体类和映射文件(以MySQL为例)
首先需要为eclipse添加对Hibernate的支持(也就是下载的Hibernate中的jar包),下载方法另查,这里不多做阐述. 想要使用反转工程,首先要下载Hibernate反转工程的插件Jbo ...
- hibernate+mysql 自动生成数据库问题
Hibernate Entity类 表名注解大写时,在windows下mysql自动生成的表都为小写(不区分大小写),在linux下mysql自动生成区分大小写.导致数据库问题. 原因(window下 ...
- hibernate通过配置文件生成数据库信息
hibernate可以通过配置文件在数据库生成相应的数据库信息.也可以把数据库的信息生成相应的代码(实体类操作类和映射文件) 下面是通过代码默认对hibernate.cfg.xml信息在数据库生成信息 ...
- powerDesigner 正向工程生成sql注释
找到script-->objects-->column-->add value内容如下: %:COLUMN% %:DATATYPE%[.Z:[%Compressed%? compre ...
- hibernate笔记--通过SchemaExport生成数据库表
方法比较简单,项目中只需要两个java类(一个实体类,如User,一个工具类),两个配置文件(hibernate必须的两个配置文件hibernate.cfg.xml,与User.hbm.xml),即可 ...
- 用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean)
用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean) 安装: 在help中eclise marksplace中查询JBo ...
- Hibernate 由实体类与配置文件的配置关系生成数据库中的表
import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ...
随机推荐
- git-bash使用ctrl C无法终止nodemon的执行
原因: git的bug 解决:git版本降级为2.10.0好了
- ES6 随记(3.4.1)-- 函数的拓展(参数默认值,扩展运算符)
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...
- C语言单元测试框架--EmbedUnit
1.简介 Embedded Unit是个纯标准c构建的单元测试框架,主要用在嵌入式c的单体测试上,其主要特点是不依赖于任何C的标准库,所有的对象都是静态分配. 最早这个项目托管在SourceForge ...
- 一种BIM缺失多态性介导的酪氨酸激酶抑制剂的耐药性
论文名称:A common BIM deletion polymorphism mediates intrinsic resistance and inferior responses to tyro ...
- centos 6+安装山逗斯骚尅特
系统支持:CentOS 6+,Debian 7+,Ubuntu 12+ 内存要求:≥128M 关于本脚本 一键安装 Shadowsocks-Python, ShadowsocksR, Shadowso ...
- JavaWeb 自定义标签库开发传统标签
自定义标签主要用于移除Jsp页面中的java代码. 移除jsp页面中的java代码,只需要完成两个步骤: 编写一个实现Tag接口的Java类,并覆盖doStartTag方法,把jsp页面中的java代 ...
- Gentoo系统安装步骤详解
下载镜像 一般我都是用国内的镜像源,不管是centos,ubuntu还是gentoo在国内的镜像来说肯定比国外快 #下载地址mirrors.163.com/gentoo/#我用的x86的http:// ...
- PAT1076. Forwards on Weibo (30)
使用DFS出现超时,改成bfs DFS #include <iostream> #include <vector> #include <set> using nam ...
- selenium学习笔记(selenium下载安装)
博主自己捣鼓的接口框架先到这里 等工作上正式开始使用再后续完善需求 还是继续学习python.学编程就直接动手写 就想看看python+selenium的组合 什么都不多说.先下载安装 博主这里已经安 ...
- Java 基于JavaMail的邮件发送
http://blog.csdn.net/xietansheng/article/details/51673073 http://blog.csdn.net/xietansheng/article/d ...