Revision ae8047fb
Added by Andreas Müller almost 3 years ago
cdmlib-test/src/main/java/eu/etaxonomy/cdm/test/unitils/DdlCreator.java | ||
---|---|---|
8 | 8 |
*/ |
9 | 9 |
package eu.etaxonomy.cdm.test.unitils; |
10 | 10 |
|
11 |
import java.io.File; |
|
12 |
import java.io.IOException; |
|
11 | 13 |
import java.util.EnumSet; |
12 | 14 |
|
13 | 15 |
import org.hibernate.boot.Metadata; |
... | ... | |
21 | 23 |
import org.hibernate.dialect.H2CorrectedDialectTest; |
22 | 24 |
import org.hibernate.tool.hbm2ddl.SchemaExport; |
23 | 25 |
import org.hibernate.tool.schema.TargetType; |
26 |
import org.springframework.core.io.ClassPathResource; |
|
24 | 27 |
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
25 | 28 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder; |
26 | 29 |
|
... | ... | |
45 | 48 |
|
46 | 49 |
public static void main(String[] args) { |
47 | 50 |
try { |
48 |
new DdlCreator().execute2(H2CorrectedDialectTest.class, "h2");
|
|
51 |
new DdlCreator().execute(H2CorrectedDialectTest.class, "h2"); |
|
49 | 52 |
} catch (Exception e) { |
50 | 53 |
e.printStackTrace(); |
51 | 54 |
} |
52 | 55 |
} |
53 | 56 |
|
54 |
public void execute2(Class<?> dialect, String lowerCaseDialectName){
|
|
57 |
public void execute(Class<?> dialect, String lowerCaseDialectName){ |
|
55 | 58 |
|
56 |
String outputFileName = String.format("%s.%s.%s ", new Object[] {"001-cdm", lowerCaseDialectName, "sql" }); |
|
57 |
String templateFile = "dbscripts/" + outputFileName + "-template"; |
|
58 |
String outputPath = "src/main/resources/dbscripts/" + outputFileName; |
|
59 |
try { |
|
60 |
String outputFileName = String.format("%s.%s.%s", new Object[] {"001-cdm", lowerCaseDialectName, "sql" }); |
|
61 |
String outputFileClassPath = "dbscripts/" + outputFileName; |
|
62 |
// String templateFileName = outputFileName + "-template"; |
|
63 |
// String templateFileClassPath = "dbscripts/" + templateFileName; |
|
59 | 64 |
|
60 |
// String classPath = "eu/etaxonomy/cdm/hibernate.cfg.xml"; |
|
61 |
// ClassPathResource resource = new ClassPathResource(classPath); |
|
62 |
// File configurationFile = resource.getFile(); |
|
65 |
ClassPathResource resource = new ClassPathResource(outputFileClassPath); |
|
66 |
File folder = resource.getFile().getParentFile(); |
|
67 |
String outputPath = folder.getCanonicalPath()+File.separator + outputFileName; |
|
68 |
// String templatePath = folder.getCanonicalPath()+File.separator + templateFileName; |
|
69 |
System.out.println(outputPath); |
|
70 |
// System.out.println(templatePath); |
|
63 | 71 |
|
64 |
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder() |
|
72 |
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder()
|
|
65 | 73 |
.applySetting(AvailableSettings.DIALECT, dialect.getCanonicalName()) // dialect |
66 |
//alternative: .loadProperties(propertiesFile) |
|
67 |
//alternative2: .configure("hibernate.cfg.xml") |
|
68 |
//alternative3: .configure(configurationFile) |
|
69 |
//SCRIPT_THEN_METADATA does not work with SchemaExport: |
|
70 |
// .applySetting(AvailableSettings.HBM2DDL_CREATE_SCRIPT_SOURCE, templateFile) |
|
71 |
// .applySetting(AvailableSettings.HBM2DDL_CREATE_SOURCE, SourceType.SCRIPT_THEN_METADATA); |
|
74 |
// .applySetting(AvailableSettings.HBM2DDL_CREATE_SCRIPT_SOURCE, resource.getURL()) |
|
72 | 75 |
; |
73 | 76 |
|
74 |
StandardServiceRegistry serviceRegistry = registryBuilder.build(); |
|
75 |
|
|
76 |
MetadataSources metadataSources = new MetadataSources(serviceRegistry); |
|
77 |
|
|
78 |
|
|
79 |
PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver(); |
|
80 |
new LocalSessionFactoryBuilder(null, resourceLoader, metadataSources).scanPackages("eu.etaxonomy.cdm.model"); |
|
81 |
|
|
82 |
// PhysicalNamingStrategy namingStrategy = new PhysicalNamingStrategyStandardImpl(); |
|
83 |
ImplicitNamingStrategyComponentPathImpl namingStrategy = new ImplicitNamingStrategyComponentPathImpl(); |
|
84 |
PhysicalNamingStrategy physicalNamingStrategy = new UpperCasePhysicalNamingStrategyStandardImpl(); |
|
85 |
|
|
86 |
Metadata metadata = metadataSources.getMetadataBuilder(serviceRegistry) |
|
87 |
.applyImplicitSchemaName("public") |
|
88 |
.applyImplicitNamingStrategy(namingStrategy) |
|
89 |
.applyPhysicalNamingStrategy(physicalNamingStrategy) |
|
90 |
.build(); |
|
91 |
|
|
92 |
EnumSet<TargetType> targetTypes = EnumSet.of(/*TargetType.STDOUT, */TargetType.SCRIPT); |
|
93 |
new SchemaExport() |
|
94 |
.setFormat(true) |
|
95 |
.setDelimiter(";") |
|
96 |
// .setImportFiles(templateFile) |
|
97 |
.setOutputFile(outputPath) |
|
98 |
.createOnly(targetTypes, metadata); |
|
99 |
|
|
100 |
((StandardServiceRegistryImpl) serviceRegistry).destroy(); |
|
77 |
StandardServiceRegistry serviceRegistry = registryBuilder.build(); |
|
78 |
|
|
79 |
MetadataSources metadataSources = new MetadataSources(serviceRegistry); |
|
80 |
|
|
81 |
//model scan |
|
82 |
PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver(); |
|
83 |
new LocalSessionFactoryBuilder(null, resourceLoader, metadataSources).scanPackages("eu.etaxonomy.cdm.model"); |
|
84 |
|
|
85 |
//metadata |
|
86 |
ImplicitNamingStrategyComponentPathImpl namingStrategy = new ImplicitNamingStrategyComponentPathImpl(); |
|
87 |
PhysicalNamingStrategy physicalNamingStrategy = new UpperCasePhysicalNamingStrategyStandardImpl(); |
|
88 |
Metadata metadata = metadataSources.getMetadataBuilder(serviceRegistry) |
|
89 |
.applyImplicitSchemaName("public") |
|
90 |
.applyImplicitNamingStrategy(namingStrategy) |
|
91 |
.applyPhysicalNamingStrategy(physicalNamingStrategy) |
|
92 |
.build(); |
|
93 |
|
|
94 |
//export |
|
95 |
EnumSet<TargetType> targetTypes = EnumSet.of(/*TargetType.STDOUT, */TargetType.SCRIPT); |
|
96 |
new SchemaExport() |
|
97 |
.setFormat(true) |
|
98 |
.setDelimiter(";") |
|
99 |
// .setImportFiles(templatePath) |
|
100 |
.setOutputFile(outputPath) |
|
101 |
.createOnly(targetTypes, metadata); |
|
102 |
|
|
103 |
((StandardServiceRegistryImpl) serviceRegistry).destroy(); |
|
104 |
} catch (IOException e) { |
|
105 |
e.printStackTrace(); |
|
106 |
return; |
|
107 |
} |
|
101 | 108 |
|
102 | 109 |
//approaches for JPA and eclipselink can be found here: https://stackoverflow.com/questions/297438/auto-generate-data-schema-from-jpa-annotated-entity-classes; |
103 | 110 |
} |
Also available in: Unified diff
ref #6714 fix ddl creation with template