问题描述
在Hibernate 4.x中,我用来生成和导出注释实体中定义的模式,如下所示(使用Spring在类路径上查找注释实体):
In Hibernate 4.x, I used to generate and export the schema as defined in annotated entities as follows (using Spring to find annotated entities on the class path):
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中不再有效:
This no longer works in Hibernate 5.0:
- 中对此更改的任何明显引用,除了:
I didn't really find any obvious references to this change in the migration guide apart from:
在基于一组注解实体的Hibernate 5.0中,在现有JDBC连接上生成和导出数据库的正确方法是什么?
What is the correct way to generate and export a database on an existing JDBC connection with Hibernate 5.0 based on a set of annotated entities? (Pure JPA-based solutions are fine, too)
(注意,只是删除对
generateSchemaCreationScript()的调用
似乎工作,但我更喜欢确保得到这个正确)(note, just removing the call to
generateSchemaCreationScript()
seems to work, but I would prefer to be sure to get this right)推荐答案
http://stackoverflow.com/a/32178192/521799\"> Vlad 和,我设法找到我的方式通过新的配置API生成等效的导出逻辑与以下。当然,历史表明这个API会再次中断,因此请务必选择合适的版本:
Thanks to the answers by Vlad and Gunnar, I've managed to find my way through the new configuration API to produce the equivalent export logic with the following. Of course, history shows that this API will break again, so make sure to choose the appropriate version:
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(无警告):
以上会产生一些令人讨厌的警告,可以忽略:
Hibernate 5.2 (without warnings):
The above will produce some nasty warnings, which can either be ignored:
...或者通过将以下
ConnectionProvider
... or you work around them by hacking the following
ConnectionProvider
into the settings (it shouldn't be required in my opinion).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; } })
Hibernate 5.0:
Hibernate 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);
Hibernate 4:
,下面是在Hibernate 4中的工作方式:
Hibernate 4:
As a reminder, here's how this worked in 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);
这篇关于Configuration.generateSchemaCreationScript()在Hibernate 5中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!