Hibernate自动创建数据库

34

我有一个Java EE Hibernate项目,使用MySQL作为数据库。

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

这是我的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>

当我第一次在另一台电脑上运行这个项目时,我应该如何使数据库 InternetProject 被创建?

根据配置文件,它可能已经完成了,但我不确定。

先感谢您的帮助。

4个回答

57

<property name="hibernate.hbm2ddl.auto">create</property> 将会创建表。但它不会创建数据库。要生成数据库,请更改连接URL。

<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>
更新:创建数据库时的字符编码。
<property name="connection.url">
    jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
</property>

如果您在属性“密码”中设置了一个值,它会自动设置并使用吗? - Pascal Schneider
1
是的,Hibernate会自动完成。 - Aung Myat Hein
我在创建数据库时如何设置字符编码? - Azarea
1
使用jdbc:mysql://localhost:3306/footballdb?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8 - Aung Myat Hein

13
<property name="hibernate.hbm2ddl.auto">create</property>

会做


1
谢谢!这个和<property name="hibernate.hbm2ddl.auto">update</property>的区别是什么? - Billie
2
创建将创建一个新模式,更新将查找模式并执行更新。 - tesnik03
1
我可以使用 create 代替 update,还是应该两者都用? - Billie
1
第一次只需要创建,以后更新时再改回更新即可。 - tesnik03
11
根据我的经验,这不会创建数据库。如果数据库已存在,则会创建表。就只有这样了。还有其他方法吗? - skmaran.nr.iras
这个 XML 语法不符合验证。cvc-complex-type.2.1: 元素 'property' 必须没有字符或元素信息项 [children],因为该类型的内容类型为空。 - Savrige

8

Hibernate不会为您创建数据库,只会创建表。要创建数据库,您需要告诉MySQL在URL中添加一个参数来创建它(如果不存在)。例如:

jdbc:mysql://db:3306/mydatabase?createDatabaseIfNotExist=true

我真的很喜欢这个解决方案,但它对我不起作用。我得到了“com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure”的错误信息。 - codepleb

7

自动属性有多种选项。

  1. create - It creates new tables corresponding mapping or annotation. It drops existing tables and data.
  2. update - It keeps existing data and tables. It updates schema. here we have to take care contrants.
  3. create-drop - It is same like create but once session gets closed it drops everything.
  4. validate - it validates or matches schema with map or annotation. It's valid for Production environment.

    <!-- create create-drop validate update -->
    <property name="hbm2ddl.auto">update</property>
    
我希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接