Search in sources :

Example 11 with Namespace

use of org.hibernate.boot.model.relational.Namespace in project hibernate-orm by hibernate.

the class IdentityGeneratorExtendsTest method testIdentifierGeneratorExtendsIdentityGenerator.

@Test
public void testIdentifierGeneratorExtendsIdentityGenerator() {
    final MetadataSources sources = new MetadataSources(serviceRegistry());
    sources.addAnnotatedClass(EntityBean.class);
    final MetadataBuilder builder = sources.getMetadataBuilder();
    final Metadata metadata = builder.build();
    for (final Namespace ns : metadata.getDatabase().getNamespaces()) {
        for (final org.hibernate.mapping.Table table : ns.getTables()) {
            final KeyValue value = table.getIdentifierValue();
            assertNotNull("IdentifierValue was null", value);
            assertTrue(value.isIdentityColumn(metadata.getIdentifierGeneratorFactory(), getDialect()));
        }
    }
    Session s = openSession();
    s.beginTransaction();
    s.save(new EntityBean());
    s.getTransaction().commit();
    s.close();
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Namespace(org.hibernate.boot.model.relational.Namespace) Session(org.hibernate.Session) Test(org.junit.Test)

Example 12 with Namespace

use of org.hibernate.boot.model.relational.Namespace in project hibernate-orm by hibernate.

the class SequenceExportTest method testMultipleUsesOfExplicitSequenceName.

@Test
@TestForIssue(jiraKey = "HHH-9936")
public void testMultipleUsesOfExplicitSequenceName() {
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Entity3.class).addAnnotatedClass(Entity4.class).buildMetadata();
    metadata.validate();
    int namespaceCount = 0;
    int sequenceCount = 0;
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        namespaceCount++;
        for (Sequence sequence : namespace.getSequences()) {
            sequenceCount++;
        }
    }
    assertEquals(1, namespaceCount);
    assertEquals(1, sequenceCount);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Sequence(org.hibernate.boot.model.relational.Sequence) Namespace(org.hibernate.boot.model.relational.Namespace) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 13 with Namespace

use of org.hibernate.boot.model.relational.Namespace in project hibernate-orm by hibernate.

the class ConstraintTest method testConstraintNameLength.

@Test
@TestForIssue(jiraKey = "HHH-1904")
public void testConstraintNameLength() {
    int foundCount = 0;
    for (Namespace namespace : metadata().getDatabase().getNamespaces()) {
        for (org.hibernate.mapping.Table table : namespace.getTables()) {
            Iterator fkItr = table.getForeignKeyIterator();
            while (fkItr.hasNext()) {
                ForeignKey fk = (ForeignKey) fkItr.next();
                assertTrue(fk.getName().length() <= MAX_NAME_LENGTH);
                // ensure the randomly generated constraint name doesn't
                // happen if explicitly given
                Column column = fk.getColumn(0);
                if (column.getName().equals("explicit_native")) {
                    foundCount++;
                    assertEquals(fk.getName(), EXPLICIT_FK_NAME_NATIVE);
                } else if (column.getName().equals("explicit_jpa")) {
                    foundCount++;
                    assertEquals(fk.getName(), EXPLICIT_FK_NAME_JPA);
                }
            }
            Iterator ukItr = table.getUniqueKeyIterator();
            while (ukItr.hasNext()) {
                UniqueKey uk = (UniqueKey) ukItr.next();
                assertTrue(uk.getName().length() <= MAX_NAME_LENGTH);
                // ensure the randomly generated constraint name doesn't
                // happen if explicitly given
                Column column = uk.getColumn(0);
                if (column.getName().equals("explicit")) {
                    foundCount++;
                    assertEquals(uk.getName(), EXPLICIT_UK_NAME);
                }
            }
        }
    }
    assertEquals("Could not find the necessary columns.", 3, foundCount);
}
Also used : JoinColumn(javax.persistence.JoinColumn) Column(org.hibernate.mapping.Column) UniqueKey(org.hibernate.mapping.UniqueKey) Iterator(java.util.Iterator) ForeignKey(org.hibernate.mapping.ForeignKey) UniqueConstraint(javax.persistence.UniqueConstraint) Namespace(org.hibernate.boot.model.relational.Namespace) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with Namespace

