Search in sources :

Example 51 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class OneToOneSchemaTest method testUniqueKeyNotGeneratedViaAnnotations.

@Test
public void testUniqueKeyNotGeneratedViaAnnotations() throws Exception {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Parent.class).addAnnotatedClass(Child.class).buildMetadata();
        Table childTable = metadata.getDatabase().getDefaultNamespace().locateTable(Identifier.toIdentifier("CHILD"));
        assertFalse("UniqueKey was generated when it should not", childTable.getUniqueKeyIterator().hasNext());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 52 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class SchemaUpdateTableBackedSequenceTest method testCreateTableOnUpdate.

@Test
public void testCreateTableOnUpdate() throws SQLException {
    Metadata metadata = new MetadataSources(ssr).buildMetadata();
    Database database = metadata.getDatabase();
    TableStructure tableStructure = new TableStructure(database.getJdbcEnvironment(), new QualifiedTableName(null, null, Identifier.toIdentifier("test_seq")), Identifier.toIdentifier("nextval"), 20, 30, Long.class);
    tableStructure.registerExportables(database);
    // lets make sure the InitCommand is there
    assertEquals(1, database.getDefaultNamespace().getTables().size());
    Table table = database.getDefaultNamespace().getTables().iterator().next();
    assertEquals(1, table.getInitCommands().size());
    final TargetImpl target = new TargetImpl();
    ssr.getService(SchemaManagementTool.class).getSchemaMigrator(Collections.emptyMap()).doMigration(metadata, new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return true;
        }

        @Override
        public Map getConfigurationValues() {
            return ssr.getService(ConfigurationService.class).getSettings();
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    }, new TargetDescriptor() {

        @Override
        public EnumSet<TargetType> getTargetTypes() {
            return EnumSet.of(TargetType.SCRIPT, TargetType.DATABASE);
        }

        @Override
        public ScriptTargetOutput getScriptTargetOutput() {
            return target;
        }
    });
    assertTrue(target.found);
    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) EnumSet(java.util.EnumSet) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) TableStructure(org.hibernate.id.enhanced.TableStructure) Database(org.hibernate.boot.model.relational.Database) ScriptTargetOutput(org.hibernate.tool.schema.spi.ScriptTargetOutput) Map(java.util.Map) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Example 53 with Metadata

use of org.hibernate.boot.Metadata in project jbosstools-hibernate by jbosstools.

the class MetadataHelper method getMetadataFromMethod.

private static Metadata getMetadataFromMethod(Configuration configuration) {
    Metadata result = null;
    Method metadataMethod = getMethod("getMetadata", configuration);
    if (metadataMethod != null) {
        try {
            metadataMethod.setAccessible(true);
            result = (Metadata) metadataMethod.invoke(configuration, new Object[] {});
        } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}
Also used : Metadata(org.hibernate.boot.Metadata) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 54 with Metadata

use of org.hibernate.boot.Metadata in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testReadFromJDBC.

@Test
public void testReadFromJDBC() throws Exception {
    Connection connection = DriverManager.getConnection("jdbc:h2:mem:test");
    Statement statement = connection.createStatement();
    statement.execute("CREATE TABLE FOO(id int primary key, bar varchar(255))");
    JDBCMetaDataConfiguration jdbcMdCfg = new JDBCMetaDataConfiguration();
    jdbcMdCfg.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
    configurationFacade = FACADE_FACTORY.createConfiguration(jdbcMdCfg);
    Metadata metadata = jdbcMdCfg.getMetadata();
    Iterator<?> iterator = metadata.getEntityBindings().iterator();
    jdbcMdCfg = new JDBCMetaDataConfiguration();
    jdbcMdCfg.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
    configurationFacade = FACADE_FACTORY.createConfiguration(jdbcMdCfg);
    Assert.assertFalse(iterator.hasNext());
    configurationFacade.readFromJDBC();
    metadata = jdbcMdCfg.getMetadata();
    iterator = metadata.getEntityBindings().iterator();
    PersistentClass persistentClass = (PersistentClass) iterator.next();
    Assert.assertEquals("Foo", persistentClass.getClassName());
    statement.execute("DROP TABLE FOO");
    connection.close();
}
Also used : JDBCMetaDataConfiguration(org.hibernate.cfg.JDBCMetaDataConfiguration) Statement(java.sql.Statement) Connection(java.sql.Connection) Metadata(org.hibernate.boot.Metadata) PersistentClass(org.hibernate.mapping.PersistentClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) Test(org.junit.Test)

Example 55 with Metadata

use of org.hibernate.boot.Metadata in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testConfigure.

@Test
public void testConfigure() {
    String fooClassName = "org.jboss.tools.hibernate.runtime.v_5_2.internal.test.Foo";
    Metadata metadata = MetadataHelper.getMetadata(configuration);
    Assert.assertNull(metadata.getEntityBinding(fooClassName));
    configurationFacade.configure();
    metadata = MetadataHelper.getMetadata(configuration);
    Assert.assertNotNull(metadata.getEntityBinding(fooClassName));
}
Also used : Metadata(org.hibernate.boot.Metadata) Test(org.junit.Test)

Aggregations

Metadata (org.hibernate.boot.Metadata)148 MetadataSources (org.hibernate.boot.MetadataSources)124 Test (org.junit.Test)103 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)73 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)56 PersistentClass (org.hibernate.mapping.PersistentClass)46 TestForIssue (org.hibernate.testing.TestForIssue)31 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)18 Session (org.hibernate.Session)14 HashMap (java.util.HashMap)13 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)13 Property (org.hibernate.mapping.Property)12 RootClass (org.hibernate.mapping.RootClass)12 Map (java.util.Map)11 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 SessionFactory (org.hibernate.SessionFactory)10 Collection (org.hibernate.mapping.Collection)10 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)10 ServiceRegistry (org.hibernate.service.ServiceRegistry)8 Before (org.junit.Before)8