Search in sources :

Example 71 with MappingException

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

the class InFlightMetadataCollectorImpl 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) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException)

Example 72 with MappingException

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

the class TypeSafeActivator method findPropertyByName.

/**
	 * Locate the property by path in a recursive way, including IdentifierProperty in the loop if propertyName is
	 * {@code null}.  If propertyName is {@code null} or empty, the IdentifierProperty is returned
	 */
private 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) MappingException(org.hibernate.MappingException)

Example 73 with MappingException

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

the class QueryTranslatorImpl method addFromAssociation.

/**
	 * Used for collection filters
	 */
private void addFromAssociation(final String elementName, final String collectionRole) throws QueryException {
    //q.addCollection(collectionName, collectionRole);
    QueryableCollection persister = getCollectionPersister(collectionRole);
    Type collectionElementType = persister.getElementType();
    if (!collectionElementType.isEntityType()) {
        throw new QueryException("collection of values in filter: " + elementName);
    }
    String[] keyColumnNames = persister.getKeyColumnNames();
    //if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);
    String collectionName;
    JoinSequence join = new JoinSequence(getFactory());
    collectionName = persister.isOneToMany() ? elementName : createNameForCollection(collectionRole);
    join.setRoot(persister, collectionName);
    if (!persister.isOneToMany()) {
        //many-to-many
        addCollection(collectionName, collectionRole);
        try {
            join.addJoin((AssociationType) persister.getElementType(), elementName, JoinType.INNER_JOIN, persister.getElementColumnNames(collectionName));
        } catch (MappingException me) {
            throw new QueryException(me);
        }
    }
    join.addCondition(collectionName, keyColumnNames, " = ?");
    //if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
    EntityType elemType = (EntityType) collectionElementType;
    addFrom(elementName, elemType.getAssociatedEntityName(), join);
}
Also used : EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence) MappingException(org.hibernate.MappingException)

Example 74 with MappingException

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

the class LiteralProcessor method setConstantValue.

private void setConstantValue(DotNode node, String text, Object value) {
    if (LOG.isDebugEnabled()) {
        LOG.debugf("setConstantValue() %s -> %s %s", text, value, value.getClass().getName());
    }
    // Chop off the rest of the tree.
    node.setFirstChild(null);
    if (value instanceof String) {
        node.setType(SqlTokenTypes.QUOTED_STRING);
    } else if (value instanceof Character) {
        node.setType(SqlTokenTypes.QUOTED_STRING);
    } else if (value instanceof Byte) {
        node.setType(SqlTokenTypes.NUM_INT);
    } else if (value instanceof Short) {
        node.setType(SqlTokenTypes.NUM_INT);
    } else if (value instanceof Integer) {
        node.setType(SqlTokenTypes.NUM_INT);
    } else if (value instanceof Long) {
        node.setType(SqlTokenTypes.NUM_LONG);
    } else if (value instanceof Double) {
        node.setType(SqlTokenTypes.NUM_DOUBLE);
    } else if (value instanceof Float) {
        node.setType(SqlTokenTypes.NUM_FLOAT);
    } else {
        node.setType(SqlTokenTypes.CONSTANT);
    }
    Type type;
    try {
        type = walker.getSessionFactoryHelper().getFactory().getTypeResolver().heuristicType(value.getClass().getName());
    } catch (MappingException me) {
        throw new QueryException(me);
    }
    if (type == null) {
        throw new QueryException(QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + node.getText());
    }
    try {
        LiteralType literalType = (LiteralType) type;
        Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
        //noinspection unchecked
        node.setText(literalType.objectToSQLString(value, dialect));
    } catch (Exception e) {
        throw new QueryException(QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + node.getText(), e);
    }
    node.setDataType(type);
    node.setResolvedConstant(text);
}
Also used : LiteralType(org.hibernate.type.LiteralType) SemanticException(antlr.SemanticException) InvalidPathException(org.hibernate.hql.internal.ast.InvalidPathException) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException) BigInteger(java.math.BigInteger) LiteralType(org.hibernate.type.LiteralType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) Dialect(org.hibernate.dialect.Dialect)

Example 75 with MappingException

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

the class SimpleValue method createParameterImpl.

private void createParameterImpl() {
    try {
        String[] columnsNames = new String[columns.size()];
        for (int i = 0; i < columns.size(); i++) {
            Selectable column = columns.get(i);
            if (column instanceof Column) {
                columnsNames[i] = ((Column) column).getName();
            }
        }
        final XProperty xProperty = (XProperty) typeParameters.get(DynamicParameterizedType.XPROPERTY);
        // todo : not sure this works for handling @MapKeyEnumerated
        final Annotation[] annotations = xProperty == null ? null : xProperty.getAnnotations();
        final ClassLoaderService classLoaderService = getMetadata().getMetadataBuildingOptions().getServiceRegistry().getService(ClassLoaderService.class);
        typeParameters.put(DynamicParameterizedType.PARAMETER_TYPE, new ParameterTypeImpl(classLoaderService.classForName(typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)), annotations, table.getCatalog(), table.getSchema(), table.getName(), Boolean.valueOf(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)), columnsNames));
    } catch (ClassLoadingException e) {
        throw new MappingException("Could not create DynamicParameterizedType for type: " + typeName, e);
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Annotation(java.lang.annotation.Annotation) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) 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