KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > entry > CacheEntry


1 //$Id: CacheEntry.java,v 1.5 2005/05/27 03:53:58 oneovthafew Exp $
2
package org.hibernate.cache.entry;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.AssertionFailure;
7 import org.hibernate.HibernateException;
8 import org.hibernate.Interceptor;
9 import org.hibernate.engine.SessionImplementor;
10 import org.hibernate.event.EventSource;
11 import org.hibernate.event.PreLoadEvent;
12 import org.hibernate.persister.entity.EntityPersister;
13 import org.hibernate.type.TypeFactory;
14 import org.hibernate.util.ArrayHelper;
15
16 /**
17  * A cached instance of a persistent class
18  *
19  * @author Gavin King
20  */

21 public final class CacheEntry implements Serializable JavaDoc {
22
23     private final Serializable JavaDoc[] disassembledState;
24     private final String JavaDoc subclass;
25     private final boolean lazyPropertiesAreUnfetched;
26     
27     public String JavaDoc getSubclass() {
28         return subclass;
29     }
30     
31     public boolean areLazyPropertiesUnfetched() {
32         return lazyPropertiesAreUnfetched;
33     }
34     
35     public CacheEntry(
36             final Object JavaDoc[] state,
37             final EntityPersister persister,
38             final boolean unfetched,
39             final SessionImplementor session,
40             final Object JavaDoc owner)
41     throws HibernateException {
42         //disassembled state gets put in a new array (we write to cache by value!)
43
this.disassembledState = TypeFactory.disassemble(
44                 state,
45                 persister.getPropertyTypes(),
46                 session,
47                 owner
48             );
49         subclass = persister.getEntityName();
50         lazyPropertiesAreUnfetched = unfetched;
51     }
52
53     CacheEntry(Serializable JavaDoc[] state, String JavaDoc subclass, boolean unfetched) {
54         this.disassembledState = state;
55         this.subclass = subclass;
56         this.lazyPropertiesAreUnfetched = unfetched;
57     }
58
59     public Object JavaDoc[] assemble(
60             final Object JavaDoc instance,
61             final Serializable JavaDoc id,
62             final EntityPersister persister,
63             final Interceptor interceptor,
64             final EventSource session)
65     throws HibernateException {
66
67         if ( !persister.getEntityName().equals(subclass) ) {
68             throw new AssertionFailure("Tried to assemble a different subclass instance");
69         }
70
71         return assemble(disassembledState, instance, id, persister, interceptor, session);
72
73     }
74
75     private static Object JavaDoc[] assemble(
76             final Serializable JavaDoc[] values,
77             final Object JavaDoc result,
78             final Serializable JavaDoc id,
79             final EntityPersister persister,
80             final Interceptor interceptor,
81             final EventSource session)
82     throws HibernateException {
83             
84         //assembled state gets put in a new array (we read from cache by value!)
85
Object JavaDoc[] assembledProps = TypeFactory.assemble(
86                 values,
87                 persister.getPropertyTypes(),
88                 session, result
89             );
90
91         //persister.setIdentifier(result, id); //before calling interceptor, for consistency with normal load
92

93         //TODO: reuse the PreLoadEvent
94
PreLoadEvent preLoadEvent = new PreLoadEvent( session )
95                 .setEntity(result)
96                 .setState(assembledProps)
97                 .setId(id)
98                 .setPersister(persister);
99         
100         session.getListeners()
101                 .getPreLoadEventListener()
102                 .onPreLoad(preLoadEvent);
103
104         persister.setPropertyValues(
105                 result,
106                 assembledProps,
107                 session.getEntityMode()
108             );
109
110         return assembledProps;
111     }
112
113     public Serializable JavaDoc[] getDisassembledState() {
114         // todo: this was added to support initializing an entity's EntityEntry snapshot during reattach;
115
// this should be refactored to instead expose a method to assemble a EntityEntry based on this
116
// state for return.
117
return disassembledState;
118     }
119
120     public String JavaDoc toString() {
121         return "CacheEntry(" + subclass + ')' +
122                 ArrayHelper.toString(disassembledState);
123     }
124
125 }
126
127
128
129
130
131
132
Popular Tags