Search in sources :

Example 6 with MappingException

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

the class InFlightMetadataCollectorImpl method getLogicalColumnName.

@Override
public String getLogicalColumnName(Table table, Identifier physicalName) throws MappingException {
    final String physicalNameString = physicalName.render(getDatabase().getJdbcEnvironment().getDialect());
    Identifier logicalName = null;
    Table currentTable = table;
    while (currentTable != null) {
        final TableColumnNameBinding binding = columnNameBindingByTableMap.get(currentTable);
        if (binding != null) {
            logicalName = binding.physicalToLogical.get(physicalNameString);
            if (logicalName != null) {
                break;
            }
        }
        if (DenormalizedTable.class.isInstance(currentTable)) {
            currentTable = ((DenormalizedTable) currentTable).getIncludedTable();
        } else {
            currentTable = null;
        }
    }
    if (logicalName == null) {
        throw new MappingException("Unable to find column with physical name " + physicalNameString + " in table " + table.getName());
    }
    return logicalName.render();
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException)

Example 7 with MappingException

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

the class InFlightMetadataCollectorImpl method topologicalSort.

/* naive O(n^3) topological sort */
private void topologicalSort(List<CopyIdentifierComponentSecondPass> sorted, Set<CopyIdentifierComponentSecondPass> toSort) {
    while (!toSort.isEmpty()) {
        CopyIdentifierComponentSecondPass independent = null;
        searchForIndependent: for (CopyIdentifierComponentSecondPass secondPass : toSort) {
            for (CopyIdentifierComponentSecondPass other : toSort) {
                if (secondPass.dependentUpon(other)) {
                    continue searchForIndependent;
                }
            }
            independent = secondPass;
            break;
        }
        if (independent == null) {
            throw new MappingException("cyclic dependency in derived identities");
        }
        toSort.remove(independent);
        sorted.add(independent);
    }
}
Also used : CopyIdentifierComponentSecondPass(org.hibernate.cfg.CopyIdentifierComponentSecondPass) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException)

Example 8 with MappingException

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

the class BinderHelper method findColumnOwner.

/**
	 * Find the column owner (ie PersistentClass or Join) of columnName.
	 * If columnName is null or empty, persistentClass is returned
	 */
public static Object findColumnOwner(PersistentClass persistentClass, String columnName, MetadataBuildingContext context) {
    if (StringHelper.isEmpty(columnName)) {
        //shortcut for implicit referenced column names
        return persistentClass;
    }
    PersistentClass current = persistentClass;
    Object result;
    boolean found = false;
    do {
        result = current;
        Table currentTable = current.getTable();
        try {
            context.getMetadataCollector().getPhysicalColumnName(currentTable, columnName);
            found = true;
        } catch (MappingException me) {
        //swallow it
        }
        Iterator joins = current.getJoinIterator();
        while (!found && joins.hasNext()) {
            result = joins.next();
            currentTable = ((Join) result).getTable();
            try {
                context.getMetadataCollector().getPhysicalColumnName(currentTable, columnName);
                found = true;
            } catch (MappingException me) {
            //swallow it
            }
        }
        current = current.getSuperclass();
    } while (!found && current != null);
    return found ? result : null;
}
Also used : Table(org.hibernate.mapping.Table) Iterator(java.util.Iterator) PersistentClass(org.hibernate.mapping.PersistentClass) MappingException(org.hibernate.MappingException)

Example 9 with MappingException

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

the class BinderHelper method buildAnyValue.

