-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Could not fetch initial value for increment generator
PostPosted: Tue Feb 16, 2010 8:34 am 
Newbie

Joined: Tue Feb 16, 2010 8:11 am
Posts: 4
SEVERE: Table 'test.MESSAGES' doesn't exist -> Could not fetch initial value for increment generator

Hi,

I'm working on a very simple 'HelloWorld' program. I have a mySQL db running on localhost. Below I printed my HelloWorld.java, Message.hbm.xml and the log/error messages. I'm stuck on this problem for a couple of days now so I hope somebody can get me in the good direction or fix the problem. If you need any more information please let me know. Thank you all VERY MUCH in advance!

----------HelloWorld.java----------
Code:
package hello;
import java.util.*;
import org.hibernate.*;
import persistence.*;
public class HelloWorld {
    public static void main(String[] args) {
        // First unit of work
        Session session =
            HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        Message message = new Message("Hello World");
        Long msgId = (Long) session.save(message); //<-- Here is where the error appears
        tx.commit();
        session.close();
        // Second unit of work
        //...
   }
   //...
}
}

----------Message.hbm.xml----------
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping  package="hello">
    <class
        name="Message"
        table="MESSAGES">
        <id
            name="id"
            column="MESSAGE_ID">
            <generator class="increment"/>
        </id>
        <property
            name="text"
            column="MESSAGE_TEXT"/>
        <many-to-one
            name="nextMessage"
            cascade="all"
            column="NEXT_MESSAGE_ID"
            foreign-key="FK_NEXT_MESSAGE"/>
    </class>
</hibernate-mapping>


After Hibernate sended the following query:

Hibernate:
select
max(MESSAGE_ID)
from
MESSAGES

I get the following log messages and error's:

Feb 16, 2010 12:25:12 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1146, SQLState: 42S02
Feb 16, 2010 12:25:12 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Table 'test.MESSAGES' doesn't exist
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:131)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:68)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at hello.HelloWorld.main(HelloWorld.java:12)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'test.MESSAGES' doesn't exist
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:109)
... 11 more


Top
 Profile  
 
 Post subject: Re: Could not fetch initial value for increment generator
PostPosted: Tue Feb 16, 2010 8:57 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Table 'test.MESSAGES' doesn't exist


This message means, that on the configured database there'n no table named test.MESSAGES.
Did you forget to generate the schema on the database?


Top
 Profile  
 
 Post subject: Re: Could not fetch initial value for increment generator
PostPosted: Tue Feb 16, 2010 9:08 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
seems like a schema issue.
Because the error message says test.MESSAGES doesn't exist. But in the query issued, I don't see the "test.Messages"

try with the following

<class name="Message" table="MESSAGES" schema="test">
....
<generator class="increment">
<param name="schema">test</param>
</generator>
...


Top
 Profile  
 
 Post subject: Re: Could not fetch initial value for increment generator
PostPosted: Tue Feb 16, 2010 10:20 am 
Newbie

Joined: Tue Feb 16, 2010 8:11 am
Posts: 4
Thanks or your quick replay guys!

@pb00067: My db is called 'test', it contains nothing, no tables. I thought Hibernate created all the needed tables (and schema's) by itself, right?

@kavithakaran: I changed my hbm.xml, the result is only a small change in the query, it now looks like:
Hibernate:
select
max(MESSAGE_ID)
from
test.MESSAGES

unfortunately the error is still the same.


Top
 Profile  
 
 Post subject: Re: Could not fetch initial value for increment generator
PostPosted: Tue Feb 16, 2010 10:31 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
<property name="hbm2ddl.auto">update</property>

If you have the above settings in hibernate.cfg.xml, Then only Hibernate exports schema DDL to the database when the SessionFactory is created.

other values for this property are create ,create-drop and validate


Top
 Profile  
 
 Post subject: Re: Could not fetch initial value for increment generator
PostPosted: Tue Feb 16, 2010 10:42 am 
Newbie

Joined: Tue Feb 16, 2010 8:11 am
Posts: 4
That line was missing in my hibernate.cfg.xml. Thank you very much :)!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.