diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java
index 07ede778ab4..5bc2c55ec2b 100644
--- a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java
+++ b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2009 the original author or authors.
+ * Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -875,21 +875,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
@Override
protected void afterSessionFactoryCreation() throws Exception {
if (this.schemaUpdate) {
- DataSource dataSource = getDataSource();
- if (dataSource != null) {
- // Make given DataSource available for the schema update,
- // which unfortunately reinstantiates a ConnectionProvider.
- configTimeDataSourceHolder.set(dataSource);
- }
- try {
- updateDatabaseSchema();
- }
- finally {
- if (dataSource != null) {
- // Reset DataSource holder.
- configTimeDataSourceHolder.set(null);
- }
- }
+ updateDatabaseSchema();
}
}
@@ -916,6 +902,93 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
}
+ /**
+ * Execute schema update script, determined by the Configuration object
+ * used for creating the SessionFactory. A replacement for Hibernate's
+ * SchemaUpdate class, for automatically executing schema update scripts
+ * on application startup. Can also be invoked manually.
+ *
Fetch the LocalSessionFactoryBean itself rather than the exposed
+ * SessionFactory to be able to invoke this method, e.g. via
+ * LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory");.
+ *
Uses the SessionFactory that this bean generates for accessing a
+ * JDBC connection to perform the script.
+ * @throws DataAccessException in case of script execution errors
+ * @see #setSchemaUpdate
+ * @see org.hibernate.cfg.Configuration#generateSchemaUpdateScript
+ * @see org.hibernate.tool.hbm2ddl.SchemaUpdate
+ */
+ public void updateDatabaseSchema() throws DataAccessException {
+ logger.info("Updating database schema for Hibernate SessionFactory");
+ DataSource dataSource = getDataSource();
+ if (dataSource != null) {
+ // Make given DataSource available for the schema update.
+ configTimeDataSourceHolder.set(dataSource);
+ }
+ try {
+ HibernateTemplate hibernateTemplate = new HibernateTemplate(getSessionFactory());
+ hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
+ hibernateTemplate.execute(
+ new HibernateCallback