1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.openejb.persistence; 19 20 import javax.persistence.EntityManager; 21 import javax.persistence.FlushModeType; 22 import javax.persistence.Query; 23 import javax.persistence.TemporalType; 24 import java.util.Calendar; 25 import java.util.Date; 26 import java.util.List; 27 28 /** 29 * The JtaQuery is a wrapper around a query and and entity manager that automatically closes the entity managers 30 * when the query is finished. This implementation is only for non-transaction queryies 31 */ 32 public class JtaQuery implements Query { 33 private final EntityManager entityManager; 34 private final Query query; 35 36 public JtaQuery(EntityManager entityManager, Query query) { 37 this.entityManager = entityManager; 38 this.query = query; 39 } 40 41 public List getResultList() { 42 try { 43 return query.getResultList(); 44 } finally { 45 entityManager.close(); 46 } 47 } 48 49 public Object getSingleResult() { 50 try { 51 return query.getSingleResult(); 52 } finally { 53 entityManager.close(); 54 } 55 } 56 57 public int executeUpdate() { 58 try { 59 return query.executeUpdate(); 60 } finally { 61 entityManager.close(); 62 } 63 } 64 65 public Query setMaxResults(int i) { 66 query.setMaxResults(i); 67 return this; 68 } 69 70 public Query setFirstResult(int i) { 71 query.setFirstResult(i); 72 return this; 73 } 74 75 public Query setFlushMode(FlushModeType flushModeType) { 76 query.setFlushMode(flushModeType); 77 return this; 78 } 79 80 public Query setHint(String s, Object o) { 81 query.setHint(s, o); 82 return this; 83 } 84 85 public Query setParameter(String s, Object o) { 86 query.setParameter(s, o); 87 return this; 88 } 89 90 public Query setParameter(String s, Date date, TemporalType temporalType) { 91 query.setParameter(s, date, temporalType); 92 return this; 93 } 94 95 public Query setParameter(String s, Calendar calendar, TemporalType temporalType) { 96 query.setParameter(s, calendar, temporalType); 97 return this; 98 } 99 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 }