use of org.hibernate.boot.model.relational.Namespace in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method addTable.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Table handling
@Override
public Table addTable(String schemaName, String catalogName, String name, String subselectFragment, boolean isAbstract) {
    final Namespace namespace = getDatabase().locateNamespace(getDatabase().toIdentifier(catalogName), getDatabase().toIdentifier(schemaName));
    // annotation binding depends on the "table name" for @Subselect bindings
    // being set into the generated table (mainly to avoid later NPE), but for now we need to keep that :(
    final Identifier logicalName;
    if (name != null) {
        logicalName = getDatabase().toIdentifier(name);
    } else {
        logicalName = null;
    }
    if (subselectFragment != null) {
        return new Table(namespace, logicalName, subselectFragment, isAbstract);
    } else {
        Table table = namespace.locateTable(logicalName);
        if (table != null) {
            if (!isAbstract) {
                table.setAbstract(false);
            }
            return table;
        }
        return namespace.createTable(logicalName, isAbstract);
    }
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) Namespace(org.hibernate.boot.model.relational.Namespace)

Example 15 with Namespace

use of org.hibernate.boot.model.relational.Namespace in project hibernate-orm by hibernate.

the class MultipleHiLoPerTableGenerator method registerExportables.

@Override
public void registerExportables(Database database) {
    final Namespace namespace = database.locateNamespace(qualifiedTableName.getCatalogName(), qualifiedTableName.getSchemaName());
    Table table = namespace.locateTable(qualifiedTableName.getObjectName());
    if (table == null) {
        table = namespace.createTable(qualifiedTableName.getObjectName(), false);
        // todo : note sure the best solution here.  do we add the columns if missing?  other?
        table.setPrimaryKey(new PrimaryKey(table));
        final Column pkColumn = new ExportableColumn(database, table, segmentColumnName, StringType.INSTANCE, database.getDialect().getTypeName(Types.VARCHAR, keySize, 0, 0));
        pkColumn.setNullable(false);
        table.addColumn(pkColumn);
        table.getPrimaryKey().addColumn(pkColumn);
        final Column valueColumn = new ExportableColumn(database, table, valueColumnName, LongType.INSTANCE);
        table.addColumn(valueColumn);
    }
    final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
    // allow physical naming strategies a chance to kick in
    tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(table.getQualifiedTableName(), jdbcEnvironment.getDialect());
    query = "select " + valueColumnName + " from " + jdbcEnvironment.getDialect().appendLockHint(LockMode.PESSIMISTIC_WRITE, tableName) + " where " + segmentColumnName + " = '" + segmentName + "'" + jdbcEnvironment.getDialect().getForUpdateString();
    update = "update " + tableName + " set " + valueColumnName + " = ? where " + valueColumnName + " = ? and " + segmentColumnName + " = '" + segmentName + "'";
    insert = "insert into " + tableName + "(" + segmentColumnName + ", " + valueColumnName + ") " + "values('" + segmentName + "', ?)";
}
Also used : Table(org.hibernate.mapping.Table) Column(org.hibernate.mapping.Column) PrimaryKey(org.hibernate.mapping.PrimaryKey) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) Namespace(org.hibernate.boot.model.relational.Namespace)

Aggregations

Namespace (org.hibernate.boot.model.relational.Namespace)22 Sequence (org.hibernate.boot.model.relational.Sequence)10 Table (org.hibernate.mapping.Table)10 Identifier (org.hibernate.boot.model.naming.Identifier)7 Test (org.junit.Test)7 MetadataSources (org.hibernate.boot.MetadataSources)5 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)5 TestForIssue (org.hibernate.testing.TestForIssue)5 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 Column (org.hibernate.mapping.Column)4 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)4 HashSet (java.util.HashSet)3 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)3 Database (org.hibernate.boot.model.relational.Database)3 Dialect (org.hibernate.dialect.Dialect)3 Iterator (java.util.Iterator)2 LinkedHashSet (java.util.LinkedHashSet)2 ForeignKey (javax.persistence.ForeignKey)2 InitCommand (org.hibernate.boot.model.relational.InitCommand)2 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)2