Search in sources :

Example 76 with MappingException

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

the class SimpleValue method getType.

public Type getType() throws MappingException {
    if (type != null) {
        return type;
    }
    if (typeName == null) {
        throw new MappingException("No type name");
    }
    if (typeParameters != null && Boolean.valueOf(typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC)) && typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null) {
        createParameterImpl();
    }
    Type result = metadata.getTypeResolver().heuristicType(typeName, typeParameters);
    // instead of BinaryType (HHH-10413)
    if (isVersion && BinaryType.class.isInstance(result)) {
        log.debug("version is BinaryType; changing to RowVersionType");
        result = RowVersionType.INSTANCE;
    }
    if (result == null) {
        String msg = "Could not determine type for: " + typeName;
        if (table != null) {
            msg += ", at table: " + table.getName();
        }
        if (columns != null && columns.size() > 0) {
            msg += ", for columns: " + columns;
        }
        throw new MappingException(msg);
    }
    return result;
}
Also used : BinaryType(org.hibernate.type.BinaryType) DynamicParameterizedType(org.hibernate.usertype.DynamicParameterizedType) RowVersionType(org.hibernate.type.RowVersionType) Type(org.hibernate.type.Type) BinaryType(org.hibernate.type.BinaryType) MappingException(org.hibernate.MappingException)

Example 77 with MappingException

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

the class AbstractEntityPersister method getNaturalIdentifierSnapshot.

public Object[] getNaturalIdentifierSnapshot(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
    if (!hasNaturalIdentifier()) {
        throw new MappingException("persistent class did not define a natural-id : " + MessageHelper.infoString(this));
    }
    if (LOG.isTraceEnabled()) {
        LOG.tracev("Getting current natural-id snapshot state for: {0}", MessageHelper.infoString(this, id, getFactory()));
    }
    int[] naturalIdPropertyIndexes = getNaturalIdentifierProperties();
    int naturalIdPropertyCount = naturalIdPropertyIndexes.length;
    boolean[] naturalIdMarkers = new boolean[getPropertySpan()];
    Type[] extractionTypes = new Type[naturalIdPropertyCount];
    for (int i = 0; i < naturalIdPropertyCount; i++) {
        extractionTypes[i] = getPropertyTypes()[naturalIdPropertyIndexes[i]];
        naturalIdMarkers[naturalIdPropertyIndexes[i]] = true;
    }
    ///////////////////////////////////////////////////////////////////////
    // TODO : look at perhaps caching this...
    Select select = new Select(getFactory().getDialect());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("get current natural-id state " + getEntityName());
    }
    select.setSelectClause(concretePropertySelectFragmentSansLeadingComma(getRootAlias(), naturalIdMarkers));
    select.setFromClause(fromTableFragment(getRootAlias()) + fromJoinFragment(getRootAlias(), true, false));
    String[] aliasedIdColumns = StringHelper.qualify(getRootAlias(), getIdentifierColumnNames());
    String whereClause = new StringBuilder().append(StringHelper.join("=? and ", aliasedIdColumns)).append("=?").append(whereJoinFragment(getRootAlias(), true, false)).toString();
    String sql = select.setOuterJoins("", "").setWhereClause(whereClause).toStatementString();
    ///////////////////////////////////////////////////////////////////////
    Object[] snapshot = new Object[naturalIdPropertyCount];
    try {
        PreparedStatement ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
        try {
            getIdentifierType().nullSafeSet(ps, id, 1, session);
            ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(ps);
            try {
                //if there is no resulting row, return null
                if (!rs.next()) {
                    return null;
                }
                final EntityKey key = session.generateEntityKey(id, this);
                Object owner = session.getPersistenceContext().getEntity(key);
                for (int i = 0; i < naturalIdPropertyCount; i++) {
                    snapshot[i] = extractionTypes[i].hydrate(rs, getPropertyAliases("", naturalIdPropertyIndexes[i]), session, null);
                    if (extractionTypes[i].isEntityType()) {
                        snapshot[i] = extractionTypes[i].resolve(snapshot[i], session, owner);
                    }
                }
                return snapshot;
            } finally {
                session.getJdbcCoordinator().getResourceRegistry().release(rs, ps);
            }
        } finally {
            session.getJdbcCoordinator().getResourceRegistry().release(ps);
            session.getJdbcCoordinator().afterStatementExecution();
        }
    } catch (SQLException e) {
        throw getFactory().getSQLExceptionHelper().convert(e, "could not retrieve snapshot: " + MessageHelper.infoString(this, id, getFactory()), sql);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) MappingException(org.hibernate.MappingException) EntityKey(org.hibernate.engine.spi.EntityKey) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) VersionType(org.hibernate.type.VersionType) Type(org.hibernate.type.Type) SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select) ResultSet(java.sql.ResultSet)

Example 78 with MappingException

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

the class PersistentClass method validate.

public void validate(Mapping mapping) throws MappingException {
    Iterator iter = getPropertyIterator();
    while (iter.hasNext()) {
        Property prop = (Property) iter.next();
        if (!prop.isValid(mapping)) {
            throw new MappingException("property mapping has wrong number of columns: " + StringHelper.qualify(getEntityName(), prop.getName()) + " type: " + prop.getType().getName());
        }
    }
    checkPropertyDuplication();
    checkColumnDuplication();
}
Also used : JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) SingletonIterator(org.hibernate.internal.util.collections.SingletonIterator) EmptyIterator(org.hibernate.internal.util.collections.EmptyIterator) MappingException(org.hibernate.MappingException)

Example 79 with MappingException

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

the class PersistentClass method checkPropertyDuplication.

private void checkPropertyDuplication() throws MappingException {
    HashSet<String> names = new HashSet<String>();
    Iterator iter = getPropertyIterator();
    while (iter.hasNext()) {
        Property prop = (Property) iter.next();
        if (!names.add(prop.getName())) {
            throw new MappingException("Duplicate property mapping of " + prop.getName() + " found in " + getEntityName());
        }
    }
}
Also used : JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) SingletonIterator(org.hibernate.internal.util.collections.SingletonIterator) EmptyIterator(org.hibernate.internal.util.collections.EmptyIterator) HashSet(java.util.HashSet) MappingException(org.hibernate.MappingException)

Example 80 with MappingException

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

the class QueryHintDefinition method getCacheMode.

public CacheMode getCacheMode(String query) {
    String hitName = QueryHints.CACHE_MODE;
    String value = (String) hintsMap.get(hitName);
    if (value == null) {
        return null;
    }
    try {
        return CacheMode.interpretExternalSetting(value);
    } catch (MappingException e) {
        throw new AnnotationException("Unknown CacheMode in hint: " + query + ":" + hitName, e);
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException) 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