Project

General

Profile

Download (4.85 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.test.unitils;
10

    
11
import java.io.File;
12
import java.io.IOException;
13
import java.util.EnumSet;
14

    
15
import org.hibernate.boot.Metadata;
16
import org.hibernate.boot.MetadataSources;
17
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
18
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
19
import org.hibernate.boot.registry.StandardServiceRegistry;
20
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
21
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
22
import org.hibernate.cfg.AvailableSettings;
23
import org.hibernate.dialect.H2CorrectedDialectTest;
24
import org.hibernate.tool.hbm2ddl.SchemaExport;
25
import org.hibernate.tool.schema.TargetType;
26
import org.springframework.core.io.ClassPathResource;
27
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
28
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
29

    
30
import eu.etaxonomy.cdm.persistence.hibernate.UpperCasePhysicalNamingStrategyStandardImpl;
31

    
32
/**
33
 *
34
 * This class may help to create your DDL file.
35
 * However, it does not support Auditing table yet as they are (maybe) not supported
36
 * by Hibernate 4 hbm2dll.
37
 * It is also unclear if the antrun plugin supports envers in hibernate 4. I wasn't successful with it.
38
 * http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch15.html#envers-generateschema
39
 *
40
 * Also the result needs to be changed to uppercase and some _uniquekey statements need to be replaced as they are not
41
 * unique themselves.
42
 *
43
 * The result is stored in a file "new-cdm.h2.sql" in the root directory and is written to the console.
44
 *
45
 * @author a.mueller
46
 */
47
public class DdlCreator {
48

    
49
	public static void main(String[] args) {
50
		try {
51
		    System.setSecurityManager(null); //avoids security exception when started by ant (problem is the jmx server registration by log4j2, similar issue is described at https://stackoverflow.com/questions/12195868/java-security-accesscontrolexception-when-using-ant-but-runs-ok-when-invoking-j )
52
			new DdlCreator().execute(H2CorrectedDialectTest.class, "h2");
53
		} catch (Exception e) {
54
			e.printStackTrace();
55
		}
56
	}
57

    
58
    public void execute(Class<?> dialect, String lowerCaseDialectName){
59

    
60
        try {
61
            String fileName = String.format("%s.%s.%s", new Object[] {"001-cdm", lowerCaseDialectName, "sql" });
62
            String outputFileClassPath = "dbscripts/" + fileName;
63

    
64
            ClassPathResource resource = new ClassPathResource(outputFileClassPath);
65
            File folder = resource.getFile().getParentFile();
66
            String outputPath = folder.getCanonicalPath()+File.separator + fileName;
67

    
68
            StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder()
69
                .applySetting(AvailableSettings.DIALECT, dialect.getCanonicalName())  // dialect
70
//                .applySetting(AvailableSettings.HBM2DDL_CREATE_SCRIPT_SOURCE, resource.getURL())  //does not have the expected effect
71
                ;
72

    
73
            StandardServiceRegistry serviceRegistry = registryBuilder.build();
74

    
75
            MetadataSources metadataSources = new MetadataSources(serviceRegistry);
76

    
77
            //model scan
78
            PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
79
            new LocalSessionFactoryBuilder(null, resourceLoader, metadataSources).scanPackages("eu.etaxonomy.cdm.model");
80

    
81
            //metadata
82
            ImplicitNamingStrategyComponentPathImpl namingStrategy = new ImplicitNamingStrategyComponentPathImpl();
83
            PhysicalNamingStrategy physicalNamingStrategy = new UpperCasePhysicalNamingStrategyStandardImpl();
84
            Metadata metadata = metadataSources.getMetadataBuilder()
85
                    .applyImplicitSchemaName("public")
86
                    .applyImplicitNamingStrategy(namingStrategy)
87
                    .applyPhysicalNamingStrategy(physicalNamingStrategy)
88
                    .build();
89

    
90
            //export
91
            EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
92
            new SchemaExport()
93
                .setFormat(true)
94
                .setDelimiter(";")
95
//                .setImportFiles(templatePath)  //does not have the expected effect
96
                .setOutputFile(outputPath)
97
                .createOnly(targetTypes, metadata);
98

    
99
            ((StandardServiceRegistryImpl) serviceRegistry).destroy();
100
        } catch (IOException e) {
101
            e.printStackTrace();
102
            return;
103
        }
104

    
105
        //approaches for JPA and eclipselink can be found here: https://stackoverflow.com/questions/297438/auto-generate-data-schema-from-jpa-annotated-entity-classes;
106
    }
107
}
(2-2/3)