34

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Billie
  • 8,938
  • 12
  • 37
  • 67

4 Answers4

57

<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>
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Aung Myat Hein
  • 4,018
  • 1
  • 36
  • 42
13
<property name="hibernate.hbm2ddl.auto">create</property>

will do

tesnik03
  • 1,324
  • 2
  • 13
  • 23
8

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
simon
  • 707
  • 8
  • 22
  • I really like this solution, but it does not work for me. I get `com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure` – codepleb Nov 11 '16 at 11:47
7

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.

Jugal Panchal
  • 1,448
  • 16
  • 20