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.
<property name="hibernate. hbm2ddl. auto">create</property> will create tables. But it will not create database.
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.
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.
<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>
<property name="hibernate.hbm2ddl.auto">create</property>
will do
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With