Search in sources :

Example 26 with HibernateException

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

the class BeanValidationIntegrator method integrate.

@Override
public void integrate(final Metadata metadata, final SessionFactoryImplementor sessionFactory, final SessionFactoryServiceRegistry serviceRegistry) {
    final ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
    // IMPL NOTE : see the comments on ActivationContext.getValidationModes() as to why this is multi-valued...
    final Set<ValidationMode> modes = ValidationMode.getModes(cfgService.getSettings().get(MODE_PROPERTY));
    if (modes.size() > 1) {
        LOG.multipleValidationModes(ValidationMode.loggable(modes));
    }
    if (modes.size() == 1 && modes.contains(ValidationMode.NONE)) {
        // we have nothing to do; just return
        return;
    }
    final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
    // see if the Bean Validation API is available on the classpath
    if (isBeanValidationApiAvailable(classLoaderService)) {
        // and if so, call out to the TypeSafeActivator
        try {
            final Class typeSafeActivatorClass = loadTypeSafeActivatorClass(classLoaderService);
            @SuppressWarnings("unchecked") final Method activateMethod = typeSafeActivatorClass.getMethod(ACTIVATE_METHOD_NAME, ActivationContext.class);
            final ActivationContext activationContext = new ActivationContext() {

                @Override
                public Set<ValidationMode> getValidationModes() {
                    return modes;
                }

                @Override
                public Metadata getMetadata() {
                    return metadata;
                }

                @Override
                public SessionFactoryImplementor getSessionFactory() {
                    return sessionFactory;
                }

                @Override
                public SessionFactoryServiceRegistry getServiceRegistry() {
                    return serviceRegistry;
                }
            };
            try {
                activateMethod.invoke(null, activationContext);
            } catch (InvocationTargetException e) {
                if (HibernateException.class.isInstance(e.getTargetException())) {
                    throw ((HibernateException) e.getTargetException());
                }
                throw new IntegrationException("Error activating Bean Validation integration", e.getTargetException());
            } catch (Exception e) {
                throw new IntegrationException("Error activating Bean Validation integration", e);
            }
        } catch (NoSuchMethodException e) {
            throw new HibernateException("Unable to locate TypeSafeActivator#activate method", e);
        }
    } else {
        // otherwise check the validation modes
        // todo : in many ways this duplicates thew checks done on the TypeSafeActivator when a ValidatorFactory could not be obtained
        validateMissingBeanValidationApi(modes);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 27 with HibernateException

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

the class BeanValidationIntegrator method validateFactory.

/**
	 * Used to validate the type of an explicitly passed ValidatorFactory instance
	 *
	 * @param object The supposed ValidatorFactory instance
	 */
@SuppressWarnings("unchecked")
public static void validateFactory(Object object) {
    try {
        // this direct usage of ClassLoader should be fine since the classes exist in the same jar
        final Class activatorClass = BeanValidationIntegrator.class.getClassLoader().loadClass(ACTIVATOR_CLASS_NAME);
        try {
            final Method validateMethod = activatorClass.getMethod(VALIDATE_SUPPLIED_FACTORY_METHOD_NAME, Object.class);
            validateMethod.setAccessible(true);
            try {
                validateMethod.invoke(null, object);
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof HibernateException) {
                    throw (HibernateException) e.getTargetException();
                }
                throw new HibernateException("Unable to check validity of passed ValidatorFactory", e);
            } catch (IllegalAccessException e) {
                throw new HibernateException("Unable to check validity of passed ValidatorFactory", e);
            }
        } catch (HibernateException e) {
            throw e;
        } catch (Exception e) {
            throw new HibernateException("Could not locate method needed for ValidatorFactory validation", e);
        }
    } catch (HibernateException e) {
        throw e;
    } catch (Exception e) {
        throw new HibernateException("Could not locate TypeSafeActivator class", e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 28 with HibernateException

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

the class JTASessionContext method currentSession.

@Override
public Session currentSession() throws HibernateException {
    final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService(JtaPlatform.class);
    final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    if (transactionManager == null) {
        throw new HibernateException("No TransactionManagerLookup specified");
    }
    Transaction txn;
    try {
        txn = transactionManager.getTransaction();
        if (txn == null) {
            throw new HibernateException("Unable to locate current JTA transaction");
        }
        if (!JtaStatusHelper.isActive(txn.getStatus())) {
            // entries cleaned up (aside from spawning threads).
            throw new HibernateException("Current transaction is not in progress");
        }
    } catch (HibernateException e) {
        throw e;
    } catch (Throwable t) {
        throw new HibernateException("Problem locating/validating JTA transaction", t);
    }
    final Object txnIdentifier = jtaPlatform.getTransactionIdentifier(txn);
    Session currentSession = currentSessionMap.get(txnIdentifier);
    if (currentSession == null) {
        currentSession = buildOrObtainSession();
        try {
            txn.registerSynchronization(buildCleanupSynch(txnIdentifier));
        } catch (Throwable t) {
            try {
                currentSession.close();
            } catch (Throwable ignore) {
                LOG.debug("Unable to release generated current-session on failed synch registration", ignore);
            }
            throw new HibernateException("Unable to register cleanup Synchronization with TransactionManager");
        }
        currentSessionMap.put(txnIdentifier, currentSession);
    } else {
        validateExistingSession(currentSession);
    }
    return currentSession;
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) HibernateException(org.hibernate.HibernateException) TransactionManager(javax.transaction.TransactionManager) Session(org.hibernate.Session)

Example 29 with HibernateException

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

the class AbstractEmptinessExpression method getQueryableCollection.

protected QueryableCollection getQueryableCollection(String entityName, String propertyName, SessionFactoryImplementor factory) throws HibernateException {
    final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister(entityName);
    final Type type = ownerMapping.toType(propertyName);
    if (!type.isCollectionType()) {
        throw new MappingException("Property path [" + entityName + "." + propertyName + "] does not reference a collection");
    }
    final String role = ((CollectionType) type).getRole();
    try {
        return (QueryableCollection) factory.getCollectionPersister(role);
    } catch (ClassCastException cce) {
        throw new QueryException("collection role is not queryable: " + role);
    } catch (Exception e) {
        throw new QueryException("collection role not found: " + role);
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) CollectionType(org.hibernate.type.CollectionType) PropertyMapping(org.hibernate.persister.entity.PropertyMapping) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 30 with HibernateException

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

the class DialectFactoryTest method testBuildDialectByProperties.

@Test
public void testBuildDialectByProperties() {
    Properties props = new Properties();
    try {
        dialectFactory.buildDialect(props, null);
        fail();
    } catch (HibernateException e) {
        assertNull(e.getCause());
    }
    props.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
    assertEquals(HSQLDialect.class, dialectFactory.buildDialect(props, null).getClass());
}
Also used : HibernateException(org.hibernate.HibernateException) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

HibernateException (org.hibernate.HibernateException)609 Session (org.hibernate.Session)285 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 DAOException (org.jbei.ice.storage.DAOException)122 ArrayList (java.util.ArrayList)63 Criteria (org.hibernate.Criteria)56 Test (org.junit.Test)43 Transaction (org.hibernate.Transaction)39 SQLException (java.sql.SQLException)38 SimpleDateFormat (java.text.SimpleDateFormat)22 Query (org.hibernate.Query)20 IOException (java.io.IOException)19 ParseException (java.text.ParseException)18 TestForIssue (org.hibernate.testing.TestForIssue)16 Date (java.util.Date)13 List (java.util.List)12 PersistenceException (org.mifos.framework.exceptions.PersistenceException)12 Serializable (java.io.Serializable)11 HashMap (java.util.HashMap)11 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)11