Posted By

pachaman on 09/12/13


Tagged

java query flush jpa


Versions (?)

JPA NonFlushableQueryWrapper


 / Published in: Java
 

Wrapper over JPA query that avoid flush

  1. import java.util.Calendar;
  2. import java.util.Date;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7. import javax.persistence.FlushModeType;
  8. import javax.persistence.LockModeType;
  9. import javax.persistence.Parameter;
  10. import javax.persistence.Query;
  11. import javax.persistence.TemporalType;
  12.  
  13. /**
  14.  * Wrapper sobre una query que evita que se haga flush en la consulta,
  15.  * cambiando el modo del flush
  16.  * @see Query
  17.  * @author jose
  18.  */
  19. public class NonFlushableQueryWrapper implements Query {
  20.  
  21. private Query wrappedQuery;
  22.  
  23. public NonFlushableQueryWrapper(Query query) {
  24. wrappedQuery = query;
  25. }
  26.  
  27. @SuppressWarnings("rawtypes")
  28. @Override
  29. public List getResultList() {
  30. FlushModeType currentFlushMode = wrappedQuery.getFlushMode();
  31. try {
  32. wrappedQuery.setFlushMode(FlushModeType.COMMIT);
  33. return wrappedQuery.getResultList();
  34. } finally {
  35. wrappedQuery.setFlushMode(currentFlushMode);
  36. }
  37. }
  38.  
  39. @Override
  40. public Object getSingleResult() {
  41. FlushModeType currentFlushMode = wrappedQuery.getFlushMode();
  42. try {
  43. wrappedQuery.setFlushMode(FlushModeType.COMMIT);
  44. return wrappedQuery.getSingleResult();
  45. } finally {
  46. wrappedQuery.setFlushMode(currentFlushMode);
  47. }
  48. }
  49.  
  50. @Override
  51. public int executeUpdate() {
  52. return wrappedQuery.executeUpdate();
  53. }
  54.  
  55. @Override
  56. public Query setMaxResults(int maxResult) {
  57. return wrappedQuery.setMaxResults(maxResult);
  58. }
  59.  
  60. @Override
  61. public int getMaxResults() {
  62. return wrappedQuery.getMaxResults();
  63. }
  64.  
  65. @Override
  66. public Query setFirstResult(int startPosition) {
  67. return wrappedQuery.setFirstResult(startPosition);
  68. }
  69.  
  70. @Override
  71. public int getFirstResult() {
  72. return wrappedQuery.getFirstResult();
  73. }
  74.  
  75. @Override
  76. public Query setHint(String hintName, Object value) {
  77. return wrappedQuery.setHint(hintName, value);
  78. }
  79.  
  80. @Override
  81. public Map<String, Object> getHints() {
  82. return wrappedQuery.getHints();
  83. }
  84.  
  85. @Override
  86. public <T> Query setParameter(Parameter<T> param, T value) {
  87. return wrappedQuery.setParameter(param, value);
  88. }
  89.  
  90. @Override
  91. public Query setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
  92. return wrappedQuery.setParameter(param, value, temporalType);
  93. }
  94.  
  95. @Override
  96. public Query setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
  97. return wrappedQuery.setParameter(param, value, temporalType);
  98. }
  99.  
  100. @Override
  101. public Query setParameter(String name, Object value) {
  102. return wrappedQuery.setParameter(name, value);
  103. }
  104.  
  105. @Override
  106. public Query setParameter(String name, Calendar value, TemporalType temporalType) {
  107. return wrappedQuery.setParameter(name, value, temporalType);
  108. }
  109.  
  110. @Override
  111. public Query setParameter(String name, Date value, TemporalType temporalType) {
  112. return wrappedQuery.setParameter(name, value, temporalType);
  113. }
  114.  
  115. @Override
  116. public Query setParameter(int position, Object value) {
  117. return wrappedQuery.setParameter(position, value);
  118. }
  119.  
  120. @Override
  121. public Query setParameter(int position, Calendar value, TemporalType temporalType) {
  122. return wrappedQuery.setParameter(position, value, temporalType);
  123. }
  124.  
  125. @Override
  126. public Query setParameter(int position, Date value, TemporalType temporalType) {
  127. return wrappedQuery.setParameter(position, value, temporalType);
  128. }
  129.  
  130. @Override
  131. public Set<Parameter<?>> getParameters() {
  132. return wrappedQuery.getParameters();
  133. }
  134.  
  135. @Override
  136. public Parameter<?> getParameter(String name) {
  137. return wrappedQuery.getParameter(name);
  138. }
  139.  
  140. @Override
  141. public <T> Parameter<T> getParameter(String name, Class<T> type) {
  142. return wrappedQuery.getParameter(name, type);
  143. }
  144.  
  145. @Override
  146. public Parameter<?> getParameter(int position) {
  147. return wrappedQuery.getParameter(position);
  148. }
  149.  
  150. @Override
  151. public <T> Parameter<T> getParameter(int position, Class<T> type) {
  152. return wrappedQuery.getParameter(position, type);
  153. }
  154.  
  155. @Override
  156. public boolean isBound(Parameter<?> param) {
  157. return wrappedQuery.isBound(param);
  158. }
  159.  
  160. @Override
  161. public <T> T getParameterValue(Parameter<T> param) {
  162. return wrappedQuery.getParameterValue(param);
  163. }
  164.  
  165. @Override
  166. public Object getParameterValue(String name) {
  167. return wrappedQuery.getParameterValue(name);
  168. }
  169.  
  170. @Override
  171. public Object getParameterValue(int position) {
  172. return wrappedQuery.getParameterValue(position);
  173. }
  174.  
  175. @Override
  176. public Query setFlushMode(FlushModeType flushMode) {
  177. return wrappedQuery.setFlushMode(flushMode);
  178. }
  179.  
  180. @Override
  181. public FlushModeType getFlushMode() {
  182. return wrappedQuery.getFlushMode();
  183. }
  184.  
  185. @Override
  186. public Query setLockMode(LockModeType lockMode) {
  187. return wrappedQuery.setLockMode(lockMode);
  188. }
  189.  
  190. @Override
  191. public LockModeType getLockMode() {
  192. return wrappedQuery.getLockMode();
  193. }
  194.  
  195. @Override
  196. public <T> T unwrap(Class<T> cls) {
  197. return wrappedQuery.unwrap(cls);
  198. }
  199. }

Report this snippet  

You need to login to post a comment.