Search in sources :

Example 51 with HibernateException

use of org.hibernate.HibernateException 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 52 with HibernateException

use of org.hibernate.HibernateException 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 53 with HibernateException

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

the class StreamCopier method copy.

public static long copy(InputStream from, OutputStream into) {
    try {
        long totalRead = 0;
        while (true) {
            synchronized (BUFFER) {
                int amountRead = from.read(BUFFER);
                if (amountRead == -1) {
                    break;
                }
                into.write(BUFFER, 0, amountRead);
                totalRead += amountRead;
                if (amountRead < BUFFER_SIZE) {
                    // should mean there is no more data in the stream, no need for next read
                    break;
                }
            }
        }
        return totalRead;
    } catch (IOException e) {
        throw new HibernateException("Unable to copy stream content", e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) IOException(java.io.IOException)

Example 54 with HibernateException

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

the class Cloneable method checkListeners.

private void checkListeners() {
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(getClass(), Object.class);
        internalCheckListeners(beanInfo);
    } catch (IntrospectionException t) {
        throw new HibernateException("Unable to validate listener config", t);
    } finally {
        if (beanInfo != null) {
            // release the jdk internal caches everytime to ensure this
            // plays nicely with destroyable class-loaders
            Introspector.flushFromCaches(getClass());
        }
    }
}
Also used : HibernateException(org.hibernate.HibernateException) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 55 with HibernateException

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

the class ConfigHelper method getUserResourceAsStream.

public static InputStream getUserResourceAsStream(String resource) {
    boolean hasLeadingSlash = resource.startsWith("/");
    String stripped = hasLeadingSlash ? resource.substring(1) : resource;
    InputStream stream = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        stream = classLoader.getResourceAsStream(resource);
        if (stream == null && hasLeadingSlash) {
            stream = classLoader.getResourceAsStream(stripped);
        }
    }
    if (stream == null) {
        stream = Environment.class.getClassLoader().getResourceAsStream(resource);
    }
    if (stream == null && hasLeadingSlash) {
        stream = Environment.class.getClassLoader().getResourceAsStream(stripped);
    }
    if (stream == null) {
        throw new HibernateException(resource + " not found");
    }
    return stream;
}
Also used : HibernateException(org.hibernate.HibernateException) InputStream(java.io.InputStream)

Aggregations

HibernateException (org.hibernate.HibernateException)372 DAOException (org.jbei.ice.storage.DAOException)141 Session (org.hibernate.Session)72 Test (org.junit.Test)41 ArrayList (java.util.ArrayList)30 SQLException (java.sql.SQLException)27 IOException (java.io.IOException)15 TestForIssue (org.hibernate.testing.TestForIssue)15 Transaction (org.hibernate.Transaction)14 Group (org.jbei.ice.storage.model.Group)14 Type (org.hibernate.type.Type)12 PersistenceException (org.mifos.framework.exceptions.PersistenceException)12 Serializable (java.io.Serializable)11 EntityEntry (org.hibernate.engine.spi.EntityEntry)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)10 HashMap (java.util.HashMap)9 Method (java.lang.reflect.Method)8 Dialect (org.hibernate.dialect.Dialect)8 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)8 HibernateProxy (org.hibernate.proxy.HibernateProxy)8