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.
<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>
There are multiple option for auto property.
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.
<property name="hibernate.hbm2ddl.auto">create</property>
will do
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