0

I am migrating a tool for hibernate that automates prefetching, and this tool uses custom collections, in other words custom classes that extend the already existing collections of hibernate, such as PersistentSet. The problem for me is that I don't understand how I can make hibernate to use my custom collection classes when returning results. I am using annotations for the mappings.

One of the wrapper classes look like this:

public class AutofetchSet extends PersistentSet implements Trackable {

private CollectionTracker collectionTracker = new CollectionTracker();

public AutofetchSet() {
    super();
}

public AutofetchSet(SessionImplementor si, Set s) {
    super(si, s);
}

public AutofetchSet(SessionImplementor si) {
    super(si);
}

public void addTracker(Statistics tracker) {
    collectionTracker.addTracker(tracker);
}

public void addTrackers(Set<Statistics> trackers) {
    collectionTracker.addTrackers(trackers);
}

public boolean disableTracking() {
    boolean oldValue = collectionTracker.isTracking();
    collectionTracker.setTracking(false);
    return oldValue;
}

public boolean enableTracking() {
    boolean oldValue = collectionTracker.isTracking();
    collectionTracker.setTracking(true);
    return oldValue;
}

public void removeTracker(Statistics stats) {
    collectionTracker.removeTracker(stats);
}

private void accessed() {
    if (wasInitialized()) {
        collectionTracker.trackAccess(set);
    }
}

/**
 * @see java.util.Set#size()
 */
@Override
public int size() {
    int ret = super.size();
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

/**
 * @see java.util.Set#isEmpty()
 */
@Override
public boolean isEmpty() {
    boolean ret = super.isEmpty();
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

/**
 * @see java.util.Set#contains(Object)
 */
@Override
public boolean contains(Object object) {
    boolean ret = super.contains(object);
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

/**
 * @see java.util.Set#iterator()
 */
@Override
public Iterator iterator() {
    Iterator iter = super.iterator();
    if (wasInitialized()) {
        accessed();
    }
    return iter;
}

/**
 * @see java.util.Set#toArray()
 */
@Override
public Object[] toArray() {
    Object[] arr = super.toArray();
    if (wasInitialized()) {
        accessed();
    }
    return arr;
}

/**
 * @see java.util.Set#toArray(Object[])
 */
@Override
public Object[] toArray(Object[] array) {
    Object[] arr = super.toArray(array);
    if (wasInitialized()) {
        accessed();
    }
    return arr;
}

/**
 * @see java.util.Set#add(Object)
 */
@Override
public boolean add(Object value) {
    Boolean exists = isOperationQueueEnabled()
            ? readElementExistence(value) : null;
    if (exists == null) {
        initialize(true);
        accessed();
        if (set.add(value)) {
            dirty();
            return true;
        } else {
            return false;
        }
    } else {
        return super.add(value);
    }
}

/**
 * @see java.util.Set#remove(Object)
 */
@Override
public boolean remove(Object value) {
    boolean ret = super.remove(value);
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

/**
 * @see java.util.Set#containsAll(Collection)
 */
@Override
public boolean containsAll(Collection coll) {
    boolean ret = super.containsAll(coll);
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

/**
 * @see java.util.Set#addAll(Collection)
 */
@Override
public boolean addAll(Collection coll) {
    if (coll.size() > 0) {
        initialize(true);
        accessed();
        if (set.addAll(coll)) {
            dirty();
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/**
 * @see java.util.Set#retainAll(Collection)
 */
@Override
public boolean retainAll(Collection coll) {
    boolean val = super.retainAll(coll);
    if (wasInitialized()) {
        accessed();
    }
    return val;
}

/**
 * @see java.util.Set#removeAll(Collection)
 */
@Override
public boolean removeAll(Collection coll) {
    boolean val = super.removeAll(coll);
    if (wasInitialized()) {
        accessed();
    }
    return val;
}

@Override
public void clear() {
    super.clear();
    if (wasInitialized()) {
        accessed();
    }
}

@Override
public String toString() {
    //if (needLoading) return "asleep";
    String ret = super.toString();
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

@Override
public boolean equals(Object other) {
    boolean ret = super.equals(other);
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

@Override
public int hashCode() {
    int ret = super.hashCode();
    if (wasInitialized()) {
        accessed();
    }
    return ret;
}

public boolean isAccessed() {
    return collectionTracker.isAccessed();
}

This is the type class that I extend:

public class AutofetchSetType extends SetType {

public AutofetchSetType(TypeScope typeScope, String role, String propertyRef) {
    super(typeScope, role, propertyRef);

}
@Override
public PersistentCollection instantiate(SessionImplementor session,
        CollectionPersister persister, Serializable key) {
        return new AutofetchSet(session);
}

@Override
public PersistentCollection wrap(SessionImplementor session,
        Object collection) {
        return new AutofetchSet(session, (java.util.Set) collection);
}

If someone has some insight in this, it would be highly appreciated.

Jssson
  • 71
  • 9
  • Possible duplicate of [Correct Way to Wrap a Hibernate PersistentSet in a Custom Implementation?](https://stackoverflow.com/questions/12648694/correct-way-to-wrap-a-hibernate-persistentset-in-a-custom-implementation) – Gimby Apr 12 '18 at 14:50
  • I am not sure that that clarifies everything for me. For example, do I need to both implement SetType and UserCollectionType? – Jssson Apr 12 '18 at 15:07
  • One question and answer usually never does fill all the blanks, you now know what to further research though. The dupe answers the core question asked, "how I can make hibernate to use my custom collection classes when returning results". – Gimby Apr 12 '18 at 15:11

0 Answers0