After reading through
http://www.theserverside.com/news/1363571/Remote-Lazy-Loading-in-Hibernate I wrote a collection-type extension specific for lists:
Code:
public class MyCollectionType implements UserCollectionType {
public PersistentCollection instantiate(SessionImplementor session,
CollectionPersister persister) throws HibernateException {
return new MyPersistentList(session);
}
public PersistentCollection wrap(SessionImplementor session,
Object collection) {
return new MyPersistentList(session, (List<?>) collection);
}
....
public Object replaceElements(Object original, Object target,
CollectionPersister persister, Object owner,
@SuppressWarnings("rawtypes") Map copyCache,
SessionImplementor session) throws HibernateException {
return original;
}
public Object instantiate(int anticipatedSize) {
return new ArrayList<Object>();
}
}
public class MyPersistentList implements PersistentCollection, List {
...
public MyPersistentList (SessionImplementor session) {
this.session = session;
}
public MyPersistentList (SessionImplementor session, List<?> list) {
this(session);
this.list = new ArrayList<Object>();
}
...
public Object getValue() {
return new ArrayList<Object>();
}
public void setSnapshot(Serializable key, String role, Serializable snapshot) {
this.key = key;
this.role = role;
this.storedSnapshot = snapshot;
}
public boolean isDirty() {
return false;
}
....
}
There are bunch of methods I see getting called that I have overrided like
unsetSession (same code as AbstractPersistentCollection),
wasInitialized (returns false always),
toArray (empty ArrayList like
getValue), and
clearDirty which does nothing in my code. Whether or not certain methods are called depends of course on the action (get, update, select, delete so forth). Have most of the CRUD flows and the code works. But it feels like putting a shoe on the wrong foot. And my code doesn't fit into annotations (there is no annotation for
collection-type, addition is still pending as of 3.4 Hibernate Core) and the implementation of the
PersistentCollection basically just removes the "dirty" controls.
I tried building a Tuplizer but the coding needed isn't straight forward for me. Building a HibernateProxy isn't problem it is using Javassisst to "ignore" method calls on the lists. Would have been cool if the JavassistLazyInitializer had a way to plug in over method filters like the one for "finalizer".
Suggestions?