Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LocalSessionFactoryBean.validateDatabaseSchema() [SPR-3212] #7898

Closed
spring-projects-issues opened this issue Feb 27, 2007 · 4 comments
Closed
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Kalle Korhonen opened SPR-3212 and commented

There's create-, drop- and updateDatabaseSchema() operations in LocalSessionFactoryBean. For completeness, add validateDatabaseSchema(). This is useful if your application doesn't have permissions to modify the schema itself. If using hibernate, one cannot auto-validate the scema in a webapplication because context loading would fail with HibernateException if schema has changed, so the application would need to validate the schema at a later point. You could also consider adding a strategy for supporting hibernate.hbm2ddl.auto=validate.

In Hibernate, schema validation can be performed with:
SchemaValidator validator = new SchemaValidator(configuration);
validator.validate();

Thread this is discussed:
http://forum.springframework.org/showthread.php?t=35274


Affects: 2.0.2

Referenced from: commits b370969

3 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

David Wood commented

Agreed. This would be good to have.

@spring-projects-issues
Copy link
Collaborator Author

Dan Dude commented

It seems to be fairly simple to implement. I have extended AnnotationSessionFactoryBean and implemented the feature as follows:

package org.springframework.orm.hibernate3.annotation;

import java.sql.Connection;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

public class SchemaValidatingAnnotationSessionFactoryBean extends
AnnotationSessionFactoryBean {

public void validateDatabaseSchema() throws DataAccessException {
	logger.info("Validating database schema for Hibernate SessionFactory");
	HibernateTemplate hibernateTemplate = new HibernateTemplate(
			getSessionFactory());
	hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
	hibernateTemplate.execute(new HibernateCallback() {
		public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
			Connection con = session.connection();
			Dialect dialect = Dialect.getDialect(getConfiguration()
					.getProperties());
			DatabaseMetadata metadata = new DatabaseMetadata(con, dialect);
			Configuration configuration = getConfiguration();
			configuration.validateSchema(dialect, metadata);
			return null;
		}
	});

}

}

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Finally added for Spring 3.0.2, as a public method to be called on LocalSessionFactoryBean.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Pascal Leclercq commented

Hello,
LocalSessionFactoryBean.validateDatabaseSchema() exists for hibernate 3 (package org.springframework.orm.hibernate3) but doesn't for hibernate 4 (org.springframework.orm.hibernate4).

Do you plan to implement It also for hibernate 4 or is there any other mechanism that can do the trick ? (eg : hibernate.hbm2ddl.auto).

Thanks in advance.

@spring-projects-issues spring-projects-issues added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants