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: */package org.apache.openejb.persistence;
018:
019: import javax.persistence.EntityManager;
020: import javax.persistence.FlushModeType;
021: import javax.persistence.Query;
022: import javax.persistence.TemporalType;
023: import java.util.Calendar;
024: import java.util.Date;
025: import java.util.List;
026:
027: /**
028: * The JtaQuery is a wrapper around a query and and entity manager that automatically closes the entity managers
029: * when the query is finished. This implementation is only for non-transaction queryies
030: */
031: public class JtaQuery implements Query {
032: private final EntityManager entityManager;
033: private final Query query;
034:
035: public JtaQuery(EntityManager entityManager, Query query) {
036: this .entityManager = entityManager;
037: this .query = query;
038: }
039:
040: public List getResultList() {
041: try {
042: return query.getResultList();
043: } finally {
044: entityManager.close();
045: }
046: }
047:
048: public Object getSingleResult() {
049: try {
050: return query.getSingleResult();
051: } finally {
052: entityManager.close();
053: }
054: }
055:
056: public int executeUpdate() {
057: try {
058: return query.executeUpdate();
059: } finally {
060: entityManager.close();
061: }
062: }
063:
064: public Query setMaxResults(int i) {
065: query.setMaxResults(i);
066: return this ;
067: }
068:
069: public Query setFirstResult(int i) {
070: query.setFirstResult(i);
071: return this ;
072: }
073:
074: public Query setFlushMode(FlushModeType flushModeType) {
075: query.setFlushMode(flushModeType);
076: return this ;
077: }
078:
079: public Query setHint(String s, Object o) {
080: query.setHint(s, o);
081: return this ;
082: }
083:
084: public Query setParameter(String s, Object o) {
085: query.setParameter(s, o);
086: return this ;
087: }
088:
089: public Query setParameter(String s, Date date,
090: TemporalType temporalType) {
091: query.setParameter(s, date, temporalType);
092: return this ;
093: }
094:
095: public Query setParameter(String s, Calendar calendar,
096: TemporalType temporalType) {
097: query.setParameter(s, calendar, temporalType);
098: return this ;
099: }
100:
101: public Query setParameter(int i, Object o) {
102: query.setParameter(i, o);
103: return this ;
104: }
105:
106: public Query setParameter(int i, Date date,
107: TemporalType temporalType) {
108: query.setParameter(i, date, temporalType);
109: return this ;
110: }
111:
112: public Query setParameter(int i, Calendar calendar,
113: TemporalType temporalType) {
114: query.setParameter(i, calendar, temporalType);
115: return this;
116: }
117: }
|