Hibernate自动创建数据库 [英] Hibernate auto create database

查看:141
本文介绍了Hibernate自动创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaEE hibernate项目,

我使用MySQL作为数据库。



I希望当我第一次运行项目时,它会自动创建数据库。



这是我的 hibernate.cnf.xml $ b

 <?xml version ='1.0'encoding ='utf-8'?> $ c>:



<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate -configuration-3.0.dtd>

< hibernate-configuration>
< session-factory>
< property name =connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =connection.url> jdbc:mysql:// localhost / InternetProject< / property>
< property name =connection.username> root< / property>
< property name =connection.password>< / property>
< property name =connection.pool_size> 10< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =hibernate.hbm2ddl.auto>更新< / property>
< property name =show_sql> true< / property>
< mapping class =entities.Business/>
< mapping class =entities.Coupon/>
< mapping class =entities.User/>
< mapping class =entities.LastLogin/>
< / session-factory>
< / hibernate-configuration>

当我第一次在另一台计算机上运行此项目时,如何使数据库 InternetProject 被创建?



根据配置文件,它可能已经做到了,我不知道它..
在此先感谢:)

解决方案


<属性name =hibernate.hbm2ddl.auto>创建< / property>
将创建表格。但它不会创建数据库。更改连接url以生成数据库。

 < property name =connection.driver_class> com.mysql.jdbc .Driver< /性> 
< property name =connection.url> jdbc:mysql:// localhost / InternetProject?createDatabaseIfNotExist = true< / property>
< property name =connection.username> root< / property>
< property name =connection.password>< / property>
< property name =connection.pool_size> 10< / property>


更新:创建数据库时的字符编码

 < property name =connection.url> 
jdbc:mysql:// localhost / InternetProject?createDatabaseIfNotExist = true& useUnicode = yes& characterEncoding = UTF-8
< / property>


I have a JavaEE hibernate project,

And I'm using MySQL as Database.

I want that when I first time runs the project, it will create the database automaticlly.

This is my hibernate.cnf.xml:

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

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/InternetProject</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="connection.pool_size">10</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <mapping class="entities.Business" />
        <mapping class="entities.Coupon" />
        <mapping class="entities.User" />
        <mapping class="entities.LastLogin" />
    </session-factory>  
</hibernate-configuration>

When I first-time run this project on another computer, how can I make database InternetProject be created?

According to the config file, it might already do it and I'm not aware to it .. Thanks in advance, :)

解决方案

<property name="hibernate.hbm2ddl.auto">create</property> will create tables. But it will not create database. Change the connection url to generate the database.

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <property name="connection.pool_size">10</property>

Update : character encoding when creating database

<property name="connection.url">
jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
</property>

这篇关于Hibernate自动创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