Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SchemaGeneratorDemo
import com.github.fluent.hibernate.cfg.scanner.EntityScanner;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.PostgreSQL94Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.hql.spi.QueryTranslatorFactory;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import py.com.ping.administracionBase.jpa.BswEmpresas;
import py.com.ping.cuentaCobrar.jpa.CcwCondicionesVentas;
import py.com.ping.fuerzaVentas.jpa.FvwGruposComision;
import py.com.ping.pasarelas.model.PsrPago;
import py.com.ping.servicios.jpa.CcwClientes;
import py.com.ping.stock.jpa.StwArticulos;
import py.com.ping.ventas.jpa.VtwComprobantesCabecera;
import java.io.File;
import java.util.Collections;
import java.util.EnumSet;
import static org.hibernate.tool.schema.TargetType.SCRIPT;
import static org.hibernate.tool.schema.TargetType.STDOUT;
// Hibernate 5.
public class SchemaGeneratorDemo {
private static final String SCRIPT_FILE = "exportScript.sql";
public static String toSql(SessionFactory sf, String hqlQueryText) {
final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
final SessionFactoryImplementor factory = (SessionFactoryImplementor) sf;
final QueryTranslator translator = translatorFactory.createQueryTranslator(hqlQueryText,
hqlQueryText,
Collections.EMPTY_MAP,
factory,
null);
translator.compile(Collections.EMPTY_MAP, false);
return translator.getSQLString();
}
private static SchemaExport getSchemaExport() {
SchemaExport export = new SchemaExport();
// Script file.
File outputFile = new File(SCRIPT_FILE);
String outputFilePath = outputFile.getAbsolutePath();
System.out.println("Export file: " + outputFilePath);
export.setDelimiter(";");
export.setOutputFile(outputFilePath);
// No Stop if Error
export.setHaltOnError(true);
//
return export;
}
public static void dropDataBase(SchemaExport export, Metadata metadata) {
// TargetType.DATABASE - Execute on Databse
// TargetType.SCRIPT - Write Script file.
// TargetType.STDOUT - Write log to Console.
EnumSet<TargetType> targetTypes =
EnumSet.of(
TargetType.DATABASE,
TargetType.SCRIPT,
STDOUT
);
export.drop(targetTypes, metadata);
}
public static void createDataBase(SchemaExport export, Metadata metadata) {
// TargetType.DATABASE - Execute on Databse
// TargetType.SCRIPT - Write Script file.
// TargetType.STDOUT - Write log to Console.
EnumSet<TargetType> targetTypes = EnumSet.of(
// TargetType.DATABASE,
TargetType.SCRIPT,
STDOUT
);
SchemaExport.Action action = SchemaExport.Action.CREATE;
//
export.execute(targetTypes, action, metadata);
System.out.println("Export OK");
}
public static void main(String[] args) {
// Using Oracle Database.
Configuration cfg = new Configuration()
.setProperty(AvailableSettings.DIALECT, PostgreSQL94Dialect.class.getName())
// .setProperty(AvailableSettings.HBM2DDL_AUTO, Action.CREATE.toString())
.setProperty(AvailableSettings.SHOW_SQL, "true");
// Create the ServiceRegistry from hibernate-xxx.cfg.xml
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.build();
// Create a metadata sources using the specified service registry.
MetadataSources sources = new MetadataSources(serviceRegistry);
EntityScanner.scanPackages(CcwClientes.class.getPackage().getName(),
PsrPago.class.getPackage().getName(),
BswEmpresas.class.getPackage().getName(),
VtwComprobantesCabecera.class.getPackage().getName(),
CcwCondicionesVentas.class.getPackage().getName(),
StwArticulos.class.getPackage().getName(),
FvwGruposComision.class.getPackage().getName()
).result().forEach(sources::addAnnotatedClass);
Metadata metadata = sources
.getMetadataBuilder()
.build();
System.out.println("Print ddl");
new SchemaExport()
.setFormat(true)
.setOutputFile(SCRIPT_FILE)
.createOnly(EnumSet.of(SCRIPT), metadata);
// System.out.println("Print queries");
// SessionFactory sf = cfg.buildSessionFactory();
// SessionFactory sf = metadata.buildSessionFactory();
// System.out.println(toSql(sf, "SELECT p FROM PsrPago p"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment