Java Hibernate 自动创建数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21069687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:22:24  来源:igfitidea点击:

Hibernate auto create database

javamysqlhibernatejakarta-ee

提问by Billie

I have a Java EE Hibernate project, and I'm using MySQL as a database.

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

I want that when I first time run the project, it will create the database automatically.

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

This is my hibernate.cnf.xml:

这是我的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 the database InternetProjectto be created?

当我第一次在另一台计算机上运行这个项目时,如何InternetProject创建要创建的数据库?

According to the config file, it might already do it and I'm not aware to it.

根据配置文件,它可能已经这样做了,我不知道。

Thanks in advance.

提前致谢。

回答by tesnik03

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

will do

会做

回答by Jugal Panchal

There are multiple option for auto property.

自动属性有多种选择。

  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>
    
  1. create - 它创建新表对应的映射或注释。它删除现有的表和数据。
  2. 更新 - 保留现有数据和表格。它更新架构。在这里,我们必须照顾contants。
  3. create-drop - 它与 create 相同,但是一旦会话关闭,它就会删除所有内容。
  4. 验证 - 它验证或匹配模式与地图或注释。它适用于生产环境。

    <!-- create create-drop validate update -->
    <property name="hbm2ddl.auto">update</property>
    

I hope it helps.

我希望它有帮助。

回答by Aung Myat Hein

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

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

Update : character encoding when creating database

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

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

回答by simon

Hibernate will not create the database for you, only the tables. To create the database you need to tell MySQL to create it if it does'nt already exist by adding a parameter to the URL. E.g.:

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

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