comparison src/main/java/de/mpiwg/indexmeta/PersistentService.java @ 0:dfce13a5f5f9

nit project!
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Thu, 11 Apr 2013 15:25:26 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfce13a5f5f9
1 package de.mpiwg.indexmeta;
2
3 import java.util.List;
4
5 import javax.persistence.Query;
6
7 import de.mpiwg.indexmeta.bo.Contextualization;
8 import de.mpiwg.indexmeta.utils.QuadMap;
9
10 public class PersistentService {
11
12 private QuadMap<Contextualization> ctxMap = null;
13 private javax.persistence.EntityManagerFactory emf;
14 private javax.persistence.EntityManager em;
15 private String PERSISTENCE_UNIT_NAME = "uno";
16
17
18 public QuadMap<Contextualization> getCtxMap() {
19 if(ctxMap == null){
20 this.loadCtxMap();
21 }
22 return ctxMap;
23 }
24
25 public void saveCtx(Contextualization ctx){
26 System.out.println("Saving= " + ctx.toString());
27
28 initEntityManager();
29 em.getTransaction().begin();
30
31 em.persist(ctx);
32 ctxMap.put(ctx.getKey(), ctx);
33
34 em.getTransaction().commit();
35 closeEntityManager();
36
37 System.out.println("Saved= " + ctx.toString());
38 }
39
40
41 private void loadCtxMap(){
42 ctxMap = new QuadMap<Contextualization>();
43
44 initEntityManager();
45 em.getTransaction().begin();
46
47 Query q = em.createQuery("from Contextualization");
48 List<Contextualization> list = q.getResultList();
49
50 for(Contextualization ctx : list){
51 ctxMap.put(ctx.getKey(), ctx);
52 }
53
54 em.getTransaction().commit();
55 closeEntityManager();
56 }
57
58
59 private void initEntityManager() {
60 emf = javax.persistence.Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
61 em = emf.createEntityManager();
62 }
63
64 private void closeEntityManager() {
65 em.close();
66 emf.close();
67 }
68
69
70
71 }