-->
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.  [ 14 posts ] 
Author Message
 Post subject: Tutorial problem (Chapter 2)
PostPosted: Tue Jul 05, 2005 5:05 am 
Newbie

Joined: Tue Jul 05, 2005 4:25 am
Posts: 3
I'm having a problem with the tutorial in chapter 2 of the reference documentation. I've followed the tutorial step by step and it seems to do what it is supposed to until I get to the end of section 2.2.6 where it tells me to remove the hbm2ddl.auto option and run the store action a few times. When I do this it blows up and says:

[java] java.sql.SQLException: Table not found in statement [select max(EVENT_ID) from EVENTS]

I've double checked that I followed the tutorial correctly. I've looked at the FAQ's and done a search on here without finding anything. If anyone has any pointers as to what needs to be done to get past this, it would be much appreciated.

/Skrap


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 5:15 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, think about it for a second. If you disable hbm2ddl, no tables are created. Of course you only do that after enabling it at least for _one_ run, but then disable it for all the following runs. Makes sense, right? :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 6:34 am 
Newbie

Joined: Tue Jul 05, 2005 4:25 am
Posts: 3
Yes, and per the tutorial I did the store action with it enabled at first, and then the list action with it still enabled which showed no records (not surprisingly). Next the tutorial says to disable the option and run a few stores again to put records into the table, so I do and get the error.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 6:56 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why don't you just skip that part until you are more experienced...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 7:26 am 
Newbie

Joined: Tue Jul 05, 2005 4:25 am
Posts: 3
Sorry, I massively overrated my abilities. I realize now I shouldn't even have tried. :/


Top
 Profile  
 
 Post subject: I'm having the same problem
PostPosted: Wed Sep 21, 2005 7:43 pm 
Newbie

Joined: Wed Sep 21, 2005 7:28 pm
Posts: 3
Location: San Francisco
I'm having the same problem as the person who initiated this thread.

I'm new to Hibernate and Ant and I'm trying to run the tutorial.

As long as I leave the hbm2ddl.auto statement in the hibernate.cfg.xml, both the -Daction=store and -Daction=list commands run without errors.

I've tried
1) ant run -Daction=store (runs without error, last message from ant is [java] Hibernate: insert into EVENTS ... )
2) editing the hibernate.cfg.xml and deleting the line:
<property name="hbm2ddl.auto">create</property>
3) ant run -Daction=store

this time I get an unhandled exception for which the stack trace starts with:
Code:
     [java] 16:34:55,100  WARN JDBCExceptionReporter:71 - SQL Error: -22, SQLState: S0002
     [java] 16:34:55,103 ERROR JDBCExceptionReporter:72 - Table not found in statement [select max(EVENT_ID) from EVENTS]
     [java] Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value


So, as far as I can tell, each time I run the tutorial, the in memory database starts up totally empty. It isn't persisting anything between runs of the program. As a result, if I don't have the hdm2dll.auto statement in the cfg.xml file, there is no table to store into or list from so the example breaks. If I do have the hdb2dll.auto statement present, then the EVENTS table is created and the code works, but never displays anything.

So my question is:
Am I doing something wrong, or is the tutorial incapable of operating as it describes:

Quote:
The reason for this is the hbm2ddl.auto switch in the Hibernate configuration: Hibernate will re-create the database on every run. Disable it by removing the option, and you will see results in your list after you called the store action a few times. Automatic schema generation and export is mostly useful in unit testing.


Thanks for your help,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 4:48 am 
Newbie

Joined: Thu Sep 22, 2005 4:32 am
Posts: 7
Location: Italy
I'm in the same situation as shani and Skrap Post, and can confirm the behaviour.

I have however found another interesting bit: (at least with HSQL 1.8) by default HSQL create table in memory only, so that they are not written to disk. This may explain the problem, because the Hibernate generated DDL does not specify the table storage class.

However, altering test.properties (the HSQL db properties file) to have
Code:
hsqldb.default_table_type=cached
led me to the same "Table not found" errors that the others found.

I'm beginning to think that some changed default in HSQL 1.8 breaked the tutorial...

Thank you in advance,

Michele


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 5:35 am 
Newbie

Joined: Thu Sep 22, 2005 4:32 am
Posts: 7
Location: Italy
I was right, it's a change in behaviour in HSQL 1.8.

From hsqldb_1_8_0_2.zip!hsqldb/doc/guide/ch01.html:
Quote:
Closing the Database

All databases running in different modes can be closed with the SHUTDOWN command, issued as an SQL query. From version 1.7.2, in-process databases are no longer closed when the last connection to the database is explicitly closed via JDBC, a SHUTDOWN is required. In 1.8.0, a connection property, shutdown=true, can be specified on the first connection to the database (the connection that opens the database) to force a shutdown when the last connection closes.

