views:

305

answers:

3

I'm trying to propagate the schema from the Hibernate configuration to the RDBMS. The code runs without any error message but the database doesn't get updated.

Any hints ? Thank you !

Update That is hibernate-core only with a HSQL database.

Update 2 Yes, i should use SchemaExport (i'm away from hibernate a while ),but it don't flush to the database. It is a HSQL in-process database (jdbc:hsqldb:file:config/config).

Update 3 Something does not work with HSQL, trying now with MySQL and all works fine !

 public static void exportSchema() {
        new SchemaExport(hbConfig).create(true, true);
    }


public static void exportSchemaXXX() {

// sessionFactory and hbConfig defined in the class

        Session sess = sessionFactory.openSession();

        sess.doWork(new Work() {

            public void execute(java.sql.Connection conn) throws SQLException {
                System.err.println("work");
                try {
                    Class dialect = Class.forName(hbConfig.getProperty("hibernate.dialect"));
                    String[] lines = hbConfig.generateSchemaCreationScript((Dialect) dialect.newInstance());

                    for (String s : lines) {
                        System.err.println(s);
                        Statement stm = conn.createStatement();
                        stm.execute(s);
                    }
                } catch (Exception ex) {
                    System.err.println("Error: " + ex);
                }

            }
        });

        sess.flush();
        sess.close();
    }
A: 

Ensure that your transaction gets committed. Some databases will actually roll back schema changes if the transaction gets rolled back.

Adam Batkin
A: 

You can try forcing a commit when your done but with out knowing the database your using I'm not sure if this is the problem. Assuming your using pojo's to represent your database schema why are you executing the statements one by one through a JDBC connection instead of using Hibernates built in schema class similar to the following?

config=new AnnotationConfiguration()
config.addAnnotatedClass(Badge.class)
config.addAnnotatedClass(Vote.class)
config.configure()
new SchemaExport(config).create(true,true)//create the database tables

For more info on this see this link

Jared
A: 

Have this now. Seems that HSQLDB beginning with version 1.7 needs shutdown for in-memory to flush all changes.

PeterMmm