0

I am very new to this. And I am trying to configure a net-beans Maven project with hsqldb and hibernate. I have set up the database and I am able to save some data to the database. I am using the file mode. and this is my connection url

 "jdbc:hsqldb:file:C:/data/findb"

however, the problem I am having is my database tables will be dropped at every execution and re-created brand new. I don't want this to happen. I want to create the tables only once and be able to update it afterwards.

Here is a snippet of my configuration file

  boolean found = false;
        String URL = "jdbc:hsqldb:file:C:/data/findb";

        Configuration cfg = new Configuration();

        if (!found) {
            cfg.setProperty("hibernate.hbm2ddl.auto", "create");
        } else {
            cfg.setProperty("hibernate.hbm2ddl.auto", "update");
        }

        cfg.setProperty("hibernate.connection.driver_class",
                "org.hsqldb.jdbcDriver");
        cfg.setProperty("hibernate.connection.url", URL);
        cfg.setProperty("hibernate.connection.username", "sa");
        cfg.setProperty("hibernate.connection.password", "1234");
        cfg.setProperty("hibernate.connection.pool_size", "20");
       // cfg.setProperty("hibernate.connection.hsqldb.default_table_type",
            //    "cached");

        sessionFactory = cfg.configure().buildSessionFactory();

And to my understanding that bit should have prevented the dropping of tables at each creation since I use the if-else statement to create if doesn't exist and update if it does.

This is how mydb.log starts

/*C4*/SET SCHEMA PUBLIC
 drop table FinPlan if exists
 drop table PersonalAssets if exists
 drop table TB_UNIQUEID if exists

What am I missing?? any help will be appreciated. Thank you in advance.

Precious
  • 237
  • 2
  • 7
  • 19

1 Answers1

2

The hibernate.hbm2ddl.auto = create will automatically recreate your tables at each executions. Since you set found to false, hibernate.hbm2ddl.auto will always be "create"

Ulf Lindback
  • 13,974
  • 3
  • 40
  • 31
  • 1
    correct -- See this (question and) answer for more details: http://stackoverflow.com/questions/438146/hibernate-question-hbm2ddl-auto-possible-values-and-what-they-do/1689769#1689769 – Ralph Aug 23 '11 at 05:50
  • man you are a genius. thank you very much, it works well now, i set the value of found to true so it doesn't drop my tables. – Precious Aug 23 '11 at 06:09
  • One quick question.... now it changed to something like this .... DELETE FROM TB_UNIQUEID WHERE ID=1 INSERT INTO TB_UNIQUEID VALUES(1,160,1314080801652,'global_tb') COMMIT ... does this mean it will delete all the data that was in my table before adding a new data? – Precious Aug 23 '11 at 06:32
  • @Precious This log is normal for updates. It deletes one row and inserts the updated version. It does not delete all the data. – fredt Aug 23 '11 at 09:59