View Javadoc

1   /**
2    * Copyright 2008 WebPhotos
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.webphotos.dao.jpa;
17  
18  import java.util.List;
19  import javax.persistence.EntityManager;
20  import javax.persistence.PersistenceContext;
21  import javax.persistence.Query;
22  import net.sf.webphotos.entity.HasID;
23  
24  /**
25   *
26   * @author Guilherme L A Silva
27   */
28  public class WebPhotosDAO<E extends HasID, I> {
29  
30      protected EntityManager entityManager;
31      private Class<E> entityClass;
32      private Class<I> keyClass;
33  
34      public WebPhotosDAO(Class<E> entityClass, Class<I> keyClass) {
35          this.entityClass = entityClass;
36          this.keyClass = keyClass;
37      }
38  
39      /**
40       * Workarround for a rapid migration from RowSet
41       *
42       * @param query
43       * @return
44       * @deprecated
45       */
46      @Deprecated
47      public Query createNativeQuery(String query) {
48          return entityManager.createNativeQuery(query);
49      }
50  
51      public E findBy(I id) {
52          return entityManager.find(this.entityClass, id);
53      }
54  
55      public void save(E object) throws Exception {
56          if (object.getId() != null && object.equals(entityManager.find(entityClass, object.getId()))) {
57              entityManager.merge(object);
58          } else {
59              entityManager.persist(object);
60          }
61      }
62  
63      public void remove(E object) throws Exception {
64          entityManager.remove(object);
65          entityManager.flush();
66      }
67  
68      /**
69       * @return the entityManager
70       */
71      public EntityManager getEntityManager() {
72          return entityManager;
73      }
74  
75      /**
76       * @param entityManager the entityManager to set
77       */
78      @PersistenceContext
79      public void setEntityManager(EntityManager entityManager) {
80          this.entityManager = entityManager;
81      }
82  
83      /**
84       * Workarround for a rapid migration from RowSet
85       *
86       * @param query
87       * @return
88       * @deprecated
89       */
90      @Deprecated
91      public List<Object[]> findByNativeQuery(String query) {
92          return createNativeQuery(query).getResultList();
93      }
94  
95      @SuppressWarnings(value = "unchecked")
96      protected List<E> find(String query) {
97          return entityManager.createQuery(query).getResultList();
98      }
99  
100     @SuppressWarnings(value = "unchecked")
101     protected List<E> findByNamedQuery(String query) {
102         return entityManager.createNamedQuery(query).getResultList();
103     }
104 }