When SHUTDOWN is issued, all active transactions are rolled back. A special form of closing the database is via the SHUTDOWN COMPACT command. This command rewrites the .data file that contains the information stored in CACHED tables and compacts it to size. This command should be issued periodically, especially when lots of inserts, updates or deletes have been performed on the cached tables. Changes to the structure of the database, such as dropping or modifying populated CACHED tables or indexes also create large amounts of unused file space that can be reclaimed using this command.


(Red color mine).

So, I went to hibernate-3.0.5.zip!hibernate-3.0/doc/reference/en/html/session-configuration.html and found:
Quote:
hibernate.connection.<propertyName> Pass the JDBC property propertyName to DriverManager.getConnection().


So, adding:
Code:
<property name="hibernate.connection.shutdown">true</property>

to my hibernate.cfg.xml did the trick.

The tutorial should be updated, however, to point out this peculiar change. And when you're at it, adding a sample log4j.properties file would help, too! :-)

Michele


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 5:39 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is an example log4j.properties in the Hibernate3 package, etc/ directory, as mentioned in the tutorial.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 11:28 am 
Newbie

Joined: Wed Sep 21, 2005 7:28 pm
Posts: 3
Location: San Francisco
Michele,

I tried your suggestion and added
Code:
<property name="hibernate.connection.shutdown">true</property>

to my hibernate.cfg.xml file.

The complete file now looks like:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:data/tutorial</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
   
   <property name="hibernate.connection.shutdown">true</property>

        <mapping resource="Event.hbm.xml"/>

    </session-factory>
</hibernate-configuration>


however, the tutorial still drops and recreates the tables each time.

My main (inside the EventManager class) is:
Code:
    public static void main(String[] args) {
   EventManager mgr = new EventManager();

   if (args[0].equals("store")) {
       mgr.createAndStoreEvent("My Event", new Date());
   }
   else if (args[0].equals("list")) {
       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.sessionFactory.close();
    }


However, just as an aside, if I change the main and replace all the argument based processing with:
Code:
   mgr.createAndStoreEvent("My Event",new Date());
   mgr.createAndStoreEvent("Event1",new Date());
   mgr.createAndStoreEvent("Event2",new Date());
   mgr.createAndStoreEvent("Event3",new Date());

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


that works correctly (e.g. multiple events are added to the table & the list at the end shows all of them).

so clearly hibernate and hsqldb are working, the problem is simply getting hsqldb to remember tables/data between runs.

Any advice on how to do that would be appreciated.

Thanks,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 11:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 12:10 pm 
Newbie

Joined: Thu Sep 22, 2005 4:32 am
Posts: 7
Location: Italy
schani,

I switched to MySQL, to isolate myself from configuration problems. Sorry, I can't help you more than this, however it should not be too difficult to research some more on the Hibernate/HSQL interactions.

Maybe some searching or asking on the HSQL forums may bring in more answers, since it's clearly more an integration / versioning issue than a Hibernate problem.

It is however a fact that the tutorial "as is" is broken for the Hibernate 3.0.5 / HSQL 1.8.0 combo. This seems connected to http://opensource2.atlassian.com/projec ... se/HHH-934 , by the way, so Christian already knows about it.

There is another possible solution in the jira record, so try that also. If everything fails, you could always add your comments to that issue. Remember to specify the HSQL version: that seems to be the key.

Thank you both in advance,

Michele


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 12:58 pm 
Newbie

Joined: Wed Sep 21, 2005 7:28 pm
Posts: 3
Location: San Francisco
Ok ... I've got it working.

For anyone else who runs into this problem. Here is what you need to do:

1) change your hibernate.cfg.xml file and replace
Code:
<property name="connection.url">jdbc:hsqldb:data/tutorial</property>

with
Code:
<property name="connection.url">jdbc:hsqldb:data/tutorial;shutdown=true</property>


2) make sure your hibernate.cfg.xml file also contains
Code:
<property name="hbm2ddl.auto">create</property>


3) run the tutorial (run ant -Daction=store)

4) this should run correctly and store an event in the table. Because of the shutdown=true option we added in step one, the table scheme and data will be written to the ./data/ directory when the program ends

5) go back into your hibernate.cfg.xml file and comment out
Code:
<property name="hbm2ddl.auto">create</property>

I did this as follows so it would be easy to reverse later
Code:
<!-- property name="hbm2ddl.auto" -- create -- /property -->


6) run the tutorial again (run ant -Daction=store) and a new event should be added on top of the one from step 3

7) run the tutorial again (run ant -Daction=list) and you should see both events listed


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 6:34 pm 
Newbie

Joined: Thu Sep 22, 2005 6:23 pm
Posts: 1
Shani's solution is very nice, I'm very grateful, and it worked for me, BUT...

I found that I had to delete everything from my ./data/ folder before this fix would work. I don't know if everyone will have this experience or not.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.