Configuration.generateSchemaCreationScript() 在 Hibernate 5 中去了哪里
Where did Configuration.generateSchemaCreationScript() go in Hibernate 5
在 Hibernate 4.x 中,我曾经生成并导出在带注释的实体中定义的模式,如下所示(使用 Spring 在 class 路径上查找带注释的实体):
Connection connection =
DriverManager.getConnection("jdbc:h2:mem:jooq-meta-extensions", "sa", "");
Configuration configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
// [...] adding annotated classes to Configuration here...
configuration.generateSchemaCreationScript(
Dialect.getDialect(configuration.getProperties()));
SchemaExport export = new SchemaExport(configuration, connection);
export.create(true, true);
这在 Hibernate 5.0 中不再有效:
除了:
,我在 migration guide 中没有真正找到任何明显的对这一变化的引用
Quite a few methods have been removed from Configuration
基于一组带注释的实体,使用 Hibernate 5.0 在现有 JDBC 连接 上生成和导出数据库 的正确方法是什么? (纯基于 JPA 的解决方案也很好)
(请注意,仅删除对 generateSchemaCreationScript()
的调用似乎可行,但我希望确保正确执行此操作)
新 SchemaExport
初始化的一个示例在 SchemaExportTask 中找到:
final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
final MetadataSources metadataSources = new MetadataSources( bsr );
final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder( bsr );
if ( configurationFile != null ) {
ssrBuilder.configure( configurationFile );
}
if ( propertiesFile != null ) {
ssrBuilder.loadProperties( propertiesFile );
}
ssrBuilder.applySettings( getProject().getProperties() );
for ( String fileName : getFiles() ) {
if ( fileName.endsWith(".jar") ) {
metadataSources.addJar( new File( fileName ) );
}
else {
metadataSources.addFile( fileName );
}
}
final StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) ssrBuilder.build();
final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder( ssr );
ClassLoaderService classLoaderService = bsr.getService( ClassLoaderService.class );
if ( implicitNamingStrategy != null ) {
metadataBuilder.applyImplicitNamingStrategy(
(ImplicitNamingStrategy) classLoaderService.classForName( implicitNamingStrategy ).newInstance()
);
}
if ( physicalNamingStrategy != null ) {
metadataBuilder.applyPhysicalNamingStrategy(
(PhysicalNamingStrategy) classLoaderService.classForName( physicalNamingStrategy ).newInstance()
);
}
return new SchemaExport( (MetadataImplementor) metadataBuilder.build() )
.setHaltOnError( haltOnError )
.setOutputFile( outputFile.getPath() )
.setDelimiter( delimiter );
当然,您可以根据自己的需要进行定制。
新的 bootstrap API 允许许多自定义,但假设您不需要这些,最短的调用看起来像这样,为服务注册表和所有设置应用默认值:
Metadata metadata = new MetadataSources()
.addAnnotatedClass( MyEntity.class )
.build();
new SchemaExport( (MetadataImplementor) metadata )
.setOutputFile( "my-statements.ddl" )
.create( Target.NONE );
更新:提供应用配置属性的示例
有几种方法可以为连接URL、方言等注入属性。例如您可以提供一个文件 hibernate.properties 或者您使用根据所需设置定制的服务注册表:
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySetting( "hibernate.connection.url", "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" )
.build();
Metadata metadata = new MetadataSources( registry )
.build();
感谢 and 的回答,我设法通过新配置 API 找到了生成具有以下等效导出逻辑的方法。当然,历史表明这个API会再次崩溃,所以一定要选择合适的版本:
休眠 5.2:
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.applySetting("javax.persistence.schema-generation-connection", connection)
.build());
// [...] adding annotated classes to metadata here...
metadata.addAnnotatedClass(...);
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
Hibernate 5.2(无警告):
上面会产生一些讨厌的警告,可以忽略:
Okt 20, 2016 2:57:16 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Okt 20, 2016 2:57:16 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
... 或者您可以通过将以下 ConnectionProvider
修改到设置中来解决这些问题(我认为不需要这样做)
.applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}
@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}
@Override
public Connection getConnection() {
return connection; // Interesting part here
}
@Override
public void closeConnection(Connection conn) throws SQLException {}
@Override
public boolean supportsAggressiveRelease() {
return true;
}
})
休眠 5.0:
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.build());
// [...] adding annotated classes to metadata here...
metadata.addAnnotatedClass(...);
SchemaExport export = new SchemaExport(
(MetadataImplementor) metadata.buildMetadata(),
connection // pre-configured Connection here
);
export.create(true, true);
休眠 4:
提醒一下,这在 Hibernate 4 中是如何工作的:
Configuration configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
// [...] adding annotated classes to metadata here...
configuration.addAnnotatedClass(...);
configuration.generateSchemaCreationScript(
Dialect.getDialect(configuration.getProperties()));
SchemaExport export = new SchemaExport(configuration, connection);
export.create(true, true);
万一使用 JPA 2.1+ - 有一种非常简单的内置可能性来生成 ddl。只需设置以下 jpa 属性,就会创建 ddl 文件。使用 spring 引导,可以使用这些特定的配置选项编写一个单独的主 class。
JPA 2.1+
javax.persistence.schema-generation.scripts.action=drop-and-create
javax.persistence.schema-generation.scripts.create-target=create.ddl
javax.persistence.schema-generation.scripts.drop-target=drop.ddl
Spring 使用 JPA 2.1+
启动
schemagenerator.properties(放入资源文件夹):
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=drop-and-create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.ddl
spring.jpa.properties.javax.persistence.schema-generation.scripts.drop-target=drop.ddl
flyway.enabled=false // in case you use flyway for db maintenance
Spring 引导架构生成器:
public class SchemaGenerator {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, new String[]{"--spring.config.name=schemagenerator"}).close();
}
}
我用 hibernate 5.4 以这种方式导出它。9.Final:
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import java.util.EnumSet;
public class ExportSchema {
public static void main(String[] args) {
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.build();
final Metadata metadata = new MetadataSources(serviceRegistry)
.addAnnotatedClass(...)
.buildMetadata();
new SchemaExport()
.setFormat(true)
.setDelimiter(";")
.setOutputFile("schema.sql")
.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata);
}
}
在 Hibernate 4.x 中,我曾经生成并导出在带注释的实体中定义的模式,如下所示(使用 Spring 在 class 路径上查找带注释的实体):
Connection connection =
DriverManager.getConnection("jdbc:h2:mem:jooq-meta-extensions", "sa", "");
Configuration configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
// [...] adding annotated classes to Configuration here...
configuration.generateSchemaCreationScript(
Dialect.getDialect(configuration.getProperties()));
SchemaExport export = new SchemaExport(configuration, connection);
export.create(true, true);
这在 Hibernate 5.0 中不再有效:
除了:
,我在 migration guide 中没有真正找到任何明显的对这一变化的引用Quite a few methods have been removed from Configuration
基于一组带注释的实体,使用 Hibernate 5.0 在现有 JDBC 连接 上生成和导出数据库 的正确方法是什么? (纯基于 JPA 的解决方案也很好)
(请注意,仅删除对 generateSchemaCreationScript()
的调用似乎可行,但我希望确保正确执行此操作)
新 SchemaExport
初始化的一个示例在 SchemaExportTask 中找到:
final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
final MetadataSources metadataSources = new MetadataSources( bsr );
final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder( bsr );
if ( configurationFile != null ) {
ssrBuilder.configure( configurationFile );
}
if ( propertiesFile != null ) {
ssrBuilder.loadProperties( propertiesFile );
}
ssrBuilder.applySettings( getProject().getProperties() );
for ( String fileName : getFiles() ) {
if ( fileName.endsWith(".jar") ) {
metadataSources.addJar( new File( fileName ) );
}
else {
metadataSources.addFile( fileName );
}
}
final StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) ssrBuilder.build();
final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder( ssr );
ClassLoaderService classLoaderService = bsr.getService( ClassLoaderService.class );
if ( implicitNamingStrategy != null ) {
metadataBuilder.applyImplicitNamingStrategy(
(ImplicitNamingStrategy) classLoaderService.classForName( implicitNamingStrategy ).newInstance()
);
}
if ( physicalNamingStrategy != null ) {
metadataBuilder.applyPhysicalNamingStrategy(
(PhysicalNamingStrategy) classLoaderService.classForName( physicalNamingStrategy ).newInstance()
);
}
return new SchemaExport( (MetadataImplementor) metadataBuilder.build() )
.setHaltOnError( haltOnError )
.setOutputFile( outputFile.getPath() )
.setDelimiter( delimiter );
当然,您可以根据自己的需要进行定制。
新的 bootstrap API 允许许多自定义,但假设您不需要这些,最短的调用看起来像这样,为服务注册表和所有设置应用默认值:
Metadata metadata = new MetadataSources()
.addAnnotatedClass( MyEntity.class )
.build();
new SchemaExport( (MetadataImplementor) metadata )
.setOutputFile( "my-statements.ddl" )
.create( Target.NONE );
更新:提供应用配置属性的示例
有几种方法可以为连接URL、方言等注入属性。例如您可以提供一个文件 hibernate.properties 或者您使用根据所需设置定制的服务注册表:
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySetting( "hibernate.connection.url", "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" )
.build();
Metadata metadata = new MetadataSources( registry )
.build();
感谢
休眠 5.2:
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.applySetting("javax.persistence.schema-generation-connection", connection)
.build());
// [...] adding annotated classes to metadata here...
metadata.addAnnotatedClass(...);
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
Hibernate 5.2(无警告):
上面会产生一些讨厌的警告,可以忽略:
Okt 20, 2016 2:57:16 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Okt 20, 2016 2:57:16 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
... 或者您可以通过将以下 ConnectionProvider
修改到设置中来解决这些问题(我认为不需要这样做)
.applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}
@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}
@Override
public Connection getConnection() {
return connection; // Interesting part here
}
@Override
public void closeConnection(Connection conn) throws SQLException {}
@Override
public boolean supportsAggressiveRelease() {
return true;
}
})
休眠 5.0:
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.build());
// [...] adding annotated classes to metadata here...
metadata.addAnnotatedClass(...);
SchemaExport export = new SchemaExport(
(MetadataImplementor) metadata.buildMetadata(),
connection // pre-configured Connection here
);
export.create(true, true);
休眠 4:
提醒一下,这在 Hibernate 4 中是如何工作的:
Configuration configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
// [...] adding annotated classes to metadata here...
configuration.addAnnotatedClass(...);
configuration.generateSchemaCreationScript(
Dialect.getDialect(configuration.getProperties()));
SchemaExport export = new SchemaExport(configuration, connection);
export.create(true, true);
万一使用 JPA 2.1+ - 有一种非常简单的内置可能性来生成 ddl。只需设置以下 jpa 属性,就会创建 ddl 文件。使用 spring 引导,可以使用这些特定的配置选项编写一个单独的主 class。
JPA 2.1+
javax.persistence.schema-generation.scripts.action=drop-and-create
javax.persistence.schema-generation.scripts.create-target=create.ddl
javax.persistence.schema-generation.scripts.drop-target=drop.ddl
Spring 使用 JPA 2.1+
启动schemagenerator.properties(放入资源文件夹):
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=drop-and-create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.ddl
spring.jpa.properties.javax.persistence.schema-generation.scripts.drop-target=drop.ddl
flyway.enabled=false // in case you use flyway for db maintenance
Spring 引导架构生成器:
public class SchemaGenerator {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, new String[]{"--spring.config.name=schemagenerator"}).close();
}
}
我用 hibernate 5.4 以这种方式导出它。9.Final:
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import java.util.EnumSet;
public class ExportSchema {
public static void main(String[] args) {
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.build();
final Metadata metadata = new MetadataSources(serviceRegistry)
.addAnnotatedClass(...)
.buildMetadata();
new SchemaExport()
.setFormat(true)
.setDelimiter(";")
.setOutputFile("schema.sql")
.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata);
}
}