Skip to content

Commit

Permalink
HHH-8113 - Persistence.createEntityManagerFactory() should run schema…
Browse files Browse the repository at this point in the history
… export if JPA properties are set
  • Loading branch information
sebersole committed Apr 3, 2013
1 parent 2f40949 commit 442c326
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
Expand Up @@ -34,6 +34,8 @@
* @author Steve Ebersole
*/
public class DDLFormatterImpl implements Formatter {
public static final DDLFormatterImpl INSTANCE = new DDLFormatterImpl();

/**
* Format an SQL statement using simple rules<ul>
* <li>Insert newline after each comma</li>
Expand Down
Expand Up @@ -68,7 +68,7 @@ private SchemaGenAction(String externalName) {
* @throws IllegalArgumentException If the incoming value is unrecognized
*/
public static SchemaGenAction interpret(String value) {
if ( StringHelper.isEmpty( value ) ) {
if ( StringHelper.isEmpty( value ) || NONE.externalName.equals( value ) ) {
// default is NONE
return NONE;
}
Expand Down
Expand Up @@ -57,6 +57,7 @@ public void acceptCreateCommands(Iterable<String> commands) {

for ( String command : commands ) {
try {
jdbcConnectionContext.logSqlStatement( command );
jdbcStatement().execute( command );
}
catch (SQLException e) {
Expand Down Expand Up @@ -87,12 +88,12 @@ public void acceptDropCommands(Iterable<String> commands) {

for ( String command : commands ) {
try {
jdbcConnectionContext.logSqlStatement( command );
jdbcStatement().execute( command );
}
catch (SQLException e) {
throw new PersistenceException(
"Unable to execute JPA schema generation drop command [" + command + "]"
);
// Just log the error because drop commands are often unsuccessful because the tables do not yet exist...
log.warn( String.format( "Unable to execute JPA schema generation drop command [%s]", command ), e );
}
}
}
Expand Down
Expand Up @@ -27,7 +27,9 @@
import java.sql.Connection;
import java.sql.SQLException;

import org.hibernate.engine.jdbc.internal.DDLFormatterImpl;
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;

/**
* Defines access to a JDBC Connection for use in Schema generation
Expand All @@ -36,10 +38,13 @@
*/
class JdbcConnectionContext {
private final JdbcConnectionAccess jdbcConnectionAccess;
private final SqlStatementLogger sqlStatementLogger;

private Connection jdbcConnection;

JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess) {
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess, SqlStatementLogger sqlStatementLogger) {
this.jdbcConnectionAccess = jdbcConnectionAccess;
this.sqlStatementLogger = sqlStatementLogger;
}

public Connection getJdbcConnection() {
Expand All @@ -64,4 +69,8 @@ public void release() {
}
}
}

public void logSqlStatement(String sqlStatement) {
sqlStatementLogger.logStatement( sqlStatement, DDLFormatterImpl.INSTANCE );
}
}
Expand Up @@ -45,6 +45,8 @@
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.jpa.AvailableSettings;
Expand Down Expand Up @@ -249,6 +251,8 @@ else if ( sourceType == SchemaGenSource.SCRIPTS_THEN_METADATA ) {
private static JdbcConnectionContext determineAppropriateJdbcConnectionContext(
Configuration hibernateConfiguration,
ServiceRegistry serviceRegistry) {
final SqlStatementLogger sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();

// see if a specific connection has been provided:
final Connection providedConnection = (Connection) hibernateConfiguration.getProperties().get(
AvailableSettings.SCHEMA_GEN_CONNECTION
Expand All @@ -271,7 +275,8 @@ public void releaseConnection(Connection connection) throws SQLException {
public boolean supportsAggressiveRelease() {
return false;
}
}
},
sqlStatementLogger
);
}

Expand All @@ -293,12 +298,13 @@ public void releaseConnection(Connection connection) throws SQLException {
public boolean supportsAggressiveRelease() {
return connectionProvider.supportsAggressiveRelease();
}
}
},
sqlStatementLogger
);
}

// otherwise, return a no-op impl
return new JdbcConnectionContext( null ) {
return new JdbcConnectionContext( null, sqlStatementLogger ) {
@Override
public Connection getJdbcConnection() {
throw new PersistenceException( "No connection information supplied" );
Expand Down Expand Up @@ -405,13 +411,13 @@ private static void doGeneration(
List<GenerationSource> dropSourceList,
List<GenerationTarget> targets) {
for ( GenerationTarget target : targets ) {
for ( GenerationSource source : createSourceList ) {
target.acceptCreateCommands( source.getCommands() );
}

for ( GenerationSource source : dropSourceList ) {
target.acceptDropCommands( source.getCommands() );
}

for ( GenerationSource source : createSourceList ) {
target.acceptCreateCommands( source.getCommands() );
}
}
}

Expand Down

0 comments on commit 442c326

Please sign in to comment.