Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HibernateJpaVendorAdapter's generateDdl doesn't alter tables

I am developing a website using Spring+JPA+Hibernate. In the persistence configuration (JPA+Hibernate) I'm setting the HibernateJpaVendorAdapter's generateDdl attribute to true and in fact new entities correctly create the new table in the DB.

Once the table has been created, though, if I add an attribute to the entity, I'm expecting the HibernateJpaVendorAdapter to alter the table and add the column as well. This is not happening and it's strange because in the Java AbstractJpaVendorAdapter's setGenerateDdl method documentation there is: "Set whether to generate DDL after the EntityManagerFactory has been initialized, creating/updating all relevant tables."

JPA+Hibernate configuration:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driverClassName}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    <property name="showSql" value="${db.showSql}" />
    <property name="generateDdl" value="${db.generateDdl}" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter" ref="jpaAdapter" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
    <property name="dataSource" ref="dataSource" />
</bean>

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
        xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
   http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">

    <persistence-unit name="m8tsPU" />
</persistence>

Am I doing something wrong?

like image 271
satoshi Avatar asked Jan 23 '12 17:01

satoshi


2 Answers

You can get a more fine-grained control over DDL generation by setting an appropriate Hibernate-specific property directly:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     ...
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            ...
        </props>
    </property>
</bean>

Alternatively, you can use create-drop instead of update, since update may cause problems in some cases. Note, however, that automatic DDL update is not intended for production use.

like image 174
axtavt Avatar answered Oct 17 '22 18:10

axtavt


Alternatively, you can also express the same properties using jpaPropertyMap:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     ...
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.hbm2ddl.auto" value="update"/>
                ...
            </map>
        </property>
</bean>
like image 27
Robert Hunt Avatar answered Oct 17 '22 18:10

Robert Hunt