将 Hibernate 升级到 5.4.1 后如何导出模式?
How do I export schema after upgrading Hibernate to 5.4.1?
我最近将 Hibernate 从 4.3.7 更新到 5.4.1,并且 SchemaExport
API 自 5.1 以来发生了变化。此代码现在显示编译问题(在 SchemaExport
构造函数和 execute
方法上)。
/**
* Method to generate a SQL script which aim is to create SQL tables for the
* entities indicated as parameters.
*/
private void generateScript(Class<?>... classes) {
Configuration configuration = new Configuration();
configuration.setProperty(Environment.DIALECT, entityManagerFactory.getProperties().get(DIALECT_PROPERTY).toString());
for (Class<?> entityClass : classes) {
configuration.addAnnotatedClass(entityClass);
}
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.setDelimiter(SCRIPT_DELIMITER);
schemaExport.setOutputFile(getScriptPath());
schemaExport.setFormat(true);
boolean consolePrint = false;
boolean exportInDatabase = false;
schemaExport.execute(consolePrint, exportInDatabase, false, true);
}
我看到过与此问题相关的其他问题,但不够具体,无法帮助我重写此函数。
这是我所做的并且有效:
private void genererScript(Class<?>... classes) {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.applySetting(Environment.DIALECT, entityManagerFactory.getProperties().get(DIALECT_PROPERTY).toString())
.build();
MetadataSources sources = new MetadataSources( standardRegistry );
for (Class<?> entityClass : classes) {
sources.addAnnotatedClass(entityClass);
}
MetadataImplementor metadata = (MetadataImplementor) sources
.getMetadataBuilder()
.build();
EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
try {
Files.delete(Paths.get(getScriptPath()));
} catch (IOException e) {
/*
* The file did not exist...
* we do nothing.
*/
}
SchemaExport schemaExport = new SchemaExport();
schemaExport.setDelimiter(SCRIPT_DELIMITER);
schemaExport.setOutputFile(getScriptPath());
schemaExport.setFormat(true);
schemaExport.createOnly(targetTypes, metadata);
}
我最近将 Hibernate 从 4.3.7 更新到 5.4.1,并且 SchemaExport
API 自 5.1 以来发生了变化。此代码现在显示编译问题(在 SchemaExport
构造函数和 execute
方法上)。
/**
* Method to generate a SQL script which aim is to create SQL tables for the
* entities indicated as parameters.
*/
private void generateScript(Class<?>... classes) {
Configuration configuration = new Configuration();
configuration.setProperty(Environment.DIALECT, entityManagerFactory.getProperties().get(DIALECT_PROPERTY).toString());
for (Class<?> entityClass : classes) {
configuration.addAnnotatedClass(entityClass);
}
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.setDelimiter(SCRIPT_DELIMITER);
schemaExport.setOutputFile(getScriptPath());
schemaExport.setFormat(true);
boolean consolePrint = false;
boolean exportInDatabase = false;
schemaExport.execute(consolePrint, exportInDatabase, false, true);
}
我看到过与此问题相关的其他问题,但不够具体,无法帮助我重写此函数。
这是我所做的并且有效:
private void genererScript(Class<?>... classes) {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.applySetting(Environment.DIALECT, entityManagerFactory.getProperties().get(DIALECT_PROPERTY).toString())
.build();
MetadataSources sources = new MetadataSources( standardRegistry );
for (Class<?> entityClass : classes) {
sources.addAnnotatedClass(entityClass);
}
MetadataImplementor metadata = (MetadataImplementor) sources
.getMetadataBuilder()
.build();
EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
try {
Files.delete(Paths.get(getScriptPath()));
} catch (IOException e) {
/*
* The file did not exist...
* we do nothing.
*/
}
SchemaExport schemaExport = new SchemaExport();
schemaExport.setDelimiter(SCRIPT_DELIMITER);
schemaExport.setOutputFile(getScriptPath());
schemaExport.setFormat(true);
schemaExport.createOnly(targetTypes, metadata);
}