HHH-8113 - Persistence.createEntityManagerFactory() should run schema export if JPA properties are set
This commit is contained in:
parent
2f40949719
commit
442c3268b3
|
@ -34,6 +34,8 @@ import org.hibernate.internal.util.StringHelper;
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
public class DDLFormatterImpl implements Formatter {
|
public class DDLFormatterImpl implements Formatter {
|
||||||
|
public static final DDLFormatterImpl INSTANCE = new DDLFormatterImpl();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format an SQL statement using simple rules<ul>
|
* Format an SQL statement using simple rules<ul>
|
||||||
* <li>Insert newline after each comma</li>
|
* <li>Insert newline after each comma</li>
|
||||||
|
|
|
@ -68,7 +68,7 @@ public enum SchemaGenAction {
|
||||||
* @throws IllegalArgumentException If the incoming value is unrecognized
|
* @throws IllegalArgumentException If the incoming value is unrecognized
|
||||||
*/
|
*/
|
||||||
public static SchemaGenAction interpret(String value) {
|
public static SchemaGenAction interpret(String value) {
|
||||||
if ( StringHelper.isEmpty( value ) ) {
|
if ( StringHelper.isEmpty( value ) || NONE.externalName.equals( value ) ) {
|
||||||
// default is NONE
|
// default is NONE
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ class DatabaseTarget implements GenerationTarget {
|
||||||
|
|
||||||
for ( String command : commands ) {
|
for ( String command : commands ) {
|
||||||
try {
|
try {
|
||||||
|
jdbcConnectionContext.logSqlStatement( command );
|
||||||
jdbcStatement().execute( command );
|
jdbcStatement().execute( command );
|
||||||
}
|
}
|
||||||
catch (SQLException e) {
|
catch (SQLException e) {
|
||||||
|
@ -87,12 +88,12 @@ class DatabaseTarget implements GenerationTarget {
|
||||||
|
|
||||||
for ( String command : commands ) {
|
for ( String command : commands ) {
|
||||||
try {
|
try {
|
||||||
|
jdbcConnectionContext.logSqlStatement( command );
|
||||||
jdbcStatement().execute( command );
|
jdbcStatement().execute( command );
|
||||||
}
|
}
|
||||||
catch (SQLException e) {
|
catch (SQLException e) {
|
||||||
throw new PersistenceException(
|
// Just log the error because drop commands are often unsuccessful because the tables do not yet exist...
|
||||||
"Unable to execute JPA schema generation drop command [" + command + "]"
|
log.warn( String.format( "Unable to execute JPA schema generation drop command [%s]", command ), e );
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,9 @@ import javax.persistence.PersistenceException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.hibernate.engine.jdbc.internal.DDLFormatterImpl;
|
||||||
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
|
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
|
* Defines access to a JDBC Connection for use in Schema generation
|
||||||
|
@ -36,10 +38,13 @@ import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
|
||||||
*/
|
*/
|
||||||
class JdbcConnectionContext {
|
class JdbcConnectionContext {
|
||||||
private final JdbcConnectionAccess jdbcConnectionAccess;
|
private final JdbcConnectionAccess jdbcConnectionAccess;
|
||||||
|
private final SqlStatementLogger sqlStatementLogger;
|
||||||
|
|
||||||
private Connection jdbcConnection;
|
private Connection jdbcConnection;
|
||||||
|
|
||||||
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess) {
|
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess, SqlStatementLogger sqlStatementLogger) {
|
||||||
this.jdbcConnectionAccess = jdbcConnectionAccess;
|
this.jdbcConnectionAccess = jdbcConnectionAccess;
|
||||||
|
this.sqlStatementLogger = sqlStatementLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getJdbcConnection() {
|
public Connection getJdbcConnection() {
|
||||||
|
@ -64,4 +69,8 @@ class JdbcConnectionContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void logSqlStatement(String sqlStatement) {
|
||||||
|
sqlStatementLogger.logStatement( sqlStatement, DDLFormatterImpl.INSTANCE );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,8 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||||
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
||||||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
||||||
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
|
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.StringHelper;
|
||||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||||
import org.hibernate.jpa.AvailableSettings;
|
import org.hibernate.jpa.AvailableSettings;
|
||||||
|
@ -249,6 +251,8 @@ public class JpaSchemaGenerator {
|
||||||
private static JdbcConnectionContext determineAppropriateJdbcConnectionContext(
|
private static JdbcConnectionContext determineAppropriateJdbcConnectionContext(
|
||||||
Configuration hibernateConfiguration,
|
Configuration hibernateConfiguration,
|
||||||
ServiceRegistry serviceRegistry) {
|
ServiceRegistry serviceRegistry) {
|
||||||
|
final SqlStatementLogger sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();
|
||||||
|
|
||||||
// see if a specific connection has been provided:
|
// see if a specific connection has been provided:
|
||||||
final Connection providedConnection = (Connection) hibernateConfiguration.getProperties().get(
|
final Connection providedConnection = (Connection) hibernateConfiguration.getProperties().get(
|
||||||
AvailableSettings.SCHEMA_GEN_CONNECTION
|
AvailableSettings.SCHEMA_GEN_CONNECTION
|
||||||
|
@ -271,7 +275,8 @@ public class JpaSchemaGenerator {
|
||||||
public boolean supportsAggressiveRelease() {
|
public boolean supportsAggressiveRelease() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
sqlStatementLogger
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,12 +298,13 @@ public class JpaSchemaGenerator {
|
||||||
public boolean supportsAggressiveRelease() {
|
public boolean supportsAggressiveRelease() {
|
||||||
return connectionProvider.supportsAggressiveRelease();
|
return connectionProvider.supportsAggressiveRelease();
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
sqlStatementLogger
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, return a no-op impl
|
// otherwise, return a no-op impl
|
||||||
return new JdbcConnectionContext( null ) {
|
return new JdbcConnectionContext( null, sqlStatementLogger ) {
|
||||||
@Override
|
@Override
|
||||||
public Connection getJdbcConnection() {
|
public Connection getJdbcConnection() {
|
||||||
throw new PersistenceException( "No connection information supplied" );
|
throw new PersistenceException( "No connection information supplied" );
|
||||||
|
@ -405,13 +411,13 @@ public class JpaSchemaGenerator {
|
||||||
List<GenerationSource> dropSourceList,
|
List<GenerationSource> dropSourceList,
|
||||||
List<GenerationTarget> targets) {
|
List<GenerationTarget> targets) {
|
||||||
for ( GenerationTarget target : targets ) {
|
for ( GenerationTarget target : targets ) {
|
||||||
for ( GenerationSource source : createSourceList ) {
|
|
||||||
target.acceptCreateCommands( source.getCommands() );
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( GenerationSource source : dropSourceList ) {
|
for ( GenerationSource source : dropSourceList ) {
|
||||||
target.acceptDropCommands( source.getCommands() );
|
target.acceptDropCommands( source.getCommands() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for ( GenerationSource source : createSourceList ) {
|
||||||
|
target.acceptCreateCommands( source.getCommands() );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue