use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class StandardCacheEntryImpl method assemble.
/**
* Assemble the previously disassembled state represented by this entry into the given entity instance.
*
* Additionally manages the PreLoadEvent callbacks.
*
* @param instance The entity instance
* @param id The entity identifier
* @param persister The entity persister
* @param interceptor (currently unused)
* @param session The session
*
* @return The assembled state
*
* @throws HibernateException Indicates a problem performing assembly or calling the PreLoadEventListeners.
*
* @see org.hibernate.type.Type#assemble
* @see org.hibernate.type.Type#disassemble
*/
public Object[] assemble(final Object instance, final Serializable id, final EntityPersister persister, final Interceptor interceptor, final EventSource session) throws HibernateException {
if (!persister.getEntityName().equals(subclass)) {
throw new AssertionFailure("Tried to assemble a different subclass instance");
}
//assembled state gets put in a new array (we read from cache by value!)
final Object[] assembledProps = TypeHelper.assemble(disassembledState, persister.getPropertyTypes(), session, instance);
//persister.setIdentifier(instance, id); //beforeQuery calling interceptor, for consistency with normal load
//TODO: reuse the PreLoadEvent
final PreLoadEvent preLoadEvent = new PreLoadEvent(session).setEntity(instance).setState(assembledProps).setId(id).setPersister(persister);
final EventListenerGroup<PreLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.PRE_LOAD);
for (PreLoadEventListener listener : listenerGroup.listeners()) {
listener.onPreLoad(preLoadEvent);
}
persister.setPropertyValues(instance, assembledProps);
return assembledProps;
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method addCollection.
/**
* Add an collection to the cache, with a given collection entry.
*
* @param coll The collection for which we are adding an entry.
* @param entry The entry representing the collection.
* @param key The key of the collection's entry.
*/
private void addCollection(PersistentCollection coll, CollectionEntry entry, Serializable key) {
collectionEntries.put(coll, entry);
final CollectionKey collectionKey = new CollectionKey(entry.getLoadedPersister(), key);
final PersistentCollection old = collectionsByKey.put(collectionKey, coll);
if (old != null) {
if (old == coll) {
throw new AssertionFailure("bug adding collection twice");
}
// or should it actually throw an exception?
old.unsetSession(session);
collectionEntries.remove(old);
// watch out for a case where old is still referenced
// somewhere in the object graph! (which is a user error)
}
}
use of org.hibernate.AssertionFailure 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.AssertionFailure in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method setReadOnly.
@Override
public void setReadOnly(Object object, boolean readOnly) {
if (object == null) {
throw new AssertionFailure("object must be non-null.");
}
if (isReadOnly(object) == readOnly) {
return;
}
if (object instanceof HibernateProxy) {
final HibernateProxy proxy = (HibernateProxy) object;
setProxyReadOnly(proxy, readOnly);
if (Hibernate.isInitialized(proxy)) {
setEntityReadOnly(proxy.getHibernateLazyInitializer().getImplementation(), readOnly);
}
} else {
setEntityReadOnly(object, readOnly);
// PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity
// so need to check the return value to be sure it is really a proxy
final Object maybeProxy = getSession().getPersistenceContext().proxyFor(object);
if (maybeProxy instanceof HibernateProxy) {
setProxyReadOnly((HibernateProxy) maybeProxy, readOnly);
}
}
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class Collections method processDereferencedCollection.
private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
final CollectionPersister loadedPersister = entry.getLoadedPersister();
if (loadedPersister != null && LOG.isDebugEnabled()) {
LOG.debugf("Collection dereferenced: %s", MessageHelper.collectionInfoString(loadedPersister, coll, entry.getLoadedKey(), session));
}
// do a check
final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
if (hasOrphanDelete) {
Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier(coll.getOwner(), session);
if (ownerId == null) {
// the persistence context
if (session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled()) {
final EntityEntry ownerEntry = persistenceContext.getEntry(coll.getOwner());
if (ownerEntry != null) {
ownerId = ownerEntry.getId();
}
}
if (ownerId == null) {
throw new AssertionFailure("Unable to determine collection owner identifier for orphan-delete processing");
}
}
final EntityKey key = session.generateEntityKey(ownerId, loadedPersister.getOwnerEntityPersister());
final Object owner = persistenceContext.getEntity(key);
if (owner == null) {
throw new AssertionFailure("collection owner not associated with session: " + loadedPersister.getRole());
}
final EntityEntry e = persistenceContext.getEntry(owner);
//only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
if (e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE) {
throw new HibernateException("A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " + loadedPersister.getRole());
}
}
// do the work
entry.setCurrentPersister(null);
entry.setCurrentKey(null);
prepareCollectionForUpdate(coll, entry, session.getFactory());
}
Aggregations