use of org.hibernate.tool.hbm2ddl.SchemaExport in project webapp by elimu-ai.
the class CustomDispatcherServlet method createJpaSchemaExport.
/**
* Export the JPA database schema to a file.
*/
private void createJpaSchemaExport() {
logger.info("createJpaSchemaExport");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", ConfigHelper.getProperty("jpa.databasePlatform")).applySetting("hibernate.hbm2ddl.auto", "update").build();
MetadataSources metadataSources = (MetadataSources) new MetadataSources(serviceRegistry);
// Scan for classes annotated as JPA @Entity
ClassPathScanningCandidateComponentProvider entityScanner = new ClassPathScanningCandidateComponentProvider(true);
entityScanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
for (BeanDefinition beanDefinition : entityScanner.findCandidateComponents("ai.elimu.model")) {
logger.info("beanDefinition.getBeanClassName(): " + beanDefinition.getBeanClassName());
try {
Class<?> annotatedClass = Class.forName(beanDefinition.getBeanClassName());
logger.info("annotatedClass.getName(): " + annotatedClass.getName());
metadataSources.addAnnotatedClass(annotatedClass);
} catch (ClassNotFoundException ex) {
logger.error(ex);
}
}
Metadata metadata = metadataSources.buildMetadata();
File outputFile = new File("src/main/resources/META-INF/jpa-schema-export.sql");
if (outputFile.exists()) {
// Delete existing file content since the SchemaExport appends to existing content.
logger.info("Deleting " + outputFile.getPath());
outputFile.delete();
}
SchemaExport schemaExport = new SchemaExport();
schemaExport.setOutputFile(outputFile.getPath());
schemaExport.setDelimiter(";");
schemaExport.setFormat(true);
schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
}
use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class MixedFieldPropertyAnnotationTest method setUp.
@Before
public void setUp() {
serviceRegistry = new StandardServiceRegistryBuilder().applySetting(Environment.GLOBALLY_QUOTED_IDENTIFIERS, "false").build();
metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(MyEntity.class).buildMetadata();
System.out.println("********* Starting SchemaExport for START-UP *************************");
new SchemaExport().create(EnumSet.of(TargetType.STDOUT, TargetType.DATABASE), metadata);
System.out.println("********* Completed SchemaExport for START-UP *************************");
}
use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class QuotedTableNameSchemaUpdateTest method testSchemaUpdateWithQuotedTableName.
@Test
@TestForIssue(jiraKey = "HHH-10820")
@Skip(condition = Skip.OperatingSystem.Windows.class, message = "On Windows, MySQL is case insensitive!")
public void testSchemaUpdateWithQuotedTableName() throws Exception {
final MetadataSources metadataSources = new MetadataSources(ssr);
metadataSources.addAnnotatedClass(QuotedTable.class);
MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.validate();
new SchemaExport().setOutputFile(output.getAbsolutePath()).setFormat(false).create(EnumSet.of(TargetType.DATABASE), metadata);
new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false).execute(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata);
final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
assertThat("The update should recognize the existing table", sqlLines.isEmpty(), is(true));
new SchemaExport().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false).drop(EnumSet.of(TargetType.DATABASE), metadata);
}
use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class SchemaExportWithGlobalQuotingEnabledTest method testSchemaExport.
@Test
public void testSchemaExport() throws Exception {
SchemaExport schemaExport = new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.STDOUT, TargetType.DATABASE), metadata);
List<SQLException> exceptions = schemaExport.getExceptions();
for (SQLException exception : exceptions) {
assertThat(exception.getMessage(), exception.getSQLState(), not("42000"));
}
}
use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class InheritanceSchemaUpdateTest method testBidirectionalOneToManyReferencingRootEntity.
@Test
public void testBidirectionalOneToManyReferencingRootEntity() throws Exception {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Step.class).addAnnotatedClass(GroupStep.class).buildMetadata();
metadata.validate();
try {
try {
new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
} finally {
new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
}
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
Aggregations