Advertisement
Guest User

Untitled

a guest
Apr 9th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. #Local MySQL Database
  2. spring.datasource.url=jdbc:mysql://localhost:3306/paddb
  3. spring.datasource.driverClassName=com.mysql.jdbc.Driver
  4. spring.datasource.username=pad
  5. spring.datasource.password=pegus
  6. spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
  7. spring.jpa.hibernate.ddl-auto=validate
  8.  
  9. spring.datasource.url=jdbc:h2:mem:paddb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
  10. spring.datasource.driverClassName=org.h2.Driver
  11. spring.datasource.username=sa
  12. spring.datasource.password=
  13. spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  14. spring.jpa.hibernate.ddl-auto=create-drop
  15.  
  16.  
  17.  
  18. package com.pegusapps.dashboard.infrastructure;
  19.  
  20. import org.hibernate.boot.MetadataSources;
  21. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  22. import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
  23. import org.hibernate.boot.spi.MetadataImplementor;
  24. import org.hibernate.cfg.AvailableSettings;
  25. import org.hibernate.service.ServiceRegistry;
  26. import org.hibernate.tool.hbm2ddl.SchemaExport;
  27. import org.reflections.Reflections;
  28.  
  29. import javax.persistence.Entity;
  30. import javax.persistence.MappedSuperclass;
  31. import java.io.File;
  32.  
  33. public class Hibernate5DDLExporter {
  34.  
  35.     private String dialect = "org.hibernate.dialect.MySQLDialect";
  36.     private String[] entityPackages;
  37.  
  38.     public Hibernate5DDLExporter dialect(String dialect) {
  39.         this.dialect = dialect;
  40.         return this;
  41.     }
  42.  
  43.     public Hibernate5DDLExporter entities(String... entityPackage) {
  44.         this.entityPackages = entityPackage;
  45.         return this;
  46.     }
  47.  
  48.     public Hibernate5DDLExporter schemaExport(String fileName, String targetDirectory) throws Exception {
  49.         if (entityPackages == null && entityPackages.length == 0) {
  50.             System.out.println("Not packages selected");
  51.             System.exit(0);
  52.         }
  53.         File exportFile = createExportFileAndMakeDirectory(fileName, targetDirectory);
  54.  
  55.         ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  56.                 .applySetting(AvailableSettings.DIALECT, dialect)
  57.                 .build();
  58.  
  59.         MetadataImplementor metadata = (MetadataImplementor) mapAnnotatedClasses(serviceRegistry).buildMetadata();
  60.  
  61.         SchemaExport schemaExport = new SchemaExport(metadata);
  62.         schemaExport.setOutputFile(exportFile.getAbsolutePath());
  63.         schemaExport.setDelimiter(";");
  64.         schemaExport.setFormat(true);
  65.         schemaExport.execute(true, false, false, true);
  66.         ((StandardServiceRegistryImpl) serviceRegistry).destroy();
  67.  
  68.         System.out.println(exportFile.getAbsolutePath());
  69.  
  70.         return this;
  71.  
  72.     }
  73.  
  74.     private File createExportFileAndMakeDirectory(String fileName, String targetDirectory) {
  75.         File exportFile;
  76.         if (targetDirectory != null) {
  77.             final File directory = new File(targetDirectory);
  78.             directory.mkdirs();
  79.             exportFile = new File(directory, fileName);
  80.         } else {
  81.             exportFile = new File(fileName);
  82.         }
  83.         return exportFile;
  84.     }
  85.  
  86.     private MetadataSources mapAnnotatedClasses(ServiceRegistry serviceRegistry) {
  87.         MetadataSources sources = new MetadataSources(serviceRegistry);
  88.  
  89.         final Reflections reflections = new Reflections((Object) entityPackages);
  90.         for (final Class<?> mappedSuperClass : reflections.getTypesAnnotatedWith(MappedSuperclass.class)) {
  91.             sources.addAnnotatedClass(mappedSuperClass);
  92.             System.out.println("Mapped = " + mappedSuperClass.getName());
  93.         }
  94.         for (final Class<?> entityClasses : reflections.getTypesAnnotatedWith(Entity.class)) {
  95.             sources.addAnnotatedClass(entityClasses);
  96.             System.out.println("Mapped = " + entityClasses.getName());
  97.         }
  98.         return sources;
  99.     }
  100.  
  101.     public static Hibernate5DDLExporter instance() {
  102.         return new Hibernate5DDLExporter();
  103.     }
  104.  
  105.     public static void main(String[] args) throws Exception {
  106.         Hibernate5DDLExporter.instance()
  107.                 .entities(
  108.                         "com.pegusapps.dashboard.user",
  109.                         "com.pegusapps.dashboard.member",
  110.                         "com.pegusapps.dashboard.project"
  111.                 )
  112.                 .schemaExport("create.sql", "build");
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement