Hibernate's 'hbm2ddl.auto' property with value 'create' is not re-creating table

27.3k views Asked by At

I'm working on my first simple Hibernate application. The crux of the problem is, I had renamed a member of persistent class (with apt changes to all other parts) and re-run the application. As 'hbm2ddl.auto' property is assigned 'create' value in configuration xml, Hibernate is expected to create new table on every run but, it's not doing so. Following is detailed information:

CLASS:

    public class Event {
    private Long id;

    private String title;
    private Date date;

    public Event() {
        // this form used by Hibernate
    }

    public Event(String title, Date date) {
        // for application use, to create new events
        this.title = title;
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Event.hbm.xml:

<hibernate-mapping package="org.hibernate.tutorial.hbm">

<class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
        <generator class="increment"/>
    </id>
    <property name="date" type="timestamp" column="EVENT_DATE"/>
    <property name="title"/>
</class>

hibernate.cfg.xml, among others, has following entry:

<property name="hbm2ddl.auto">create</property>

MAIN CLASS:

public class EventManager {

public static void main(String[] args) {

    EventManager mgr = new EventManager();

    List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
    Event theEvent = (Event) events.get(i);
        System.out.println( "Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate() );
    }

    HibernateUtil.getSessionFactory().close();

}

private List listEvents() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List result = session.createQuery("from Event").list();
    session.getTransaction().commit();
    return result;
} }

The above code is working as expected. Just to test 'hbm2ddl.auto' property, I had changed 'title' member in Event class to 'title1'. And, updated setter and getter methods and all references (in mapping xml, Event and EventManager class). Complied with no errors. But, when I try to run the application, I see following exception:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: Unknown column 'event0_.title1' in 'field list' at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) at $Proxy6.executeQuery(Unknown Source) at org.hibernate.loader.Loader.getResultSet(Loader.java:1953) at org.hibernate.loader.Loader.doQuery(Loader.java:829) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) at org.hibernate.loader.Loader.doList(Loader.java:2438) at org.hibernate.loader.Loader.doList(Loader.java:2424) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2254) at org.hibernate.loader.Loader.list(Loader.java:2249) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355) at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1248) at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) at org.hibernate.tutorial.hbm.EventManager.listEvents(EventManager.java:56) at org.hibernate.tutorial.hbm.EventManager.main(EventManager.java:20) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'event0_.title1' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.Util.getInstance(Util.java:384) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2264) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122) ... 16 more

The error is pointing to EventManager class at following line:

List result = session.createQuery("from Event").list();

As 'hbm2ddl.auto' property is assigned 'create' value in configuration xml, Hibernate is expected to create new table on every run but, it's not doing so. Please help resolve the problem.

Thanks in advance.

2

There are 2 answers

5
Alex Barnes On

Try specifying hibernate.hbm2ddl.auto=create instead of just hbm2ddl.auto=create.

This is what the documentation uses.

0
Sachchidanand Singh On

Try <property name="hibernate.hbm2ddl.auto">create</property> instead of <property name="hbm2ddl.auto">create</property>