Search in sources :

Example 31 with MappingException

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

the class PersistentClass method getRecursiveProperty.

private Property getRecursiveProperty(String propertyPath, Iterator iter) throws MappingException {
    Property property = null;
    StringTokenizer st = new StringTokenizer(propertyPath, ".", false);
    try {
        while (st.hasMoreElements()) {
            final String element = (String) st.nextElement();
            if (property == null) {
                Property identifierProperty = getIdentifierProperty();
                if (identifierProperty != null && identifierProperty.getName().equals(element)) {
                    // we have a mapped identifier property and the root of
                    // the incoming property path matched that identifier
                    // property
                    property = identifierProperty;
                } else if (identifierProperty == null && getIdentifierMapper() != null) {
                    // we have an embedded composite identifier
                    try {
                        identifierProperty = getProperty(element, getIdentifierMapper().getPropertyIterator());
                        if (identifierProperty != null) {
                            // the root of the incoming property path matched one
                            // of the embedded composite identifier properties
                            property = identifierProperty;
                        }
                    } catch (MappingException ignore) {
                    // ignore it...
                    }
                }
                if (property == null) {
                    property = getProperty(element, iter);
                }
            } else {
                //flat recursive algorithm
                property = ((Component) property.getValue()).getProperty(element);
            }
        }
    } catch (MappingException e) {
        throw new MappingException("property [" + propertyPath + "] not found on entity [" + getEntityName() + "]");
    }
    return property;
}
Also used : StringTokenizer(java.util.StringTokenizer) MappingException(org.hibernate.MappingException)

Example 32 with MappingException

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

the class AbstractEntityPersister method collectAttributeDefinitions.

private void collectAttributeDefinitions(Map<String, AttributeDefinition> attributeDefinitionsByName, EntityMetamodel metamodel) {
    for (int i = 0; i < metamodel.getPropertySpan(); i++) {
        final AttributeDefinition attributeDefinition = metamodel.getProperties()[i];
        // Don't replace an attribute definition if it is already in attributeDefinitionsByName
        // because the new value will be from a subclass.
        final AttributeDefinition oldAttributeDefinition = attributeDefinitionsByName.get(attributeDefinition.getName());
        if (oldAttributeDefinition != null) {
            if (LOG.isTraceEnabled()) {
                LOG.tracef("Ignoring subclass attribute definition [%s.%s] because it is defined in a superclass ", entityMetamodel.getName(), attributeDefinition.getName());
            }
        } else {
            attributeDefinitionsByName.put(attributeDefinition.getName(), attributeDefinition);
        }
    }
    // see if there are any subclass persisters...
    final Set<String> subClassEntityNames = metamodel.getSubclassEntityNames();
    if (subClassEntityNames == null) {
        return;
    }
    // see if we can find the persisters...
    for (String subClassEntityName : subClassEntityNames) {
        if (metamodel.getName().equals(subClassEntityName)) {
            // skip it
            continue;
        }
        try {
            final EntityPersister subClassEntityPersister = factory.getEntityPersister(subClassEntityName);
            collectAttributeDefinitions(attributeDefinitionsByName, subClassEntityPersister.getEntityMetamodel());
        } catch (MappingException e) {
            throw new IllegalStateException(String.format("Could not locate subclass EntityPersister [%s] while processing EntityPersister [%s]", subClassEntityName, metamodel.getName()), e);
        }
    }
}
Also used : AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) MappingException(org.hibernate.MappingException)

Example 33 with MappingException

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

the class AbstractPropertyMapping method initComponentPropertyPaths.

