/ Published in: Java
Wrapper over JPA query that avoid flush
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 FlushModeType currentFlushMode = wrappedQuery.getFlushMode(); try { wrappedQuery.setFlushMode(FlushModeType.COMMIT); return wrappedQuery.getResultList(); } finally { wrappedQuery.setFlushMode(currentFlushMode); } } @Override 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 return wrappedQuery.setHint(hintName, value); } @Override return wrappedQuery.getHints(); } @Override public <T> Query setParameter(Parameter<T> param, T value) { return wrappedQuery.setParameter(param, value); } @Override return wrappedQuery.setParameter(param, value, temporalType); } @Override return wrappedQuery.setParameter(param, value, temporalType); } @Override return wrappedQuery.setParameter(name, value); } @Override return wrappedQuery.setParameter(name, value, temporalType); } @Override return wrappedQuery.setParameter(name, value, temporalType); } @Override return wrappedQuery.setParameter(position, value); } @Override return wrappedQuery.setParameter(position, value, temporalType); } @Override return wrappedQuery.setParameter(position, value, temporalType); } @Override public Set<Parameter<?>> getParameters() { return wrappedQuery.getParameters(); } @Override return wrappedQuery.getParameter(name); } @Override 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 return wrappedQuery.getParameterValue(name); } @Override 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); } }