use of org.hibernate.boot.model.relational.Sequence 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);
}
use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.
the class SequenceGenerator method registerExportables.
@Override
public void registerExportables(Database database) {
final Namespace namespace = database.locateNamespace(logicalQualifiedSequenceName.getCatalogName(), logicalQualifiedSequenceName.getSchemaName());
Sequence sequence = namespace.locateSequence(logicalQualifiedSequenceName.getObjectName());
if (sequence != null) {
sequence.validate(1, 1);
} else {
sequence = namespace.createSequence(logicalQualifiedSequenceName.getObjectName(), 1, 1);
}
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(sequence.getName(), dialect);
this.sql = jdbcEnvironment.getDialect().getSequenceNextValString(sequenceName);
}
use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.
the class AbstractSchemaValidator method performValidation.
public void performValidation(Metadata metadata, DatabaseInformation databaseInformation, ExecutionOptions options, Dialect dialect) {
for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
validateTables(metadata, databaseInformation, options, dialect, namespace);
}
}
for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
for (Sequence sequence : namespace.getSequences()) {
if (schemaFilter.includeSequence(sequence)) {
final SequenceInformation sequenceInformation = databaseInformation.getSequenceInformation(sequence.getName());
validateSequence(sequence, sequenceInformation);
}
}
}
}
}
use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.
the class SchemaCreatorImpl method createFromMetadata.
public void createFromMetadata(Metadata metadata, ExecutionOptions options, Dialect dialect, Formatter formatter, GenerationTarget... targets) {
boolean tryToCreateCatalogs = false;
boolean tryToCreateSchemas = false;
if (options.shouldManageNamespaces()) {
if (dialect.canCreateSchema()) {
tryToCreateSchemas = true;
}
if (dialect.canCreateCatalog()) {
tryToCreateCatalogs = true;
}
}
final Database database = metadata.getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Set<String> exportIdentifiers = new HashSet<String>(50);
// first, create each catalog/schema
if (tryToCreateCatalogs || tryToCreateSchemas) {
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
if (tryToCreateCatalogs) {
final Identifier catalogLogicalName = namespace.getName().getCatalog();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
if (catalogPhysicalName != null && !exportedCatalogs.contains(catalogLogicalName)) {
applySqlStrings(dialect.getCreateCatalogCommand(catalogPhysicalName.render(dialect)), formatter, options, targets);
exportedCatalogs.add(catalogLogicalName);
}
}
if (tryToCreateSchemas && namespace.getPhysicalName().getSchema() != null) {
applySqlStrings(dialect.getCreateSchemaCommand(namespace.getPhysicalName().getSchema().render(dialect)), formatter, options, targets);
}
}
}
// next, create all "before table" auxiliary objects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (!auxiliaryDatabaseObject.beforeTablesOnCreation()) {
continue;
}
if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// then, create all schema objects (tables, sequences, constraints, etc) in each schema
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
// sequences
for (Sequence sequence : namespace.getSequences()) {
if (!schemaFilter.includeSequence(sequence)) {
continue;
}
checkExportIdentifier(sequence, exportIdentifiers);
applySqlStrings(dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), // ),
formatter, options, targets);
}
// tables
for (Table table : namespace.getTables()) {
if (!table.isPhysicalTable()) {
continue;
}
if (!schemaFilter.includeTable(table)) {
continue;
}
checkExportIdentifier(table, exportIdentifiers);
applySqlStrings(dialect.getTableExporter().getSqlCreateStrings(table, metadata), formatter, options, targets);
}
for (Table table : namespace.getTables()) {
if (!table.isPhysicalTable()) {
continue;
}
if (!schemaFilter.includeTable(table)) {
continue;
}
// indexes
final Iterator indexItr = table.getIndexIterator();
while (indexItr.hasNext()) {
final Index index = (Index) indexItr.next();
checkExportIdentifier(index, exportIdentifiers);
applySqlStrings(dialect.getIndexExporter().getSqlCreateStrings(index, metadata), formatter, options, targets);
}
// unique keys
final Iterator ukItr = table.getUniqueKeyIterator();
while (ukItr.hasNext()) {
final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
checkExportIdentifier(uniqueKey, exportIdentifiers);
applySqlStrings(dialect.getUniqueKeyExporter().getSqlCreateStrings(uniqueKey, metadata), formatter, options, targets);
}
}
}
// NOTE : Foreign keys must be created *after* all tables of all namespaces for cross namespace fks. see HHH-10420
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
for (Table table : namespace.getTables()) {
if (!schemaFilter.includeTable(table)) {
continue;
}
// foreign keys
final Iterator fkItr = table.getForeignKeyIterator();
while (fkItr.hasNext()) {
final ForeignKey foreignKey = (ForeignKey) fkItr.next();
applySqlStrings(dialect.getForeignKeyExporter().getSqlCreateStrings(foreignKey, metadata), formatter, options, targets);
}
}
}
// next, create all "after table" auxiliary objects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.appliesToDialect(dialect) && !auxiliaryDatabaseObject.beforeTablesOnCreation()) {
checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// and finally add all init commands
for (InitCommand initCommand : database.getInitCommands()) {
// todo: this should alo probably use the DML formatter...
applySqlStrings(initCommand.getInitCommands(), formatter, options, targets);
}
}
use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.
the class LegacySequenceExportTest method testMultipleUsesOfDefaultSequenceName.
@Test
@TestForIssue(jiraKey = "HHH-9936")
public void testMultipleUsesOfDefaultSequenceName() {
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Entity1.class).addAnnotatedClass(Entity2.class).buildMetadata();
metadata.validate();
assertEquals(0, metadata.getDatabase().getAuxiliaryDatabaseObjects().size());
int count = 0;
for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
for (Sequence sequence : namespace.getSequences()) {
count++;
}
}
assertEquals(1, count);
}
Aggregations