Hrushikesh Salunkhe
Hrushikesh Salunkhe

Reputation: 81

hibernate.hbm2ddl.auto property creates table with create but not with update

The database table is NOT auto-created by the <property name="hbm2ddl.auto">update</property> settings.

It works for <property name="hbm2ddl.auto">create</property>

Configuration:

<?xml version="1.0" encoding="UTF-8"?>

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
        value="com.mysql.cj.jdbc.Driver"></property>
    <property name="url"
        value="jdbc:mysql://localhost:3306/StrutsPractice"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

<bean id="mysessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>

    <property name="mappingResources">
        <list>
            <value>com/hbm/person.hbm.xml</value>
        </list>
    </property>


    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="d" class="com.DAO.PersonDAO">
    <property name="sf" ref="mysessionFactory"></property>
</bean>

If table does not exist in the database then it should be created automatically.

Upvotes: 0

Views: 276

Answers (1)

Ravi
Ravi

Reputation: 332

Possible values for hbm2ddl.auto and their description is as follows:

create - create a schema

update - update existing schema

validate - validate existing schema

create-drop - create and drop the schema automatically when a session is starts and ends

Upvotes: 1

Related Questions