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.  [ 2 posts ] 
Author Message
 Post subject: Configurable SchemaExport import file (2 session factories)
PostPosted: Mon Jan 28, 2008 8:37 am 
Newbie

Joined: Sat Mar 05, 2005 2:36 pm
Posts: 19
I have two separate session factories (sf1, sf2) that both load their proper import.sql during the session factory's initialization (hibernate.hbm2ddl.auto is set to create). Each session factory resides in its own jar and the test cases work perfectly in an isolated environment.

As soon as both session factories are loaded from the main application, sf2 executes schema export with the contents of sf1's import.sql. Of course that results in an error. I assume this happens because the thread's class loader is used (import.sql is loaded using ConfigHelper), but I might be wrong.

An option would be to rename the two import.sql files to import_project1.sql and import_project2.sql, and load them during initialization of the respective session factories. The problem is, I could not find a way to configure the importFile property in SchemaExport. I don't see a way to override it using either config or settings. How could I do something like that?

In this project, sf1 is loaded via a managed persistence context in JBoss Seam. sf2 is loaded by Spring using a customized AnnotationSessionFactoryBean. See the error I encounter during the intialization of sf2 below:

Code:
INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] Running hbm2ddl schema export
INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] exporting generated schema to database
INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] Executing import script: /import.sql
ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] schema export unsuccessful
org.hibernate.JDBCException: Error during import script execution
   at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:258)
   at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:192)
   at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:918)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:753)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:691)
   …
Caused by: java.sql.SQLException: Table not found in statement [insert into user]
   at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
   at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
   at org.hsqldb.jdbc.jdbcStatement.execute(Unknown Source)
   at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:254)


I hope someone can point me to a way to do that.

Thank you,
Kariem


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 10:53 am 
Newbie

Joined: Sat Mar 05, 2005 2:36 pm
Posts: 19
Unfortunately I could not find an answer here, so I have made the following changes. I could not find any other feasible way (extend the session factory and override a single method, set a configuration property, inject some kind of configuration loader), because of the way the configuration intializes SessionFactoryImpl.

What I did is override the initialization code for the session factory in my AnnotationSessionFactoryBean (Spring). I have one additional property customImportFile that holds the filename to replace import.sql. If this property is set, I disable schema export by removing the appropriate property from the configuration and do that same import with the correct file after the session factory's initialization.

Code:
@Override
protected SessionFactory newSessionFactory(Configuration config)
      throws HibernateException {
   if (customImportFile == null) {
      return super.newSessionFactory(config);
   }
   Properties props = config.getProperties();
   String auto = props.getProperty(Environment.HBM2DDL_AUTO);
   if (auto == null || !auto.equals("create")) {
      throw new HibernateException(
            "Could not create session factory with settings for "
                  + Environment.HBM2DDL_AUTO
                  + ". Only 'create' supported.");
   }
   logger.debug("Removing 'create' setting for "
         + Environment.HBM2DDL_AUTO
         + ". Postponing to after session factory creation.");
   props.remove(Environment.HBM2DDL_AUTO);

   SessionFactoryImpl sf = (SessionFactoryImpl) config
         .buildSessionFactory();
   Settings settings = sf.getSettings();
   logger.debug("Performing schema export with custom import file: '"
         + customImportFile + "'.");
   SchemaExport export = new SchemaExport(config, settings);
   export.setImportFile(customImportFile);
   export.create(false, true);
   return sf;
}


I hope this can help anyone. It's working for me. Should I put this in JIRA (with a feature request to make the schema export feature more extensible)?

Kariem


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