Search in sources :

Example 66 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class QueryAndSQLTest method testImportQueryFromMappedSuperclass.

/**
	 * We are testing 2 things here:
	 * 1. The query 'night.olderThan' is defined in a MappedSuperClass - Darkness.
	 * We are verifying that queries defined in a MappedSuperClass are processed.
	 * 2. There are 2 Entity classes that extend from Darkness - Night and Twilight.
	 * We are verifying that this does not cause any issues.eg. Double processing of the
	 * MappedSuperClass
	 */
@Test
public void testImportQueryFromMappedSuperclass() {
    Session s = openSession();
    try {
        s.getNamedQuery("night.olderThan");
    } catch (MappingException ex) {
        fail("Query imported from MappedSuperclass");
    }
    s.close();
}
Also used : Session(org.hibernate.Session) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Example 67 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method buildUniqueKeyFromColumnNames.

private void buildUniqueKeyFromColumnNames(final Table table, String keyName, final String[] columnNames, String[] orderings, boolean unique, final MetadataBuildingContext buildingContext) {
    int size = columnNames.length;
    Column[] columns = new Column[size];
    Set<Column> unbound = new HashSet<Column>();
    Set<Column> unboundNoLogical = new HashSet<Column>();
    for (int index = 0; index < size; index++) {
        final String logicalColumnName = columnNames[index];
        try {
            final String physicalColumnName = getPhysicalColumnName(table, logicalColumnName);
            columns[index] = new Column(physicalColumnName);
            unbound.add(columns[index]);
        //column equals and hashcode is based on column name
        } catch (MappingException e) {
            // If at least 1 columnName does exist, 'columns' will contain a mix of Columns and nulls.  In order
            // to exhaustively report all of the unbound columns at once, w/o an NPE in
            // Constraint#generateName's array sorting, simply create a fake Column.
            columns[index] = new Column(logicalColumnName);
            unboundNoLogical.add(columns[index]);
        }
    }
    final String originalKeyName = keyName;
    if (unique) {
        final Identifier keyNameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineUniqueKeyName(new ImplicitUniqueKeyNameSource() {

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return buildingContext;
            }

            @Override
            public Identifier getTableName() {
                return table.getNameIdentifier();
            }

            private List<Identifier> columnNameIdentifiers;

            @Override
            public List<Identifier> getColumnNames() {
                // be lazy about building these
                if (columnNameIdentifiers == null) {
                    columnNameIdentifiers = toIdentifiers(columnNames);
                }
                return columnNameIdentifiers;
            }

            @Override
            public Identifier getUserProvidedIdentifier() {
                return originalKeyName != null ? Identifier.toIdentifier(originalKeyName) : null;
            }
        });
        keyName = keyNameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect());
        UniqueKey uk = table.getOrCreateUniqueKey(keyName);
        for (int i = 0; i < columns.length; i++) {
            Column column = columns[i];
            String order = orderings != null ? orderings[i] : null;
            if (table.containsColumn(column)) {
                uk.addColumn(column, order);
                unbound.remove(column);
            }
        }
    } else {
        final Identifier keyNameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineIndexName(new ImplicitIndexNameSource() {

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return buildingContext;
            }

            @Override
            public Identifier getTableName() {
                return table.getNameIdentifier();
            }

            private List<Identifier> columnNameIdentifiers;

            @Override
            public List<Identifier> getColumnNames() {
                // be lazy about building these
                if (columnNameIdentifiers == null) {
                    columnNameIdentifiers = toIdentifiers(columnNames);
                }
                return columnNameIdentifiers;
            }

            @Override
            public Identifier getUserProvidedIdentifier() {
                return originalKeyName != null ? Identifier.toIdentifier(originalKeyName) : null;
            }
        });
        keyName = keyNameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect());
        Index index = table.getOrCreateIndex(keyName);
        for (int i = 0; i < columns.length; i++) {
            Column column = columns[i];
            String order = orderings != null ? orderings[i] : null;
            if (table.containsColumn(column)) {
                index.addColumn(column, order);
                unbound.remove(column);
            }
        }
    }
    if (unbound.size() > 0 || unboundNoLogical.size() > 0) {
        StringBuilder sb = new StringBuilder("Unable to create ");
        if (unique) {
            sb.append("unique key constraint (");
        } else {
            sb.append("index (");
        }
        for (String columnName : columnNames) {
            sb.append(columnName).append(", ");
        }
        sb.setLength(sb.length() - 2);
        sb.append(") on table ").append(table.getName()).append(": database column ");
        for (Column column : unbound) {
            sb.append("'").append(column.getName()).append("', ");
        }
        for (Column column : unboundNoLogical) {
            sb.append("'").append(column.getName()).append("', ");
        }
        sb.setLength(sb.length() - 2);
        sb.append(" not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)");
        throw new AnnotationException(sb.toString());
    }
}
Also used : MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Index(org.hibernate.mapping.Index) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException) Identifier(org.hibernate.boot.model.naming.Identifier) ImplicitIndexNameSource(org.hibernate.boot.model.naming.ImplicitIndexNameSource) Column(org.hibernate.mapping.Column) UniqueKey(org.hibernate.mapping.UniqueKey) AnnotationException(org.hibernate.AnnotationException) List(java.util.List) ArrayList(java.util.ArrayList) ImplicitUniqueKeyNameSource(org.hibernate.boot.model.naming.ImplicitUniqueKeyNameSource) HashSet(java.util.HashSet)

