📄 localsessionfactorybean.java
字号:
}
}
// Execute schema update if requested.
if (this.schemaUpdate) {
updateDatabaseSchema();
}
}
/**
* Subclasses can override this method to perform custom initialization
* of the Configuration instance used for SessionFactory creation.
* The properties of this LocalSessionFactoryBean will be applied to
* the Configuration object that gets returned here.
* <p>The default implementation creates a new Configuration instance.
* A custom implementation could prepare the instance in a specific way,
* or use a custom Configuration subclass.
* @return the Configuration instance
* @throws HibernateException in case of Hibernate initialization errors
* @see net.sf.hibernate.cfg.Configuration#Configuration()
*/
protected Configuration newConfiguration() throws HibernateException {
return new Configuration();
}
/**
* To be implemented by subclasses that want to to perform custom
* post-processing of the Configuration object after this FactoryBean
* performed its default initialization.
* @param config the current Configuration object
* @throws HibernateException in case of Hibernate initialization errors
*/
protected void postProcessConfiguration(Configuration config) throws HibernateException {
}
/**
* Subclasses can override this method to perform custom initialization
* of the SessionFactory instance, creating it via the given Configuration
* object that got prepared by this LocalSessionFactoryBean.
* <p>The default implementation invokes Configuration's buildSessionFactory.
* A custom implementation could prepare the instance in a specific way,
* or use a custom SessionFactoryImpl subclass.
* @param config Configuration prepared by this LocalSessionFactoryBean
* @return the SessionFactory instance
* @throws HibernateException in case of Hibernate initialization errors
* @see net.sf.hibernate.cfg.Configuration#buildSessionFactory
*/
protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
return config.buildSessionFactory();
}
/**
* Execute schema drop script, determined by the Configuration object
* used for creating the SessionFactory. A replacement for Hibernate's
* SchemaExport class, to be invoked on application setup.
* <p>Fetch the LocalSessionFactoryBean itself rather than the exposed
* SessionFactory to be able to invoke this method, e.g. via
* <code>LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory");</code>.
* <p>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 net.sf.hibernate.cfg.Configuration#generateDropSchemaScript
* @see net.sf.hibernate.tool.hbm2ddl.SchemaExport#drop
*/
public void dropDatabaseSchema() throws DataAccessException {
logger.info("Dropping database schema for Hibernate SessionFactory");
HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory);
hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Connection con = session.connection();
Dialect dialect = Dialect.getDialect(configuration.getProperties());
String[] sql = configuration.generateDropSchemaScript(dialect);
executeSchemaScript(con, sql);
return null;
}
}
);
}
/**
* Execute schema creation script, determined by the Configuration object
* used for creating the SessionFactory. A replacement for Hibernate's
* SchemaExport class, to be invoked on application setup.
* <p>Fetch the LocalSessionFactoryBean itself rather than the exposed
* SessionFactory to be able to invoke this method, e.g. via
* <code>LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory");</code>.
* <p>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 net.sf.hibernate.cfg.Configuration#generateSchemaCreationScript
* @see net.sf.hibernate.tool.hbm2ddl.SchemaExport#create
*/
public void createDatabaseSchema() throws DataAccessException {
logger.info("Creating database schema for Hibernate SessionFactory");
HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory);
hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Connection con = session.connection();
final Dialect dialect = Dialect.getDialect(configuration.getProperties());
String[] sql = configuration.generateSchemaCreationScript(dialect);
executeSchemaScript(con, sql);
return null;
}
}
);
}
/**
* 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.
* <p>Fetch the LocalSessionFactoryBean itself rather than the exposed
* SessionFactory to be able to invoke this method, e.g. via
* <code>LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory");</code>.
* <p>Uses the SessionFactory that this bean generates for accessing a JDBC
* connection to perform the script.
* @throws HibernateException in case of Hibernate initialization errors
* @see #setSchemaUpdate
* @see net.sf.hibernate.cfg.Configuration#generateSchemaUpdateScript
* @see net.sf.hibernate.tool.hbm2ddl.SchemaUpdate
*/
public void updateDatabaseSchema() throws HibernateException {
logger.info("Updating database schema for Hibernate SessionFactory");
HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory);
hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Connection con = session.connection();
final Dialect dialect = Dialect.getDialect(configuration.getProperties());
DatabaseMetadata metadata = new DatabaseMetadata(con, dialect);
String[] sql = configuration.generateSchemaUpdateScript(dialect, metadata);
executeSchemaScript(con, sql);
return null;
}
}
);
}
/**
* Execute the given schema script on the given JDBC Connection.
* <p>Note that the default implementation will log unsuccessful statements
* and continue to execute. Override the <code>executeSchemaStatement</code>
* method to treat failures differently.
* @param con the JDBC Connection to execute the script on
* @param sql the SQL statements to execute
* @throws SQLException if thrown by JDBC methods
* @see #executeSchemaStatement
*/
protected void executeSchemaScript(Connection con, String[] sql) throws SQLException {
if (sql != null && sql.length > 0) {
boolean oldAutoCommit = con.getAutoCommit();
if (!oldAutoCommit) {
con.setAutoCommit(true);
}
try {
Statement stmt = con.createStatement();
try {
for (int i = 0; i < sql.length; i++) {
executeSchemaStatement(stmt, sql[i]);
}
}
finally {
JdbcUtils.closeStatement(stmt);
}
}
finally {
if (!oldAutoCommit) {
con.setAutoCommit(false);
}
}
}
}
/**
* Execute the given schema SQL on the given JDBC Statement.
* <p>Note that the default implementation will log unsuccessful statements
* and continue to execute. Override this method to treat failures differently.
* @param stmt the JDBC Statement to execute the SQL on
* @param sql the SQL statement to execute
* @throws SQLException if thrown by JDBC methods (and considered fatal)
*/
protected void executeSchemaStatement(Statement stmt, String sql) throws SQLException {
if (logger.isDebugEnabled()) {
logger.debug("Executing schema statement: " + sql);
}
try {
stmt.executeUpdate(sql);
}
catch (SQLException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Unsuccessful schema statement: " + sql, ex);
}
}
}
/**
* Return the Configuration object used to build the SessionFactory.
* Allows access to configuration metadata stored there (rarely needed).
*/
public Configuration getConfiguration() {
return configuration;
}
/**
* Return the singleton SessionFactory.
*/
public Object getObject() {
return this.sessionFactory;
}
public Class getObjectType() {
return (this.sessionFactory != null) ? this.sessionFactory.getClass() : SessionFactory.class;
}
public boolean isSingleton() {
return true;
}
/**
* Close the SessionFactory on bean factory shutdown.
*/
public void destroy() throws HibernateException {
logger.info("Closing Hibernate SessionFactory");
if (this.dataSource != null) {
// Make given DataSource available for potential SchemaExport,
// which unfortunately reinstantiates a ConnectionProvider.
configTimeDataSourceHolder.set(this.dataSource);
}
try {
this.sessionFactory.close();
}
finally {
if (this.dataSource != null) {
// Reset DataSource holder.
configTimeDataSourceHolder.set(null);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -