Return to Snippet

Revision: 64731
at September 12, 2013 20:30 by pachaman


Initial Code
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.TemporalType;

/**
 * Wrapper sobre una query que evita que se haga flush en la consulta,
 * cambiando el modo del flush
 * @see Query
 * @author jose
 */
public class NonFlushableQueryWrapper implements Query {

	private Query wrappedQuery;

	public NonFlushableQueryWrapper(Query query) {
		wrappedQuery = query;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getResultList() {
		FlushModeType currentFlushMode = wrappedQuery.getFlushMode();
		try {
			wrappedQuery.setFlushMode(FlushModeType.COMMIT);
			return wrappedQuery.getResultList();
		} finally {
			wrappedQuery.setFlushMode(currentFlushMode);
		}
	}

	@Override
	public Object getSingleResult() {
		FlushModeType currentFlushMode = wrappedQuery.getFlushMode();
		try {
			wrappedQuery.setFlushMode(FlushModeType.COMMIT);
			return wrappedQuery.getSingleResult();
		} finally {
			wrappedQuery.setFlushMode(currentFlushMode);
		}
	}

	@Override
	public int executeUpdate() {
		return wrappedQuery.executeUpdate();
	}

	@Override
	public Query setMaxResults(int maxResult) {
		return wrappedQuery.setMaxResults(maxResult);
	}

	@Override
	public int getMaxResults() {
		return wrappedQuery.getMaxResults();
	}

	@Override
	public Query setFirstResult(int startPosition) {
		return wrappedQuery.setFirstResult(startPosition);
	}

	@Override
	public int getFirstResult() {
		return wrappedQuery.getFirstResult();
	}

	@Override
	public Query setHint(String hintName, Object value) {
		return wrappedQuery.setHint(hintName, value);
	}

	@Override
	public Map<String, Object> getHints() {
		return wrappedQuery.getHints();
	}

	@Override
	public <T> Query setParameter(Parameter<T> param, T value) {
		return wrappedQuery.setParameter(param, value);
	}

	@Override
	public Query setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
		return wrappedQuery.setParameter(param, value, temporalType);
	}

	@Override
	public Query setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
		return wrappedQuery.setParameter(param, value, temporalType);
	}

	@Override
	public Query setParameter(String name, Object value) {
		return wrappedQuery.setParameter(name, value);
	}

	@Override
	public Query setParameter(String name, Calendar value, TemporalType temporalType) {
		return wrappedQuery.setParameter(name, value, temporalType);
	}

	@Override
	public Query setParameter(String name, Date value, TemporalType temporalType) {
		return wrappedQuery.setParameter(name, value, temporalType);
	}

	@Override
	public Query setParameter(int position, Object value) {
		return wrappedQuery.setParameter(position, value);
	}

	@Override
	public Query setParameter(int position, Calendar value, TemporalType temporalType) {
		return wrappedQuery.setParameter(position, value, temporalType);
	}

	@Override
	public Query setParameter(int position, Date value, TemporalType temporalType) {
		return wrappedQuery.setParameter(position, value, temporalType);
	}

	@Override
	public Set<Parameter<?>> getParameters() {
		return wrappedQuery.getParameters();
	}

	@Override
	public Parameter<?> getParameter(String name) {
		return wrappedQuery.getParameter(name);
	}

	@Override
	public <T> Parameter<T> getParameter(String name, Class<T> type) {
		return wrappedQuery.getParameter(name, type);
	}

	@Override
	public Parameter<?> getParameter(int position) {
		return wrappedQuery.getParameter(position);
	}

	@Override
	public <T> Parameter<T> getParameter(int position, Class<T> type) {
		return wrappedQuery.getParameter(position, type);
	}

	@Override
	public boolean isBound(Parameter<?> param) {
		return wrappedQuery.isBound(param);
	}

	@Override
	public <T> T getParameterValue(Parameter<T> param) {
		return wrappedQuery.getParameterValue(param);
	}

	@Override
	public Object getParameterValue(String name) {
		return wrappedQuery.getParameterValue(name);
	}

	@Override
	public Object getParameterValue(int position) {
		return wrappedQuery.getParameterValue(position);
	}

	@Override
	public Query setFlushMode(FlushModeType flushMode) {
		return wrappedQuery.setFlushMode(flushMode);
	}

	@Override
	public FlushModeType getFlushMode() {
		return wrappedQuery.getFlushMode();
	}

	@Override
	public Query setLockMode(LockModeType lockMode) {
		return wrappedQuery.setLockMode(lockMode);
	}

	@Override
	public LockModeType getLockMode() {
		return wrappedQuery.getLockMode();
	}

	@Override
	public <T> T unwrap(Class<T> cls) {
		return wrappedQuery.unwrap(cls);
	}
}

Initial URL

                                

Initial Description
Wrapper over JPA query that avoid flush

Initial Title
JPA NonFlushableQueryWrapper

Initial Tags
java, query

Initial Language
Java