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
|
<?xmlversion='1.0'encoding='UTF-8'?><!DOCTYPEhibernate-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> <!-- 指定连接数据库所用的驱动 --> <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 指定连接数据库的url,hibernate连接的数据库名 --> <propertyname="connection.url">jdbc:mysql://localhost:3306/wsnsp</property> <propertyname="connection.useUnicode">true</property> <propertyname="connection.characterEncoding">gbk</property> <!-- 指定连接数据库的用户名 --> <propertyname="connection.username">root</property> <!-- 指定连接数据库的密码 --> <propertyname="connection.password">1111</property> <!-- 指定数据库方言 --> <propertyname="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 根据需要自动创建数据库 --> <propertyname="hbm2ddl.auto">create</property> <!-- 显示Hibernate持久化操作所生成的SQL --> <propertyname="show_sql">true</property> <!-- 将SQL脚本进行格式化后再输出--> <propertyname="hibernate.format_sql">true</property> <!-- 罗列所有的映射文件--> <mappingresource="ty/change/wsn/entity/Coordinator.hbm.xml"/> <mappingresource="ty/change/wsn/entity/EndDevice.hbm.xml"/> <mappingresource="ty/change/wsn/entity/Router.hbm.xml"/> <mappingresource="ty/change/wsn/entity/User.hbm.xml"/> <mappingresource="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 { publicstatic 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
|
<propkey="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> |
现在改为:
|
1
|
<propkey="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><?xmlversion='1.0'encoding='utf-8'?><!DOCTYPEhibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- hibernate配置 --> <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> <propertyname="connection.url">jdbc:mysql://localhost:3306/db_shome</property> <propertyname="connection.username">root</property> <propertyname="connection.password">1111</property> <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property> <propertyname="hibernate.connection.pool.size">20</property> <propertyname="show_sql">true</property> <propertyname="hbm2ddl.auto">create</property> <propertyname="current_session_context_class">thread</property> <mappingclass="com.java1234.model.User"/> <mappingclass="com.java1234.model.Grade"/> <mappingclass="com.java1234.model.Student"/> </session-factory></hibernate-configuration></strong> |
注意:
|
1
|
<strong><propertyname="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 ...
随机推荐
- jmeter之报告和分析
转载:http://www.cnblogs.com/miaomiaokaixin/p/6118081.html jmeter -n -t 脚本名字.jmx -l xxx.jtl -e -o 指定目录( ...
- Spark机器学习5·回归模型(pyspark)
分类模型的预测目标是:类别编号 回归模型的预测目标是:实数变量 回归模型种类 线性模型 最小二乘回归模型 应用L2正则化时--岭回归(ridge regression) 应用L1正则化时--LASSO ...
- Mybatis之基础应用小结以及IntelliJ IDEA目录结构的一些小问题
IntelliJ IDEA 目录结构的一些小问题 [Mybatis 之基础应用小结] 1.不管怎么样,先建立一个简单的MySQL数据表,如下所示 2.接下来要做的事情就是通过Mybatis对数据表进行 ...
- 响应式Tab选项卡
在线演示 本地下载
- mysql用户与权限管理笔记
今天想使用一下李刚那本书上的hibernate的Demo,试出了点问题,过程中就发现mysql的用户管理和权限管理上也有点东西要注意,所以顺便就写一下mysql用户管理和权限管理的笔记. 先说一说my ...
- 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
第二次碰到这个问题了,记录一下 解决方案:1. 运行->cmd 2. cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 3. aspnet_ ...
- TrappingRainWater
问题描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
- Spring获取bean的几种方式
工作中需要对一个原本加载属性文件的工具类修改成对数据库的操作当然,ado层已经写好,但是需要从Spring中获取bean,然而,工具类并没有交给Spring来管理,所以需要通过方法获取所需要的bean ...
- Netty原理
1. Netty简介Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都 ...
- java项目生成jar,并在cmd中执行jar
自己写的jar并使用:============Java项目============Jar包的打包在Eclipse中直接打包,具体步骤: 点击右键>export>jar file>取消 ...