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.

4 Answers 4

59

<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>
Sign up to request clarification or add additional context in comments.

4 Comments

If you put a value in the property 'password', is automatically set and used?
Yes. hibernate will do automatically.
how can i set the character encoding when creating database?
Use jdbc:mysql://localhost:3306/footballdb?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
13
<property name="hibernate.hbm2ddl.auto">create</property>

will do

6 Comments

Thanks! when is the difference between this and <property name="hibernate.hbm2ddl.auto">update</property>?
create will create a new schema and update will look for the schema and do the update
I can use create instead of udpate , or should I use both?
just create is enough for the first time and then change it back to update for future
@yyny: this will not create database from my experience. If database is there, it will create tables. Thats only. Any other way?
|
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.

Comments

7

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

1 Comment

I really like this solution, but it does not work for me. I get com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.