All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.db.jpa.HibernateInterceptor Maven / Gradle / Ivy

There is a newer version: 2.6.2
Show newest version
package play.db.jpa;


import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.type.Type;

import java.io.Serializable;


public class HibernateInterceptor extends EmptyInterceptor {

  public HibernateInterceptor() {

  }
  
  @Override
  public int[] findDirty(Object o, Serializable id, Object[] arg2, Object[] arg3, String[] arg4, Type[] arg5) {
    if (o instanceof JPABase && !((JPABase) o).willBeSaved) {
      return new int[0];
    }
    return null;
  }

    @Override
    public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
        if (collection instanceof PersistentCollection) {
            Object o = ((PersistentCollection) collection).getOwner();
            if (o instanceof JPABase) {
                if (entities.get() != null) {
                    return;
                } else {
                    return;
                }
            }
        } else {
            System.out.println("HOO: Case not handled !!!");
        }
        super.onCollectionUpdate(collection, key);
    }

    @Override
    public void onCollectionRecreate(Object collection, Serializable key) throws CallbackException {
        if (collection instanceof PersistentCollection) {
            Object o = ((PersistentCollection) collection).getOwner();
            if (o instanceof JPABase) {
                if (entities.get() != null) {
                    return;
                } else {
                    return;
                }
            }
        } else {
            System.out.println("HOO: Case not handled !!!");
        }

        super.onCollectionRecreate(collection, key);
    }

    @Override
    public void onCollectionRemove(Object collection, Serializable key) throws CallbackException {
        if (collection instanceof PersistentCollection) {
            Object o = ((PersistentCollection) collection).getOwner();
            if (o instanceof JPABase) {
                if (entities.get() != null) {
                    return;
                } else {
                    return;
                }
            }
        } else {
            System.out.println("HOO: Case not handled !!!");
        }
        super.onCollectionRemove(collection, key);
    }

    protected final ThreadLocal entities = new ThreadLocal<>();

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        entities.set(entity);
        return super.onSave(entity, id, state, propertyNames, types);
    }

    @Override
    public void afterTransactionCompletion(org.hibernate.Transaction tx) {
        entities.remove();
    }
}