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 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 >

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


转载于:https://www.cnblogs.com/baobeiqi-e/p/9884828.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值