Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Schema Export using a spring config file?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Schema Export using a spring config file??

    Hi-

    Sorry for the noobi question but I have just inherited some code at work and am trying to do some integration.

    Here is the scenario. Right now my application uses a basic spring config file for 'autowiring' our components (simple normal spring stuff). This includes setting up the database and datasource (we are just using a local postgres db) and when I use the application all is fine.

    However, we have also used hibernates schema export facility to auto generate/connect to our db. This works excellent EXCEPT the task unfortunatly uses the hibernate.properties file (we spawn this task from from an ant script).

    What I was wondering if anybody had any experience with using the hibernate schema export facilities and a spring application config file together or ant integration. I imagine there must be a solution (or maybe a very short and easy program to write).

    The problem that we have now is that unfortunatly we have two seperate configuration files containing our db information (springConfig.xml and hibernate.properties) and I would like to tie it all into one nice config file (or at least one file with the db properties) such that I can auto generate the schema and run my application without any major heartburn.

    thanks a ton

    matt

  • #2
    Just get out the LocalSessionFactoryBean from your context and use
    the methods to create/delete/update the database schema :-)

    The trick is to prepend an & in order to get the bean, and not the session factory:

    myContext.getBean("&myLocalSessionFactoryBean")

    See:
    http://www.springframework.org/docs/...ctoryBean.html

    Comment


    • #3
      hi,

      I am tryinf to do that on spring 3.0.5 and it doesn't work, it says that a datasource is not set

      the issue seems that the dataSource is thread bound and reseted after configuration

      Code:
      	public void configure(Properties props) throws HibernateException {
      		this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource();
      		// absolutely needs thread-bound DataSource to initialize
      		if (this.dataSource == null) {
      			throw new HibernateException("No local DataSource found for configuration - " +
      			    "'dataSource' property must be set on LocalSessionFactoryBean");
      		}
      		this.dataSourceToUse = getDataSourceToUse(this.dataSource);
      	}
      
      	/**
      	 * Return the DataSource to use for retrieving Connections.
      	 * <p>This implementation returns the passed-in DataSource as-is.
      	 * @param originalDataSource the DataSource as configured by the user
      	 * on LocalSessionFactoryBean
      	 * @return the DataSource to actually retrieve Connections from
      	 * (potentially wrapped)
      	 * @see LocalSessionFactoryBean#setDataSource
      	 */
      	protected DataSource getDataSourceToUse(DataSource originalDataSource) {
      		return originalDataSource;
      	}
      2012-04-12 15:50:53,765 ERROR [[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'] org.hibernate.tool.hbm2ddl.SchemaExport (SchemaExport.java:287) - schema export unsuccessful
      org.hibernate.HibernateException: No local DataSource found for configuration - 'dataSource' property must be set on LocalSessionFactoryBean
      at org.springframework.orm.hibernate3.LocalDataSource ConnectionProvider.configure(LocalDataSourceConnec tionProvider.java:49)
      at org.hibernate.connection.ConnectionProviderFactory .newConnectionProvider(ConnectionProviderFactory.j ava:143)
      at org.hibernate.connection.ConnectionProviderFactory .newConnectionProvider(ConnectionProviderFactory.j ava:84)
      at org.hibernate.tool.hbm2ddl.ManagedProviderConnecti onHelper.prepare(ManagedProviderConnectionHelper.j ava:51)
      at org.hibernate.tool.hbm2ddl.SchemaExport.execute(Sc hemaExport.java:263)
      at org.hibernate.tool.hbm2ddl.SchemaExport.create(Sch emaExport.java:219)

      Comment


      • #4
        Originally posted by etrier View Post
        hi,

        I am tryinf to do that on spring 3.0.5 and it doesn't work, it says that a datasource is not set

        the issue seems that the dataSource is thread bound and reseted after configuration

        Did you ever get this to work? I am running into the same problem/error at the moment. Using the Hibernate Configuration object from the SessionFactory does not work, as SchemaExport essentially creates a new LocalSessionFactoryBean in which it does not carry the datasource over.

        Thanks,

        Eric

        Comment

        Working...
        X