public static Any buildAnyValue(String anyMetaDefName, Ejb3JoinColumn[] columns, javax.persistence.Column metaColumn, PropertyData inferredData, boolean cascadeOnDelete, Nullability nullability, PropertyHolder propertyHolder, EntityBinder entityBinder, boolean optional, MetadataBuildingContext context) {
    //All FK columns should be in the same table
    Any value = new Any(context.getMetadataCollector(), columns[0].getTable());
    AnyMetaDef metaAnnDef = inferredData.getProperty().getAnnotation(AnyMetaDef.class);
    if (metaAnnDef != null) {
        //local has precedence over general and can be mapped for future reference if named
        bindAnyMetaDefs(inferredData.getProperty(), context);
    } else {
        metaAnnDef = context.getMetadataCollector().getAnyMetaDef(anyMetaDefName);
    }
    if (metaAnnDef != null) {
        value.setIdentifierType(metaAnnDef.idType());
        value.setMetaType(metaAnnDef.metaType());
        HashMap values = new HashMap();
        org.hibernate.type.Type metaType = context.getMetadataCollector().getTypeResolver().heuristicType(value.getMetaType());
        for (MetaValue metaValue : metaAnnDef.metaValues()) {
            try {
                Object discrim = ((org.hibernate.type.DiscriminatorType) metaType).stringToObject(metaValue.value());
                String entityName = metaValue.targetEntity().getName();
                values.put(discrim, entityName);
            } catch (ClassCastException cce) {
                throw new MappingException("metaType was not a DiscriminatorType: " + metaType.getName());
            } catch (Exception e) {
                throw new MappingException("could not interpret metaValue", e);
            }
        }
        if (!values.isEmpty()) {
            value.setMetaValues(values);
        }
    } else {
        throw new AnnotationException("Unable to find @AnyMetaDef for an @(ManyTo)Any mapping: " + StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
    }
    value.setCascadeDeleteEnabled(cascadeOnDelete);
    if (!optional) {
        for (Ejb3JoinColumn column : columns) {
            column.setNullable(false);
        }
    }
    Ejb3Column[] metaColumns = Ejb3Column.buildColumnFromAnnotation(new javax.persistence.Column[] { metaColumn }, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), context);
    //set metaColumn to the right table
    for (Ejb3Column column : metaColumns) {
        column.setTable(value.getTable());
    }
    //meta column
    for (Ejb3Column column : metaColumns) {
        column.linkWithValue(value);
    }
    //id columns
    final String propertyName = inferredData.getPropertyName();
    Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
    for (Ejb3JoinColumn column : columns) {
        column.linkWithValue(value);
    }
    return value;
}
Also used : MetaValue(org.hibernate.annotations.MetaValue) HashMap(java.util.HashMap) Any(org.hibernate.mapping.Any) AnnotationException(org.hibernate.AnnotationException) MappingException(org.hibernate.MappingException) ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) MappingException(org.hibernate.MappingException) AnyMetaDef(org.hibernate.annotations.AnyMetaDef) AnnotationException(org.hibernate.AnnotationException)

Example 10 with MappingException

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

the class BinderHelper method findPropertyByName.

/**
	 * Retrieve the property by path in a recursive way, including IndetifierProperty in the loop
	 * If propertyName is null or empty, the IdentifierProperty is returned
	 */
public static Property findPropertyByName(PersistentClass associatedClass, String propertyName) {
    Property property = null;
    Property idProperty = associatedClass.getIdentifierProperty();
    String idName = idProperty != null ? idProperty.getName() : null;
    try {
        if (propertyName == null || propertyName.length() == 0 || propertyName.equals(idName)) {
            //default to id
            property = idProperty;
        } else {
            if (propertyName.indexOf(idName + ".") == 0) {
                property = idProperty;
                propertyName = propertyName.substring(idName.length() + 1);
            }
            StringTokenizer st = new StringTokenizer(propertyName, ".", false);
            while (st.hasMoreElements()) {
                String element = (String) st.nextElement();
                if (property == null) {
                    property = associatedClass.getProperty(element);
                } else {
                    if (!property.isComposite()) {
                        return null;
                    }
                    property = ((Component) property.getValue()).getProperty(element);
                }
            }
        }
    } catch (MappingException e) {
        try {
            //if we do not find it try to check the identifier mapper
            if (associatedClass.getIdentifierMapper() == null) {
                return null;
            }
            StringTokenizer st = new StringTokenizer(propertyName, ".", false);
            while (st.hasMoreElements()) {
                String element = (String) st.nextElement();
                if (property == null) {
                    property = associatedClass.getIdentifierMapper().getProperty(element);
                } else {
                    if (!property.isComposite()) {
                        return null;
                    }
                    property = ((Component) property.getValue()).getProperty(element);
                }
            }
        } catch (MappingException ee) {
            return null;
        }
    }
    return property;
}
Also used : StringTokenizer(java.util.StringTokenizer) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty) MappingException(org.hibernate.MappingException)

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