Hibernate auto create database

假装没事ソ 提交于 2019-12-17 15:54:30

问题


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

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

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 the database InternetProject to be created?

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

Thanks in advance.


回答1:


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



回答2:


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

will do




回答3:


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>
    

I hope it helps.




回答4:


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

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


来源:https://stackoverflow.com/questions/21069687/hibernate-auto-create-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!