protected void initComponentPropertyPaths(final String path, final CompositeType type, final String[] columns, final String[] columnReaders, final String[] columnReaderTemplates, String[] formulaTemplates, final Mapping factory) throws MappingException {
    Type[] types = type.getSubtypes();
    String[] properties = type.getPropertyNames();
    int begin = 0;
    for (int i = 0; i < properties.length; i++) {
        String subpath = extendPath(path, properties[i]);
        try {
            int length = types[i].getColumnSpan(factory);
            String[] columnSlice = ArrayHelper.slice(columns, begin, length);
            String[] columnReaderSlice = ArrayHelper.slice(columnReaders, begin, length);
            String[] columnReaderTemplateSlice = ArrayHelper.slice(columnReaderTemplates, begin, length);
            String[] formulaSlice = formulaTemplates == null ? null : ArrayHelper.slice(formulaTemplates, begin, length);
            initPropertyPaths(subpath, types[i], columnSlice, columnReaderSlice, columnReaderTemplateSlice, formulaSlice, factory);
            begin += length;
        } catch (Exception e) {
            throw new MappingException("bug in initComponentPropertyPaths", e);
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 34 with MappingException

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

the class NamedQueryRepository method checkNamedQueries.

public Map<String, HibernateException> checkNamedQueries(QueryPlanCache queryPlanCache) {
    Map<String, HibernateException> errors = new HashMap<String, HibernateException>();
    // Check named HQL queries
    log.debugf("Checking %s named HQL queries", namedQueryDefinitionMap.size());
    for (NamedQueryDefinition namedQueryDefinition : namedQueryDefinitionMap.values()) {
        // this will throw an error if there's something wrong.
        try {
            log.debugf("Checking named query: %s", namedQueryDefinition.getName());
            //TODO: BUG! this currently fails for named queries for non-POJO entities
            queryPlanCache.getHQLQueryPlan(namedQueryDefinition.getQueryString(), false, Collections.EMPTY_MAP);
        } catch (HibernateException e) {
            errors.put(namedQueryDefinition.getName(), e);
        }
    }
    // Check native-sql queries
    log.debugf("Checking %s named SQL queries", namedSqlQueryDefinitionMap.size());
    for (NamedSQLQueryDefinition namedSQLQueryDefinition : namedSqlQueryDefinitionMap.values()) {
        // this will throw an error if there's something wrong.
        try {
            log.debugf("Checking named SQL query: %s", namedSQLQueryDefinition.getName());
            // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
            // currently not doable though because of the resultset-ref stuff...
            NativeSQLQuerySpecification spec;
            if (namedSQLQueryDefinition.getResultSetRef() != null) {
                ResultSetMappingDefinition definition = getResultSetMappingDefinition(namedSQLQueryDefinition.getResultSetRef());
                if (definition == null) {
                    throw new MappingException("Unable to find resultset-ref definition: " + namedSQLQueryDefinition.getResultSetRef());
                }
                spec = new NativeSQLQuerySpecification(namedSQLQueryDefinition.getQueryString(), definition.getQueryReturns(), namedSQLQueryDefinition.getQuerySpaces());
            } else {
                spec = new NativeSQLQuerySpecification(namedSQLQueryDefinition.getQueryString(), namedSQLQueryDefinition.getQueryReturns(), namedSQLQueryDefinition.getQuerySpaces());
            }
            queryPlanCache.getNativeSQLQueryPlan(spec);
        } catch (HibernateException e) {
            errors.put(namedSQLQueryDefinition.getName(), e);
        }
    }
    return errors;
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HashMap(java.util.HashMap) HibernateException(org.hibernate.HibernateException) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) NativeSQLQuerySpecification(org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition) MappingException(org.hibernate.MappingException)

Example 35 with MappingException

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

the class CollectionBinderTest method testAssociatedClassException.

@Test
@TestForIssue(jiraKey = "HHH-10106")
public void testAssociatedClassException() throws SQLException {
    final Collection collection = mock(Collection.class);
    final Map persistentClasses = mock(Map.class);
    final XClass collectionType = mock(XClass.class);
    final MetadataBuildingContext buildingContext = mock(MetadataBuildingContext.class);
    final InFlightMetadataCollector inFly = mock(InFlightMetadataCollector.class);
    final PersistentClass persistentClass = mock(PersistentClass.class);
    final Table table = mock(Table.class);
    when(buildingContext.getMetadataCollector()).thenReturn(inFly);
    when(persistentClasses.get(null)).thenReturn(null);
    when(collection.getOwner()).thenReturn(persistentClass);
    when(collectionType.getName()).thenReturn("List");
    when(persistentClass.getTable()).thenReturn(table);
    when(table.getName()).thenReturn("Hibernate");
    CollectionBinder collectionBinder = new CollectionBinder(false) {

        @Override
        protected Collection createCollection(PersistentClass persistentClass) {
            return null;
        }

        {
            final PropertyHolder propertyHolder = Mockito.mock(PropertyHolder.class);
            when(propertyHolder.getClassName()).thenReturn(CollectionBinderTest.class.getSimpleName());
            this.propertyName = "abc";
            this.propertyHolder = propertyHolder;
        }
    };
    String expectMessage = "Association [abc] for entity [CollectionBinderTest] references unmapped class [List]";
    try {
        collectionBinder.bindOneToManySecondPass(collection, persistentClasses, null, collectionType, false, false, buildingContext, null);
    } catch (MappingException e) {
        assertEquals(expectMessage, e.getMessage());
    }
}
Also used : InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) Table(org.hibernate.mapping.Table) PropertyHolder(org.hibernate.cfg.PropertyHolder) Collection(org.hibernate.mapping.Collection) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) Map(java.util.Map) XClass(org.hibernate.annotations.common.reflection.XClass) PersistentClass(org.hibernate.mapping.PersistentClass) MappingException(org.hibernate.MappingException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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