001: //$Id: CacheEntry.java 7785 2005-08-08 23:24:44Z oneovthafew $
002: package org.hibernate.cache.entry;
003:
004: import java.io.Serializable;
005:
006: import org.hibernate.AssertionFailure;
007: import org.hibernate.HibernateException;
008: import org.hibernate.Interceptor;
009: import org.hibernate.engine.SessionImplementor;
010: import org.hibernate.event.EventSource;
011: import org.hibernate.event.PreLoadEvent;
012: import org.hibernate.event.PreLoadEventListener;
013: import org.hibernate.persister.entity.EntityPersister;
014: import org.hibernate.type.TypeFactory;
015: import org.hibernate.util.ArrayHelper;
016:
017: /**
018: * A cached instance of a persistent class
019: *
020: * @author Gavin King
021: */
022: public final class CacheEntry implements Serializable {
023:
024: private final Serializable[] disassembledState;
025: private final String subclass;
026: private final boolean lazyPropertiesAreUnfetched;
027: private final Object version;
028:
029: public String getSubclass() {
030: return subclass;
031: }
032:
033: public boolean areLazyPropertiesUnfetched() {
034: return lazyPropertiesAreUnfetched;
035: }
036:
037: public CacheEntry(final Object[] state,
038: final EntityPersister persister, final boolean unfetched,
039: final Object version, final SessionImplementor session,
040: final Object owner) throws HibernateException {
041: //disassembled state gets put in a new array (we write to cache by value!)
042: this .disassembledState = TypeFactory.disassemble(state,
043: persister.getPropertyTypes(), persister
044: .isLazyPropertiesCacheable() ? null : persister
045: .getPropertyLaziness(), session, owner);
046: subclass = persister.getEntityName();
047: lazyPropertiesAreUnfetched = unfetched
048: || !persister.isLazyPropertiesCacheable();
049: this .version = version;
050: }
051:
052: public Object getVersion() {
053: return version;
054: }
055:
056: CacheEntry(Serializable[] state, String subclass,
057: boolean unfetched, Object version) {
058: this .disassembledState = state;
059: this .subclass = subclass;
060: this .lazyPropertiesAreUnfetched = unfetched;
061: this .version = version;
062: }
063:
064: public Object[] assemble(final Object instance,
065: final Serializable id, final EntityPersister persister,
066: final Interceptor interceptor, final EventSource session)
067: throws HibernateException {
068:
069: if (!persister.getEntityName().equals(subclass)) {
070: throw new AssertionFailure(
071: "Tried to assemble a different subclass instance");
072: }
073:
074: return assemble(disassembledState, instance, id, persister,
075: interceptor, session);
076:
077: }
078:
079: private static Object[] assemble(final Serializable[] values,
080: final Object result, final Serializable id,
081: final EntityPersister persister,
082: final Interceptor interceptor, final EventSource session)
083: throws HibernateException {
084:
085: //assembled state gets put in a new array (we read from cache by value!)
086: Object[] assembledProps = TypeFactory.assemble(values,
087: persister.getPropertyTypes(), session, result);
088:
089: //persister.setIdentifier(result, id); //before calling interceptor, for consistency with normal load
090:
091: //TODO: reuse the PreLoadEvent
092: PreLoadEvent preLoadEvent = new PreLoadEvent(session)
093: .setEntity(result).setState(assembledProps).setId(id)
094: .setPersister(persister);
095:
096: PreLoadEventListener[] listeners = session.getListeners()
097: .getPreLoadEventListeners();
098: for (int i = 0; i < listeners.length; i++) {
099: listeners[i].onPreLoad(preLoadEvent);
100: }
101:
102: persister.setPropertyValues(result, assembledProps, session
103: .getEntityMode());
104:
105: return assembledProps;
106: }
107:
108: public Serializable[] getDisassembledState() {
109: // todo: this was added to support initializing an entity's EntityEntry snapshot during reattach;
110: // this should be refactored to instead expose a method to assemble a EntityEntry based on this
111: // state for return.
112: return disassembledState;
113: }
114:
115: public String toString() {
116: return "CacheEntry(" + subclass + ')'
117: + ArrayHelper.toString(disassembledState);
118: }
119:
120: }
|