Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Local MySQL Database
- spring.datasource.url=jdbc:mysql://localhost:3306/paddb
- spring.datasource.driverClassName=com.mysql.jdbc.Driver
- spring.datasource.username=pad
- spring.datasource.password=pegus
- spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
- spring.jpa.hibernate.ddl-auto=validate
- spring.datasource.url=jdbc:h2:mem:paddb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
- spring.datasource.driverClassName=org.h2.Driver
- spring.datasource.username=sa
- spring.datasource.password=
- spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
- spring.jpa.hibernate.ddl-auto=create-drop
- package com.pegusapps.dashboard.infrastructure;
- import org.hibernate.boot.MetadataSources;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
- import org.hibernate.boot.spi.MetadataImplementor;
- import org.hibernate.cfg.AvailableSettings;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- import org.reflections.Reflections;
- import javax.persistence.Entity;
- import javax.persistence.MappedSuperclass;
- import java.io.File;
- public class Hibernate5DDLExporter {
- private String dialect = "org.hibernate.dialect.MySQLDialect";
- private String[] entityPackages;
- public Hibernate5DDLExporter dialect(String dialect) {
- this.dialect = dialect;
- return this;
- }
- public Hibernate5DDLExporter entities(String... entityPackage) {
- this.entityPackages = entityPackage;
- return this;
- }
- public Hibernate5DDLExporter schemaExport(String fileName, String targetDirectory) throws Exception {
- if (entityPackages == null && entityPackages.length == 0) {
- System.out.println("Not packages selected");
- System.exit(0);
- }
- File exportFile = createExportFileAndMakeDirectory(fileName, targetDirectory);
- ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
- .applySetting(AvailableSettings.DIALECT, dialect)
- .build();
- MetadataImplementor metadata = (MetadataImplementor) mapAnnotatedClasses(serviceRegistry).buildMetadata();
- SchemaExport schemaExport = new SchemaExport(metadata);
- schemaExport.setOutputFile(exportFile.getAbsolutePath());
- schemaExport.setDelimiter(";");
- schemaExport.setFormat(true);
- schemaExport.execute(true, false, false, true);
- ((StandardServiceRegistryImpl) serviceRegistry).destroy();
- System.out.println(exportFile.getAbsolutePath());
- return this;
- }
- private File createExportFileAndMakeDirectory(String fileName, String targetDirectory) {
- File exportFile;
- if (targetDirectory != null) {
- final File directory = new File(targetDirectory);
- directory.mkdirs();
- exportFile = new File(directory, fileName);
- } else {
- exportFile = new File(fileName);
- }
- return exportFile;
- }
- private MetadataSources mapAnnotatedClasses(ServiceRegistry serviceRegistry) {
- MetadataSources sources = new MetadataSources(serviceRegistry);
- final Reflections reflections = new Reflections((Object) entityPackages);
- for (final Class<?> mappedSuperClass : reflections.getTypesAnnotatedWith(MappedSuperclass.class)) {
- sources.addAnnotatedClass(mappedSuperClass);
- System.out.println("Mapped = " + mappedSuperClass.getName());
- }
- for (final Class<?> entityClasses : reflections.getTypesAnnotatedWith(Entity.class)) {
- sources.addAnnotatedClass(entityClasses);
- System.out.println("Mapped = " + entityClasses.getName());
- }
- return sources;
- }
- public static Hibernate5DDLExporter instance() {
- return new Hibernate5DDLExporter();
- }
- public static void main(String[] args) throws Exception {
- Hibernate5DDLExporter.instance()
- .entities(
- "com.pegusapps.dashboard.user",
- "com.pegusapps.dashboard.member",
- "com.pegusapps.dashboard.project"
- )
- .schemaExport("create.sql", "build");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement