use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class SchemaExportTest method testGenerateDdlToFile.
@Test
public void testGenerateDdlToFile() {
final SchemaExport schemaExport = new SchemaExport();
java.io.File outFile = new java.io.File("schema.ddl");
schemaExport.setOutputFile(outFile.getPath());
// do not script to console or export to database
schemaExport.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.DROP, metadata);
if (doesDialectSupportDropTableIfExist() && schemaExport.getExceptions().size() > 0) {
assertEquals(2, schemaExport.getExceptions().size());
}
assertTrue(outFile.exists());
//check file is not empty
assertTrue(outFile.length() > 0);
outFile.delete();
}
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);
}
}
use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class SchemaCreationTest method testUniqueConstraintIsCorrectlyGenerated.
@Test
@TestForIssue(jiraKey = "HHH-10553")
public void testUniqueConstraintIsCorrectlyGenerated() throws Exception {
final MetadataSources metadataSources = new MetadataSources(ssr);
metadataSources.addAnnotatedClass(Element.class);
metadataSources.addAnnotatedClass(Category.class);
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.validate();
final SchemaExport schemaExport = new SchemaExport().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false);
schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
boolean isUniqueConstraintCreated = false;
for (String statement : sqlLines) {
assertThat("Should not try to create the unique constraint for the non existing table element", statement.toLowerCase().contains("alter table element"), is(false));
if (ssr.getService(JdbcEnvironment.class).getDialect() instanceof DB2Dialect) {
if (statement.toLowerCase().startsWith("create unique index") && statement.toLowerCase().contains("category (code)")) {
isUniqueConstraintCreated = true;
}
} else {
if (statement.toLowerCase().startsWith("alter table category add constraint") && statement.toLowerCase().contains("unique (code)")) {
isUniqueConstraintCreated = true;
}
}
}
assertThat("Unique constraint for table category is not created", isUniqueConstraintCreated, is(true));
}
use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.
the class TestFkUpdating method setUp.
@Before
public void setUp() {
serviceRegistry = new StandardServiceRegistryBuilder().build();
metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(User.class).addAnnotatedClass(Role.class).buildMetadata();
System.out.println("********* Starting SchemaExport for START-UP *************************");
new SchemaExport().create(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), metadata);
System.out.println("********* Completed SchemaExport for START-UP *************************");
}
Aggregations