use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testSetReadOnlyAfterSessionClosedViaLazyInitializer.
@Test
public void testSetReadOnlyAfterSessionClosedViaLazyInitializer() {
DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
assertFalse(Hibernate.isInitialized(dp));
checkReadOnly(s, dp, false);
s.getTransaction().commit();
assertTrue(s.contains(dp));
s.close();
assertNull(((HibernateProxy) dp).getHibernateLazyInitializer().getSession());
try {
((HibernateProxy) dp).getHibernateLazyInitializer().setReadOnly(true);
fail("should have failed because session was detached");
} catch (TransientObjectException ex) {
// expected
assertFalse(((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnlySettingAvailable());
} finally {
s = openSession();
s.beginTransaction();
s.delete(dp);
s.getTransaction().commit();
s.close();
}
}
use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method isReadOnly.
@Override
public boolean isReadOnly(Object entityOrProxy) {
if (entityOrProxy == null) {
throw new AssertionFailure("object must be non-null.");
}
boolean isReadOnly;
if (entityOrProxy instanceof HibernateProxy) {
isReadOnly = ((HibernateProxy) entityOrProxy).getHibernateLazyInitializer().isReadOnly();
} else {
final EntityEntry ee = getEntry(entityOrProxy);
if (ee == null) {
throw new TransientObjectException("Instance was not associated with this persistence context");
}
isReadOnly = ee.isReadOnly();
}
return isReadOnly;
}
use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class ExceptionConverterImpl method convert.
@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
Throwable cause = e;
if (cause instanceof StaleStateException) {
final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
handlePersistenceException(converted);
return converted;
} else if (cause instanceof LockingStrategyException) {
final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
handlePersistenceException(converted);
return converted;
} else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
handlePersistenceException(converted);
return converted;
} else if (cause instanceof org.hibernate.PessimisticLockException) {
final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
handlePersistenceException(converted);
return converted;
} else if (cause instanceof org.hibernate.QueryTimeoutException) {
final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
handlePersistenceException(converted);
return converted;
} else if (cause instanceof ObjectNotFoundException) {
final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
handlePersistenceException(converted);
return converted;
} else if (cause instanceof org.hibernate.NonUniqueObjectException) {
final EntityExistsException converted = new EntityExistsException(cause.getMessage());
handlePersistenceException(converted);
return converted;
} else if (cause instanceof org.hibernate.NonUniqueResultException) {
final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
handlePersistenceException(converted);
return converted;
} else if (cause instanceof UnresolvableObjectException) {
final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
handlePersistenceException(converted);
return converted;
} else if (cause instanceof QueryException) {
return new IllegalArgumentException(cause);
} else if (cause instanceof MultipleBagFetchException) {
return new IllegalArgumentException(cause);
} else if (cause instanceof TransientObjectException) {
try {
sharedSessionContract.markForRollbackOnly();
} catch (Exception ne) {
//we do not want the subsequent exception to swallow the original one
log.unableToMarkForRollbackOnTransientObjectException(ne);
}
//Spec 3.2.3 Synchronization rules
return new IllegalStateException(e);
} else {
final PersistenceException converted = new PersistenceException(cause);
handlePersistenceException(converted);
return converted;
}
}
use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class SessionImpl method getEntityName.
@Override
public String getEntityName(Object object) {
checkOpen();
// checkTransactionSynchStatus();
if (object instanceof HibernateProxy) {
if (!persistenceContext.containsProxy(object)) {
throw new TransientObjectException("proxy was not associated with the session");
}
object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
}
EntityEntry entry = persistenceContext.getEntry(object);
if (entry == null) {
throwTransientObjectException(object);
}
return entry.getPersister().getEntityName();
}
use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class CascadeTest method testManyToOnePropertyRefAssignedIds.
public void testManyToOnePropertyRefAssignedIds() {
try {
Session s = openSession();
s.beginTransaction();
ParentAssigned p = new ParentAssigned(new Long(1), "parent");
OtherAssigned other = new OtherAssigned(new Long(2));
other.setOwner(p);
s.persist(other);
try {
s.getTransaction().commit();
fail("expecting TransientObjectException on flush");
} catch (TransientObjectException e) {
// expected result
log.trace("handled expected exception", e);
s.getTransaction().rollback();
} finally {
s.close();
}
} finally {
cleanupData();
}
}
Aggregations