Example 68 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class MetadataImpl method getReferencedPropertyType.

@Override
public org.hibernate.type.Type getReferencedPropertyType(String entityName, String propertyName) throws MappingException {
    final PersistentClass pc = entityBindingMap.get(entityName);
    if (pc == null) {
        throw new MappingException("persistent class not known: " + entityName);
    }
    Property prop = pc.getReferencedProperty(propertyName);
    if (prop == null) {
        throw new MappingException("property not known: " + entityName + '.' + propertyName);
    }
    return prop.getType();
}
Also used : Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) MappingException(org.hibernate.MappingException)

Example 69 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method getPhysicalColumnName.

@Override
public String getPhysicalColumnName(Table table, Identifier logicalName) throws MappingException {
    if (logicalName == null) {
        throw new MappingException("Logical column name cannot be null");
    }
    Table currentTable = table;
    String physicalName = null;
    while (currentTable != null) {
        final TableColumnNameBinding binding = columnNameBindingByTableMap.get(currentTable);
        if (binding != null) {
            physicalName = binding.logicalToPhysical.get(logicalName);
            if (physicalName != null) {
                break;
            }
        }
        if (DenormalizedTable.class.isInstance(currentTable)) {
            currentTable = ((DenormalizedTable) currentTable).getIncludedTable();
        } else {
            currentTable = null;
        }
    }
    if (physicalName == null) {
        throw new MappingException("Unable to find column with logical name " + logicalName.render() + " in table " + table.getName());
    }
    return physicalName;
}
Also used : DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException)

Example 70 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method secondPassCompileForeignKeys.

protected void secondPassCompileForeignKeys(final Table table, Set<ForeignKey> done, final MetadataBuildingContext buildingContext) throws MappingException {
    table.createForeignKeys();
    Iterator itr = table.getForeignKeyIterator();
    while (itr.hasNext()) {
        final ForeignKey fk = (ForeignKey) itr.next();
        if (!done.contains(fk)) {
            done.add(fk);
            final String referencedEntityName = fk.getReferencedEntityName();
            if (referencedEntityName == null) {
                throw new MappingException("An association from the table " + fk.getTable().getName() + " does not specify the referenced entity");
            }
            log.debugf("Resolving reference to class: %s", referencedEntityName);
            final PersistentClass referencedClass = getEntityBinding(referencedEntityName);
            if (referencedClass == null) {
                throw new MappingException("An association from the table " + fk.getTable().getName() + " refers to an unmapped class: " + referencedEntityName);
            }
            if (referencedClass.isJoinedSubclass()) {
                secondPassCompileForeignKeys(referencedClass.getSuperclass().getTable(), done, buildingContext);
            }
            fk.setReferencedTable(referencedClass.getTable());
            Identifier nameIdentifier;
            ImplicitForeignKeyNameSource foreignKeyNameSource = new ImplicitForeignKeyNameSource() {

                final List<Identifier> columnNames = extractColumnNames(fk.getColumns());

                List<Identifier> referencedColumnNames = null;

                @Override
                public Identifier getTableName() {
                    return table.getNameIdentifier();
                }

                @Override
                public List<Identifier> getColumnNames() {
                    return columnNames;
                }

                @Override
                public Identifier getReferencedTableName() {
                    return fk.getReferencedTable().getNameIdentifier();
                }

                @Override
                public List<Identifier> getReferencedColumnNames() {
                    if (referencedColumnNames == null) {
                        referencedColumnNames = extractColumnNames(fk.getReferencedColumns());
                    }
                    return referencedColumnNames;
                }

                @Override
                public Identifier getUserProvidedIdentifier() {
                    return fk.getName() != null ? Identifier.toIdentifier(fk.getName()) : null;
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return buildingContext;
                }
            };
            nameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineForeignKeyName(foreignKeyNameSource);
            fk.setName(nameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect()));
            fk.alignColumns();
        }
    }
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) Iterator(java.util.Iterator) ImplicitForeignKeyNameSource(org.hibernate.boot.model.naming.ImplicitForeignKeyNameSource) List(java.util.List) ArrayList(java.util.ArrayList) ForeignKey(org.hibernate.mapping.ForeignKey) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

MappingException (org.hibernate.MappingException)94 PersistentClass (org.hibernate.mapping.PersistentClass)17 HibernateException (org.hibernate.HibernateException)12 Iterator (java.util.Iterator)11 Test (org.junit.Test)11 AnnotationException (org.hibernate.AnnotationException)10 QueryException (org.hibernate.QueryException)10 Type (org.hibernate.type.Type)10 Property (org.hibernate.mapping.Property)9 HashMap (java.util.HashMap)8 XClass (org.hibernate.annotations.common.reflection.XClass)8 DuplicateMappingException (org.hibernate.DuplicateMappingException)6 Configuration (org.hibernate.cfg.Configuration)6 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)6 ServiceRegistry (org.hibernate.service.ServiceRegistry)6 Map (java.util.Map)5 AssociationType (org.hibernate.type.AssociationType)5 HashSet (java.util.HashSet)4 ClassLoadingException (org.hibernate.annotations.common.reflection.ClassLoadingException)4 MetadataSources (org.hibernate.boot.MetadataSources)4