Search in sources :

Example 46 with SchemaExport

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);
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) ServiceRegistry(org.hibernate.service.ServiceRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) File(java.io.File) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 47 with SchemaExport

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 *************************");
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Before(org.junit.Before)

Example 48 with SchemaExport

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);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) Skip(org.hibernate.testing.Skip) TestForIssue(org.hibernate.testing.TestForIssue)

Example 49 with SchemaExport

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"));
    }
}
Also used : SQLException(java.sql.SQLException) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Example 50 with SchemaExport

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);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Aggregations

SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)90 Test (org.junit.Test)45 MetadataSources (org.hibernate.boot.MetadataSources)42 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)28 ISchemaExport (org.jboss.tools.hibernate.runtime.spi.ISchemaExport)27 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)22 TestForIssue (org.hibernate.testing.TestForIssue)13 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)12 Configuration (org.hibernate.cfg.Configuration)12 Connection (java.sql.Connection)10 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)10 After (org.junit.After)10 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)9 File (java.io.File)8 Before (org.junit.Before)8 HashMap (java.util.HashMap)5 Metadata (org.hibernate.boot.Metadata)5 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)5 SQLException (java.sql.SQLException)4 Entity (javax.persistence.Entity)4