0

My hibernate.cfg.xml is :

<?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="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateSimpleDB</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>

        <property name="transaction.factory_class">org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory</property>

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

        <property name="hibernate.cache.use_second_level_cache">true</property>

        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

        <property name="current_session_context_class">thread</property>

        <mapping resource="com/masterhibernate/SimpleHibernateDemo/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

I expect hibernate to create database hibernateSimpleDB automatically. But it doesn't do that.

I know that it will create tables automatically but I want to create database and tables automatically.

Is it possible?

3
  • Add with "database" you mean schema? Commented Jun 10, 2014 at 7:46
  • Yes I mean schema by word database Commented Jun 10, 2014 at 7:50
  • I don't know whether this is possible. Because then Hibernate would also have to create the database user including password etc. Commented Jun 10, 2014 at 8:11

1 Answer 1

1

Check this out http://mojo.codehaus.org/hibernate3-maven-plugin/hbm2ddl-mojo.html

And to create tables.

Check this Hibernate properties.

             <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

This is the line that make the magic:

                        <prop key="hibernate.hbm2ddl.auto">create</prop>
Sign up to request clarification or add additional context in comments.

4 Comments

Yes that generates the tables and related meta data; the OP is asking about the -database- those tables will be created in.
Ok but the question here would be, how many times the guy expect to create the schema?. I never have to worry about to let Hibernate to create the schema because is something that I do just one time per machine.
Still, it does not answer his question does it?
"but I want to create database and tables automatically. Is it possible?" I told him how to create tables.

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.