001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.geronimo.persistence;
019
020 import javax.persistence.EntityManager;
021 import javax.persistence.FlushModeType;
022 import javax.persistence.Query;
023 import javax.persistence.TemporalType;
024 import java.util.Calendar;
025 import java.util.Date;
026 import java.util.List;
027
028 /**
029 * The JtaQuery is a wrapper around a query and and entity manager that automatically closes the entity managers
030 * when the query is finished. This implementation is only for non-transaction queryies
031 */
032 public class NoTxQueryWrapper implements Query {
033 private final EntityManager entityManager;
034 private final Query query;
035
036 public NoTxQueryWrapper(EntityManager entityManager, Query query) {
037 this.entityManager = entityManager;
038 this.query = query;
039 }
040
041 public List getResultList() {
042 try {
043 return query.getResultList();
044 } finally {
045 entityManager.close();
046 }
047 }
048
049 public Object getSingleResult() {
050 try {
051 return query.getSingleResult();
052 } finally {
053 entityManager.close();
054 }
055 }
056
057 public int executeUpdate() {
058 try {
059 return query.executeUpdate();
060 } finally {
061 entityManager.close();
062 }
063 }
064
065 public Query setMaxResults(int i) {
066 query.setMaxResults(i);
067 return this;
068 }
069
070 public Query setFirstResult(int i) {
071 query.setFirstResult(i);
072 return this;
073 }
074
075 public Query setFlushMode(FlushModeType flushModeType) {
076 query.setFlushMode(flushModeType);
077 return this;
078 }
079
080 public Query setHint(String s, Object o) {
081 query.setHint(s, o);
082 return this;
083 }
084
085 public Query setParameter(String s, Object o) {
086 query.setParameter(s, o);
087 return this;
088 }
089
090 public Query setParameter(String s, Date date, TemporalType temporalType) {
091 query.setParameter(s, date, temporalType);
092 return this;
093 }
094
095 public Query setParameter(String s, Calendar calendar, TemporalType temporalType) {
096 query.setParameter(s, calendar, temporalType);
097 return this;
098 }
099
100 public Query setParameter(int i, Object o) {
101 query.setParameter(i, o);
102 return this;
103 }
104
105 public Query setParameter(int i, Date date, TemporalType temporalType) {
106 query.setParameter(i, date, temporalType);
107 return this;
108 }
109
110 public Query setParameter(int i, Calendar calendar, TemporalType temporalType) {
111 query.setParameter(i, calendar, temporalType);
112 return this;
113 }
114 }