use of org.hibernate.boot.MetadataSources in project cas by apereo.
the class GenerateDdlCommand method generate.
/**
* Generate.
*
* @param file the file
* @param dialect the dialect
* @param delimiter the delimiter
* @param pretty the pretty
* @param dropSchema the drop schema
* @param createSchema the create schema
* @param haltOnError the halt on error
*/
@CliCommand(value = "generate-ddl", help = "Generate database DDL scripts")
public void generate(@CliOption(key = { "file" }, help = "DDL file to contain to generated script", specifiedDefaultValue = "/etc/cas/config/cas-db-schema.sql", unspecifiedDefaultValue = "/etc/cas/config/cas-db-schema.sql", optionContext = "DDL file to contain to generated script") final String file, @CliOption(key = { "dialect" }, help = "Database dialect class", specifiedDefaultValue = "HSQL", unspecifiedDefaultValue = "HSQL", optionContext = "Database dialect class") final String dialect, @CliOption(key = { "delimiter" }, help = "Delimiter to use for separation of statements when generating SQL", specifiedDefaultValue = ";", unspecifiedDefaultValue = ";", optionContext = "Delimiter to use for separation of statements when generating SQL") final String delimiter, @CliOption(key = { "pretty" }, help = "Format DDL scripts and pretty-print the output", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Format DDL scripts and pretty-print the output") final boolean pretty, @CliOption(key = { "dropSchema" }, help = "Generate DROP SQL statements in the DDL", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Generate DROP statements in the DDL") final boolean dropSchema, @CliOption(key = { "createSchema" }, help = "Generate DROP SQL statements in the DDL", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Generate CREATE SQL statements in the DDL") final boolean createSchema, @CliOption(key = { "haltOnError" }, help = "Halt if an error occurs during the generation process", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Halt if an error occurs during the generation process") final boolean haltOnError) {
final String dialectName = DIALECTS_MAP.getOrDefault(dialect.trim().toUpperCase(), dialect);
LOGGER.info("Using database dialect class [{}]", dialectName);
if (!dialectName.contains(".")) {
LOGGER.warn("Dialect name must be a fully qualified class name. Supported dialects by default are [{}] " + "or you may specify the dialect class directly", DIALECTS_MAP.keySet());
return;
}
final StandardServiceRegistryBuilder svcRegistry = new StandardServiceRegistryBuilder();
if (StringUtils.isNotBlank(dialectName)) {
svcRegistry.applySetting(AvailableSettings.DIALECT, dialect);
}
final MetadataSources metadata = new MetadataSources(svcRegistry.build());
REFLECTIONS.getTypesAnnotatedWith(MappedSuperclass.class).forEach(metadata::addAnnotatedClass);
REFLECTIONS.getTypesAnnotatedWith(Entity.class).forEach(metadata::addAnnotatedClass);
final SchemaExport export = new SchemaExport();
export.setDelimiter(delimiter);
export.setOutputFile(file);
export.setFormat(pretty);
export.setHaltOnError(haltOnError);
export.setManageNamespaces(true);
final SchemaExport.Action action;
if (createSchema && dropSchema) {
action = SchemaExport.Action.BOTH;
} else if (createSchema) {
action = SchemaExport.Action.CREATE;
} else if (dropSchema) {
action = SchemaExport.Action.DROP;
} else {
action = SchemaExport.Action.NONE;
}
LOGGER.info("Exporting Database DDL to [{}] using dialect [{}] with export type set to [{}]", file, dialect, action);
export.execute(EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT), SchemaExport.Action.BOTH, metadata.buildMetadata());
LOGGER.info("Database DDL is exported to [{}]", file);
}
use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.
the class MetadataHelper method getMetadataSources.
public static MetadataSources getMetadataSources(Configuration configuration) {
MetadataSources result = null;
Field metadataSourcesField = getField("metadataSources", configuration);
if (metadataSourcesField != null) {
try {
metadataSourcesField.setAccessible(true);
result = (MetadataSources) metadataSourcesField.get(configuration);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
if (result == null) {
result = new MetadataSources();
}
return result;
}
use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.
the class ClassMetadataFacadeTest method createSampleEntityPersister.
private TestEntityPersister createSampleEntityPersister() {
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
ServiceRegistry serviceRegistry = builder.build();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
RootClass rc = new RootClass(null);
Table t = new Table("foobar");
rc.setTable(t);
Column c = new Column("foo");
t.addColumn(c);
ArrayList<Column> keyList = new ArrayList<>();
keyList.add(c);
t.createUniqueKey(keyList);
SimpleValue sv = new SimpleValue(metadata);
sv.setNullValue("null");
sv.setTypeName(Integer.class.getName());
sv.addColumn(c);
rc.setEntityName("foobar");
rc.setIdentifier(sv);
return new TestEntityPersister(rc, sfi, metadata);
}
use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.
the class TypeFacadeTest method testIsComponentType.
@Test
public void testIsComponentType() {
IType typeFacade = null;
ClassType classType = new ClassType();
typeFacade = FACADE_FACTORY.createType(classType);
Assert.assertFalse(typeFacade.isComponentType());
MetadataBuildingOptions mdbo = new MetadataBuilderImpl.MetadataBuildingOptionsImpl(new StandardServiceRegistryBuilder().build());
MetadataImplementor mdi = (MetadataImplementor) new MetadataBuilderImpl(new MetadataSources()).build();
ComponentType componentType = new ComponentType(null, new ComponentMetamodel(new Component(mdi, new RootClass(null)), mdbo));
typeFacade = FACADE_FACTORY.createType(componentType);
Assert.assertTrue(typeFacade.isComponentType());
}
use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.
the class ConfigurationFacadeTest method testAddFile.
@Test
public void testAddFile() throws Exception {
File testFile = File.createTempFile("test", "hbm.xml");
PrintWriter printWriter = new PrintWriter(testFile);
printWriter.write(TEST_HBM_XML_STRING);
printWriter.close();
MetadataSources metadataSources = MetadataHelper.getMetadataSources(configuration);
Assert.assertTrue(metadataSources.getXmlBindings().isEmpty());
Assert.assertSame(configurationFacade, configurationFacade.addFile(testFile));
Assert.assertFalse(metadataSources.getXmlBindings().isEmpty());
Binding<?> binding = metadataSources.getXmlBindings().iterator().next();
Assert.assertEquals(testFile.getAbsolutePath(), binding.getOrigin().getName());
Assert.assertTrue(testFile.delete());
}
Aggregations