4

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.

user1418717
  • 425
  • 2
  • 5
  • 13

2 Answers2

13

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

This is what the documentation uses.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • Alex, 1/2: Thanks much for the response which helped track problem. I tried with your suggestion but, I saw same exception. This time I had a close look of whole logging and I noticed below lines: ERROR: HHH000389: Unsuccessful: drop table if exists EVENTS ERROR: Cannot delete or update a parent row: a foreign key constraint fails – user1418717 May 28 '12 at 06:00
  • Alex, 2/2: So, the last error statement clearly points out the problem which I had overlooked, earlier. As I was working with examples, I had created PERSON table which had many2many relationship with EVENTS table, which was successful. Then, I tried to change 'title' member by doing as mentioned in OP and commenting mapping entry in config file for PERSON table. Hibernate reported the error but, I had overlooked. As I'm a new member, I can't up vote, otherwise, I would have surely done. Anyhow, Thanks again. – user1418717 May 28 '12 at 06:00
  • 2
    But if you use create, then you can lose data because it may drop current data. Update should work. – María Arias de Reyna Domínguez Nov 08 '13 at 12:07
  • Awesome little tip. Just saved me a huge headache. +1 – christopher Oct 07 '14 at 13:19
  • 1
    To avoid other same kind errors, use `org.hibernate.cfg.AvailableSettings` and his static attributes – deldev May 12 '16 at 14:16
1

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

Sachchidanand Singh
  • 1,456
  • 1
  • 15
  • 15