-->
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.  [ 9 posts ] 
Author Message
 Post subject: HSQL Database (or table) recreated on each run
PostPosted: Thu Jul 12, 2007 9:40 pm 
Newbie

Joined: Thu Jul 12, 2007 9:27 pm
Posts: 11
I am having trouble loading data back from my HSQL database. I can save data to the DB. But, if I re-run my main method and try and load the data the DB is empty. Almost like the DB (or at least the table) is being recreated each time. See details below....

Hibernate version: 3

Mapping documents:
<?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:file:mydb
</property>
<property name="connection.username">blah</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>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</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>

<mapping class="com.myproject.Person" />

</session-factory>

</hibernate-configuration>


Code between sessionFactory.openSession() and session.close():
// I run this bit of code in a main method to store data in the DB
// then I exit my app.
List<Person> people = loadData(); // creates a bunch of Person objects
Session session = getSession();
Transaction tx = session.beginTransaction();
for (Person p : people) {
Object val = session.save(val);
System.out.println("Saved - " + val+ ", " + p.getName());
}

tx.commit();
session.close();

...after I run this code (above) HSQL generates a file, mydb.log (which is about 112kb).

// then i try and run this code and it returns 0 rows (and i can see that
// my DB files are empty
session = getSession();
tx = session.beginTransaction();
people = session.createQuery("from Person p").list();
System.out.println(people.size()) // outputs, 0

...when I run this code mydb.log becomes 0kb.

Name and version of the database you are using: HSQL 1.8.0


Any ideas why the DB (or table) is being recreated over and over?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 4:23 am 
Beginner
Beginner

Joined: Sat Jan 29, 2005 8:49 pm
Posts: 20
you already turne show sql on so what does it say?

Code:
<property name="show_sql">true</property>


what sql is generated?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 5:02 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Code:
<property name="hbm2ddl.auto">create</property>


Comment the above line and try again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 5:15 am 
Newbie

Joined: Thu Jul 12, 2007 9:27 pm
Posts: 11
Thanks for the help.

1. comment out the "hbm2dll.auto" line in the config file.

seemed to do the trick


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 5:32 am 
Newbie

Joined: Thu Jul 12, 2007 9:27 pm
Posts: 11
Actually...that didn't completely solve my issues. here are the steps I am trying right now.

1. Comment out the hbm2dll.auto line in the config file.

2. Create the database (i.e. use ant script which has a "sql" target in it which does a 'CREATE TABLE ....')

3. Run my code which tries to save a bunch of Person objects.
for (Person p : getPeople())
session.save(p)

This gives me errors:
[ WARN] 25:28 (JDBCExceptionReporter.java:logExceptions:77)
SQL Error: -22, SQLState: S0002

[ERROR] 25:28 (JDBCExceptionReporter.java:logExceptions:78)
Table not found in statement [insert into PEOPLE (NAME, AGE, SEX) values (?, ?, ?)]

[ERROR] 25:28 (AbstractFlushingEventListener.java:performExecutions:301)
Could not synchronize database state with session

org.hibernate.exception.SQLGrammarException: could not insert: [com.myproject.Person]



Now if instead I do the following:

1. Delete the database

2. Uncomment the hbm2dll.auto line in the config, so now I have:
<property name="hbm2dll.auto">create</property>

3. Run my code which tries to save a bunch of Person objects:
for (Person p : getPeople())
session.save(p)

It works just like I want. However, I want to rerun my code to retrieve the Person objects, I have to comment out the hbm2dll.auto line before doing so. Seems like there should be an easier way for this work (i.e. not having to comment/uncomment that line in the config file before re-running the app).

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 5:40 am 
Newbie

Joined: Thu Jul 12, 2007 9:27 pm
Posts: 11
actually I think switching hbm2dll.auto to "update" may be what I need.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 8:35 am 
Beginner
Beginner

Joined: Mon Apr 23, 2007 8:30 am
Posts: 27
Location: India
Can you post your hbm file? Looks like something smells in the table to object mapping.

-Nikhil


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 9:39 am 
Newbie

Joined: Thu Jul 12, 2007 9:27 pm
Posts: 11
the file is in my original post with the exception that I now have hbm2dll.auto set to "update".


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 21, 2007 11:05 pm 
Newbie

Joined: Sat Jul 21, 2007 11:01 pm
Posts: 3
<property name="hbm2ddl.auto">upate</property>
i thought above must work

_________________
Ashish Gupta

Sr. System Analyst
India


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