• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: How to avoid drop & recreate of table on Server restart

 
Manish Shah
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is there any where we can configure the server to create the table just once & then let the data be persisted in it.

It should not re-create the table everytime I restart my application server and also I should be able to see the data that I saved earlier.

Can any one help me out..

Thanks
Manish
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
details please.

which application server are you using?
 
Manish Shah
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

I am using JBoss 4.2. & Hypersonic SQL 1.8.0

I tried by setting the property value of hbm2ddl.auto to validate.
<property name="hbm2ddl.auto">validate</property>

This works it does not drops & recreates the table. I am able to see the previous data saved by me.

But this does not work if the table does not exists. I am looking for a solution where the application will automatically create the table once & will keep on persisting the data irrespective of number of times I restart the application server.

By using <property name="hbm2ddl.auto">create</property> it every time drops & creates the table.

I am also starting the database server from my codebase by creating the instance of class Server and calling server.start().

Another soln. is to first create a table before running the application.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you've always got the auto creation entry enabled, you'll always wipe out the database.

The SchemaExport class can give you programatic control, which you can then do anything you want with regards to how well you can program.




The SchemaExport Create Method

You will notice that the create method of the SchemaExport object takes two boolean parameters. The first boolean value indicates whether or not you want the generated database creation script to be printed out to the log file. The second parameter indicates whether you want the generated script to be executed against the underlying database. Passing two true values to the create method will cause the database generation scripts to be printed out to the log files, while also triggering the execution of the database creation scripts, which would mean dropping the existing tables in the database, and subsequently recreating them.

public void create(boolean script, boolean export)

Run the schema creation script.

Parameters:

script - print the DDL to the console

export - export the script to the database


Tutorial on Using the Hibernate SchemaExport Class to Create a Database

-Cameron McKenzie
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually your problem is you are using Hypersonic which is just set up as an in-memory database, so everytime you restart your server, the in-memory is lost.

There is another Datasource, or maybe even in the one in the deploy directory that has attributes commented out that will turn Hypersonic into a file memory database to save data on server restarts. If it is another datasource, go to the JBoss docs/examples/jca directory and find tons of datasources for all types of databases.

But Hypersonic should never be used as a production database. If you want a free database, use Postgres or MySql.

Mark
 
Manish Shah
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks for your earliest reply.

Yes the tables are created inmemory.

In order to avoid drops & creates & i have kept the below entry in hibernate config
<property name="hbm2ddl.auto">validate</property>

and have written a simple jdbc servlet (loads on start up) that will check whether the table is existing or not. It will start the HSQL server & then it will create the table if it does not exists.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic