Search in sources :

Example 26 with MappingException

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

the class SessionImpl method contains.

@Override
public boolean contains(String entityName, Object object) {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        return false;
    }
    try {
        if (!HibernateProxy.class.isInstance(object) && persistenceContext.getEntry(object) == null) {
            // check if it is an entity -> if not throw an exception (per JPA)
            try {
                getSessionFactory().getMetamodel().entityPersister(entityName);
            } catch (HibernateException e) {
                throw new IllegalArgumentException("Not an entity [" + entityName + "] : " + object);
            }
        }
        if (object instanceof HibernateProxy) {
            //do not use proxiesByKey, since not all
            //proxies that point to this session's
            //instances are in that collection!
            LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                //the underlying instance will be "contained"
                return li.getSession() == this;
            } else {
                //if it is initialized, see if the underlying
                //instance is contained, since we need to
                //account for the fact that it might have been
                //evicted
                object = li.getImplementation();
            }
        }
        // A session is considered to contain an entity only if the entity has
        // an entry in the session's persistence context and the entry reports
        // that the entity has not been removed
        EntityEntry entry = persistenceContext.getEntry(object);
        delayedAfterCompletion();
        return entry != null && entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
    } catch (MappingException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 27 with MappingException

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

the class SessionImpl method contains.

@Override
public boolean contains(Object object) {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        return false;
    }
    try {
        if (object instanceof HibernateProxy) {
            //do not use proxiesByKey, since not all
            //proxies that point to this session's
            //instances are in that collection!
            LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                //the underlying instance will be "contained"
                return li.getSession() == this;
            } else {
                //if it is initialized, see if the underlying
                //instance is contained, since we need to
                //account for the fact that it might have been
                //evicted
                object = li.getImplementation();
            }
        }
        // A session is considered to contain an entity only if the entity has
        // an entry in the session's persistence context and the entry reports
        // that the entity has not been removed
        EntityEntry entry = persistenceContext.getEntry(object);
        delayedAfterCompletion();
        if (entry == null) {
            if (!HibernateProxy.class.isInstance(object) && persistenceContext.getEntry(object) == null) {
                // check if it is even an entity -> if not throw an exception (per JPA)
                try {
                    final String entityName = getEntityNameResolver().resolveEntityName(object);
                    if (entityName == null) {
                        throw new IllegalArgumentException("Could not resolve entity-name [" + object + "]");
                    }
                    getSessionFactory().getMetamodel().entityPersister(object.getClass());
                } catch (HibernateException e) {
                    throw new IllegalArgumentException("Not an entity [" + object.getClass() + "]", e);
                }
            }
            return false;
        } else {
            return entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
        }
    } catch (MappingException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 28 with MappingException

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

the class CallbackBuilderLegacyImpl method buildCallbacksForEntity.

@Override
public void buildCallbacksForEntity(String entityClassName, CallbackRegistrar callbackRegistrar) {
    try {
        final XClass entityXClass = reflectionManager.classForName(entityClassName);
        final Class entityClass = reflectionManager.toClass(entityXClass);
        for (CallbackType callbackType : CallbackType.values()) {
            if (callbackRegistrar.hasRegisteredCallbacks(entityClass, callbackType)) {
                // this most likely means we have a class mapped multiple times using the hbm.xml
                // "entity name" feature
                log.debugf("CallbackRegistry reported that Class [%s] already had %s callbacks registered; " + "assuming this means the class was mapped twice " + "(using hbm.xml entity-name support) - skipping subsequent registrations", entityClassName, callbackType.getCallbackAnnotation().getSimpleName());
                continue;
            }
            final Callback[] callbacks = resolveCallbacks(entityXClass, callbackType, reflectionManager);
            callbackRegistrar.registerCallbacks(entityClass, callbacks);
        }
    } catch (ClassLoadingException e) {
        throw new MappingException("entity class not found: " + entityClassName, e);
    }
}
Also used : Callback(org.hibernate.jpa.event.spi.jpa.Callback) CallbackType(org.hibernate.jpa.event.spi.jpa.CallbackType) ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) XClass(org.hibernate.annotations.common.reflection.XClass) XClass(org.hibernate.annotations.common.reflection.XClass) MappingException(org.hibernate.MappingException)

Example 29 with MappingException

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

the class Collection method getComparator.

public Comparator getComparator() {
    if (comparator == null && comparatorClassName != null) {
        try {
            final ClassLoaderService classLoaderService = getMetadata().getMetadataBuildingOptions().getServiceRegistry().getService(ClassLoaderService.class);
            setComparator((Comparator) classLoaderService.classForName(comparatorClassName).newInstance());
        } catch (Exception e) {
            throw new MappingException("Could not instantiate comparator class [" + comparatorClassName + "] for collection " + getRole());
        }
    }
    return comparator;
}
Also used : MappingException(org.hibernate.MappingException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) MappingException(org.hibernate.MappingException)

Example 30 with MappingException

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

the class ForeignKey method alignColumns.

private void alignColumns(Table referencedTable) {
    final int referencedPkColumnSpan = referencedTable.getPrimaryKey().getColumnSpan();
    if (referencedPkColumnSpan != getColumnSpan()) {
        StringBuilder sb = new StringBuilder();
        sb.append("Foreign key (").append(getName()).append(":").append(getTable().getName()).append(" [");
        appendColumns(sb, getColumnIterator());
        sb.append("])").append(") must have same number of columns as the referenced primary key (").append(referencedTable.getName()).append(" [");
        appendColumns(sb, referencedTable.getPrimaryKey().getColumnIterator());
        sb.append("])");
        throw new MappingException(sb.toString());
    }
    Iterator fkCols = getColumnIterator();
    Iterator pkCols = referencedTable.getPrimaryKey().getColumnIterator();
    while (pkCols.hasNext()) {
        ((Column) fkCols.next()).setLength(((Column) pkCols.next()).getLength());
    }
}
Also used : Iterator(java.util.Iterator) 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