Search in sources :

Example 36 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class MigrationTest method testIndexCreationViaSchemaUpdate.

//	/**
//	 * 3_Version.hbm.xml contains a named unique constraint and an un-named
//	 * unique constraint (will receive a randomly-generated name).  Create
//	 * the original schema with 2_Version.hbm.xml.  Then, run SchemaUpdate
//	 * TWICE using 3_Version.hbm.xml.  Neither RECREATE_QUIETLY nor SKIP should
//	 * generate any exceptions.
//	 */
//	@Test
//	@TestForIssue( jiraKey = "HHH-8162" )
//	public void testConstraintUpdate() {
//		doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY);
//		doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy.RECREATE_QUIETLY);
//		doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy.SKIP);
//	}
//
//	private void doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy strategy) {
//		// original
//		String resource1 = "org/hibernate/test/schemaupdate/2_Version.hbm.xml";
//		// adds unique constraint
//		String resource2 = "org/hibernate/test/schemaupdate/3_Version.hbm.xml";
//
//		MetadataImplementor v1metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
//				.addResource( resource1 )
//				.buildMetadata();
//		MetadataImplementor v2metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
//				.addResource( resource2 )
//				.buildMetadata();
//
//		new SchemaExport( v1metadata ).execute( false, true, true, false );
//
//		// adds unique constraint
//		Configuration v2cfg = new Configuration();
//		v2cfg.getProperties().put( AvailableSettings.UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY, strategy );
//		v2cfg.addResource( resource2 );
//		SchemaUpdate v2schemaUpdate = new SchemaUpdate( serviceRegistry, v2cfg );
//		v2schemaUpdate.execute( true, true );
//		assertEquals( 0, v2schemaUpdate.getExceptions().size() );
//
//		Configuration v3cfg = new Configuration();
//		v3cfg.getProperties().put( AvailableSettings.UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY, strategy );
//		v3cfg.addResource( resource2 );
//		SchemaUpdate v3schemaUpdate = new SchemaUpdate( serviceRegistry, v3cfg );
//		v3schemaUpdate.execute( true, true );
//		assertEquals( 0, v3schemaUpdate.getExceptions().size() );
//
//		new SchemaExport( serviceRegistry, v3cfg ).drop( false, true );
//	}
@Test
@TestForIssue(jiraKey = "HHH-9713")
public void testIndexCreationViaSchemaUpdate() {
    MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(EntityWithIndex.class).buildMetadata();
    // drop and then create the schema
    new SchemaExport().execute(EnumSet.of(TargetType.DATABASE), SchemaExport.Action.BOTH, metadata);
    try {
        // update the schema
        new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
    } finally {
        // drop the schema
        new SchemaExport().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) TestForIssue(org.hibernate.testing.TestForIssue)

Example 37 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor 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 38 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class QuotedTableNameWithForeignKeysSchemaUpdateTest method testUpdateExistingSchema.

@Test
public void testUpdateExistingSchema() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addResource("org/hibernate/test/schemaupdate/UserGroup.hbm.xml").buildMetadata();
        new SchemaUpdate().execute(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) Test(org.junit.Test)

Example 39 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class IdBagSequenceTest method testIdBagSequenceGeneratorIsCreated.

@Test
public void testIdBagSequenceGeneratorIsCreated() throws Exception {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(Environment.HBM2DDL_AUTO, "none").build();
    try {
        File output = File.createTempFile("update_script", ".sql");
        output.deleteOnExit();
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addResource("org/hibernate/test/schemaupdate/idbag/Mappings.hbm.xml").buildMetadata();
        metadata.validate();
        new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setDelimiter(";").setFormat(true).execute(EnumSet.of(TargetType.SCRIPT), metadata);
        String fileContent = new String(Files.readAllBytes(output.toPath()));
        assertThat(fileContent.toLowerCase().contains("create sequence seq_child_id"), is(true));
    } 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) File(java.io.File) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 40 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class ForeignKeyNameTest method testJoinedSubclassForeignKeyNameIsNotAutoGeneratedWhenProvided.

@Test
@TestForIssue(jiraKey = "HHH-10169")
public void testJoinedSubclassForeignKeyNameIsNotAutoGeneratedWhenProvided() throws Exception {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(Environment.HBM2DDL_AUTO, "none").build();
    try {
        File output = File.createTempFile("update_script", ".sql");
        output.deleteOnExit();
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addResource("org/hibernate/test/schemaupdate/inheritance/Employee.hbm.xml").addResource("org/hibernate/test/schemaupdate/inheritance/Person.hbm.xml").addResource("org/hibernate/test/schemaupdate/inheritance/Manager.hbm.xml").addResource("org/hibernate/test/schemaupdate/inheritance/Payment.hbm.xml").buildMetadata();
        metadata.validate();
        new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setDelimiter(";").setFormat(true).execute(EnumSet.of(TargetType.SCRIPT), metadata);
        String fileContent = new String(Files.readAllBytes(output.toPath()));
        assertThat(fileContent.toLowerCase().contains("fk_emp_per"), is(true));
    } 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) File(java.io.File) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)68 MetadataSources (org.hibernate.boot.MetadataSources)58 Test (org.junit.Test)53 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)34 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)33 TestForIssue (org.hibernate.testing.TestForIssue)25 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)19 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)17 PersistentClass (org.hibernate.mapping.PersistentClass)15 File (java.io.File)14 MetadataBuilder (org.hibernate.boot.MetadataBuilder)7 Property (org.hibernate.mapping.Property)7 SimpleValue (org.hibernate.mapping.SimpleValue)7 Type (org.hibernate.type.Type)7 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)6 BasicType (org.hibernate.type.BasicType)6 Metadata (org.hibernate.boot.Metadata)4 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)4 Namespace (org.hibernate.boot.model.relational.Namespace)4 Sequence (org.hibernate.boot.model.relational.Sequence)4