changeset 43:13178bbe77ff

New generic repository for data requests.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 24 May 2013 11:51:01 +0200
parents 28b9167e5a5b
children 2d89a04d5ad9
files src/main/java/de/intevation/lada/data/Repository.java
diffstat 1 files changed, 53 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/data/Repository.java	Fri May 24 11:51:01 2013 +0200
@@ -0,0 +1,53 @@
+package de.intevation.lada.data;
+
+import java.util.List;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+import javax.inject.Named;
+
+/**
+ * This generic Container is an interface to request and select Data
+ * obejcts from the connected database.
+ * 
+ * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
+ */
+@Named
+@ApplicationScoped
+public class Repository
+{
+    /**
+     * The entitymanager managing the data.
+     */
+    @Inject
+    private EntityManager em;
+
+    /**
+     * Get all objects of type <link>clazz</link>from database.
+     *
+     * @param clazz The class type.
+     * @return List of objects.
+     */
+    public <T> List<T> findAll(Class<T> clazz) {
+        CriteriaBuilder builder = em.getCriteriaBuilder();
+        CriteriaQuery<T> criteria = builder.createQuery(clazz);
+        Root<T> member = criteria.from(clazz);
+        criteria.select(member);
+        return em.createQuery(criteria).getResultList();
+    }
+
+    /**
+     * Find a single object identified by its id.
+     * 
+     * @param clazz The class type.
+     * @param id The object id.
+     * @return The requested object of type clazz
+     */
+    public <T> T findById(Class<T> clazz, String id) {
+        return em.find(clazz, id);
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)