@Override public RuntimeException mapStatusCheckFailure(String message, SystemException systemException, SessionImplementor session) { throw new PersistenceException( message, systemException ); }
private EntityManagerFactory buildNativeEntityManagerFactory() { EntityManagerFactory emf; try { emf = createNativeEntityManagerFactory(); } catch (PersistenceException ex) { if (ex.getClass() == PersistenceException.class) { // Plain PersistenceException wrapper for underlying exception? // Make sure the nested exception message is properly exposed, // along the lines of Spring's NestedRuntimeException.getMessage() Throwable cause = ex.getCause(); if (cause != null) { String message = ex.getMessage(); String causeString = cause.toString(); if (!message.endsWith(causeString)) { throw new PersistenceException(message + "; nested exception is " + causeString, cause); } } } throw ex; } JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter(); if (jpaVendorAdapter != null) { jpaVendorAdapter.postProcessEntityManagerFactory(emf); } if (logger.isInfoEnabled()) { logger.info("Initialized JPA EntityManagerFactory for persistence unit '" + getPersistenceUnitName() + "'"); } return emf; }
@Provider public class PersistenceMapper implements ExceptionMapper<PersistenceException> { @Override public Response toResponse(PersistenceException arg0) { if(arg0.getCause() instanceof InvalidDataException) { return Response.status(Response.Status.BAD_REQUEST).build(); } else { ... } } }
public void flush() { if (entityManager != null && (!handleTransactions || isTransactionActive())) { try { entityManager.flush(); } catch (IllegalStateException ise) { throw new ActivitiException("Error while flushing EntityManager, illegal state", ise); } catch (TransactionRequiredException tre) { throw new ActivitiException("Cannot flush EntityManager, an active transaction is required", tre); } catch (PersistenceException pe) { throw new ActivitiException("Error while flushing EntityManager: " + pe.getMessage(), pe); } } }
/** * Trigger a flush on the given Hibernate Session, converting regular * {@link HibernateException} instances as well as Hibernate 5.2's * {@link PersistenceException} wrappers accordingly. * @param session the Hibernate Session to flush * @param synch whether this flush is triggered by transaction synchronization * @throws DataAccessException in case of flush failures * @since 4.3.2 */ static void flush(Session session, boolean synch) throws DataAccessException { if (synch) { logger.debug("Flushing Hibernate Session on transaction synchronization"); } else { logger.debug("Flushing Hibernate Session on explicit request"); } try { session.flush(); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } catch (PersistenceException ex) { if (ex.getCause() instanceof HibernateException) { throw convertHibernateAccessException((HibernateException) ex.getCause()); } throw ex; } }
assertTrue(ex.getMessage().contains("changed an immutable collection instance")); log.debug("success"); fail(); } catch (PersistenceException e) { assertTrue(e.getMessage().contains("changed an immutable collection instance")); log.debug("success");
@Override public void additionalMethod(boolean fail) { if (fail) { throw new PersistenceException("my failure"); } } }
private EntityManagerFactory buildNativeEntityManagerFactory() { EntityManagerFactory emf; try { emf = createNativeEntityManagerFactory(); } catch (PersistenceException ex) { if (ex.getClass() == PersistenceException.class) { // Plain PersistenceException wrapper for underlying exception? // Make sure the nested exception message is properly exposed, // along the lines of Spring's NestedRuntimeException.getMessage() Throwable cause = ex.getCause(); if (cause != null) { String message = ex.getMessage(); String causeString = cause.toString(); if (!message.endsWith(causeString)) { throw new PersistenceException(message + "; nested exception is " + causeString, cause); } } } throw ex; } JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter(); if (jpaVendorAdapter != null) { jpaVendorAdapter.postProcessEntityManagerFactory(emf); } if (logger.isInfoEnabled()) { logger.info("Initialized JPA EntityManagerFactory for persistence unit '" + getPersistenceUnitName() + "'"); } return emf; }
@Override public void flush() { try { getSessionHolder().getSession().flush(); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } catch (PersistenceException ex) { if (ex.getCause() instanceof HibernateException) { throw convertHibernateAccessException((HibernateException) ex.getCause()); } throw ex; } } }
assertTrue( e.getMessage().contains( "Transaction was rolled back in a different thread!" ) );
private HashMap<Class, Callback[]> determineAppropriateCallbackMap(CallbackType callbackType) { if ( callbackType == CallbackType.PRE_PERSIST ) { return preCreates; } if ( callbackType == CallbackType.POST_PERSIST ) { return postCreates; } if ( callbackType == CallbackType.PRE_REMOVE ) { return preRemoves; } if ( callbackType == CallbackType.POST_REMOVE ) { return postRemoves; } if ( callbackType == CallbackType.PRE_UPDATE ) { return preUpdates; } if ( callbackType == CallbackType.POST_UPDATE ) { return postUpdates; } if ( callbackType == CallbackType.POST_LOAD ) { return postLoads; } throw new PersistenceException( "Unrecognized JPA callback type [" + callbackType + "]" ); }
/** * Trigger a flush on the given Hibernate Session, converting regular * {@link HibernateException} instances as well as Hibernate 5.2's * {@link PersistenceException} wrappers accordingly. * @param session the Hibernate Session to flush * @param synch whether this flush is triggered by transaction synchronization * @throws DataAccessException in case of flush failures * @since 4.3.2 */ static void flush(Session session, boolean synch) throws DataAccessException { if (synch) { logger.debug("Flushing Hibernate Session on transaction synchronization"); } else { logger.debug("Flushing Hibernate Session on explicit request"); } try { session.flush(); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } catch (PersistenceException ex) { if (ex.getCause() instanceof HibernateException) { throw convertHibernateAccessException((HibernateException) ex.getCause()); } throw ex; } }
private InputStream openInputStream() { try { return persistenceBundle.getResource( resource ).openStream(); } catch ( IOException e ) { throw new PersistenceException( "Unable to open an InputStream on the OSGi Bundle resource!", e ); } }
@Test(expected = ConstraintViolationException.class) public void updateSamWithNullDriversLicenseWithSessionFlush() throws Throwable { updateSamWithNullDriversLicense(); // Manual flush is required to avoid false positive in test try { sessionFactory.getCurrentSession().flush(); } catch (PersistenceException ex) { // Wrapped in Hibernate 5.2, with the constraint violation as cause throw ex.getCause(); } }
@Override @SuppressWarnings("unchecked") public <T> T unwrap(Class<T> cls) { if ( org.hibernate.Cache.class.isAssignableFrom( cls ) ) { return (T) this; } if ( RegionFactory.class.isAssignableFrom( cls ) ) { return (T) regionFactory; } throw new PersistenceException( "Hibernate cannot unwrap Cache as " + cls.getName() ); }
@Override public void flush() { try { getSessionHolder().getSession().flush(); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } catch (PersistenceException ex) { if (ex.getCause() instanceof HibernateException) { throw convertHibernateAccessException((HibernateException) ex.getCause()); } throw ex; } } }
private PersistenceException persistenceException(String message, Exception cause) { return new PersistenceException( getExceptionHeader() + message, cause ); }
@Override protected void doCommit(DefaultTransactionStatus status) { HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); Transaction hibTx = txObject.getSessionHolder().getTransaction(); Assert.state(hibTx != null, "No Hibernate transaction"); if (status.isDebug()) { logger.debug("Committing Hibernate transaction on Session [" + txObject.getSessionHolder().getSession() + "]"); } try { hibTx.commit(); } catch (org.hibernate.TransactionException ex) { // assumably from commit call to the underlying JDBC connection throw new TransactionSystemException("Could not commit Hibernate transaction", ex); } catch (HibernateException ex) { // assumably failed to flush changes to database throw convertHibernateAccessException(ex); } catch (PersistenceException ex) { if (ex.getCause() instanceof HibernateException) { throw convertHibernateAccessException((HibernateException) ex.getCause()); } throw ex; } }
@Override public <T> T unwrap(Class<T> type) { if ( type.isAssignableFrom( SessionFactory.class ) ) { return type.cast( this ); } if ( type.isAssignableFrom( SessionFactoryImplementor.class ) ) { return type.cast( this ); } if ( type.isAssignableFrom( SessionFactoryImpl.class ) ) { return type.cast( this ); } if ( type.isAssignableFrom( EntityManagerFactory.class ) ) { return type.cast( this ); } throw new PersistenceException( "Hibernate cannot unwrap EntityManagerFactory as '" + type.getName() + "'" ); }