我最近将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);
}
我还看到了与此问题有关的其他问题,但是不够具体,无法帮助我重写此功能。
答案 0 :(得分:0)
这是我所做的并且有效的方法:
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);
}