Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate auto create database

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.

like image 864
Billie Avatar asked Jan 12 '14 00:01

Billie


People also ask

Can Hibernate create the database automatically?

<property name="hibernate. hbm2ddl. auto">create</property> will create tables. But it will not create database.

Which property is used for auto creation of tables in Hibernate?

A developer can have Hibernate create tables by adding the hbm2ddl. auto property and setting it to one of the following four values: validate. update.

What does Hibernate hbm2ddl auto create-drop this means?

hibernate.hbm2ddl.auto. Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop , the database schema will be dropped when the SessionFactory is closed explicitly.


2 Answers

<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> 
like image 96
Aung Myat Hein Avatar answered Sep 20 '22 09:09

Aung Myat Hein


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

will do

like image 35
tesnik03 Avatar answered Sep 22 '22 09:09

tesnik03