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);
}
}
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);
}
}
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;
}
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);
}
}
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());
}
Aggregations