comparison src/main/java/de/intevation/lada/data/Repository.java @ 43:13178bbe77ff

New generic repository for data requests.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 24 May 2013 11:51:01 +0200
parents
children 74a488e52dde
comparison
equal deleted inserted replaced
42:28b9167e5a5b 43:13178bbe77ff
1 package de.intevation.lada.data;
2
3 import java.util.List;
4
5 import javax.enterprise.context.ApplicationScoped;
6 import javax.inject.Inject;
7 import javax.persistence.EntityManager;
8 import javax.persistence.criteria.CriteriaBuilder;
9 import javax.persistence.criteria.CriteriaQuery;
10 import javax.persistence.criteria.Root;
11 import javax.inject.Named;
12
13 /**
14 * This generic Container is an interface to request and select Data
15 * obejcts from the connected database.
16 *
17 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
18 */
19 @Named
20 @ApplicationScoped
21 public class Repository
22 {
23 /**
24 * The entitymanager managing the data.
25 */
26 @Inject
27 private EntityManager em;
28
29 /**
30 * Get all objects of type <link>clazz</link>from database.
31 *
32 * @param clazz The class type.
33 * @return List of objects.
34 */
35 public <T> List<T> findAll(Class<T> clazz) {
36 CriteriaBuilder builder = em.getCriteriaBuilder();
37 CriteriaQuery<T> criteria = builder.createQuery(clazz);
38 Root<T> member = criteria.from(clazz);
39 criteria.select(member);
40 return em.createQuery(criteria).getResultList();
41 }
42
43 /**
44 * Find a single object identified by its id.
45 *
46 * @param clazz The class type.
47 * @param id The object id.
48 * @return The requested object of type clazz
49 */
50 public <T> T findById(Class<T> clazz, String id) {
51 return em.find(clazz, id);
52 }
53 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)