equal
deleted
inserted
replaced
33 |
33 |
34 protected static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName()); |
34 protected static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName()); |
35 protected static String persistenceUnitName = null; |
35 protected static String persistenceUnitName = null; |
36 protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select()); |
36 protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select()); |
37 protected EntityManager em = emf.createEntityManager(); |
37 protected EntityManager em = emf.createEntityManager(); |
|
38 protected EntityTransaction tx = null; |
38 |
39 |
39 JPADataAccessObject() { |
40 JPADataAccessObject() { |
40 em.isOpen(); |
41 tx = em.getTransaction(); |
|
42 tx.begin(); |
|
43 } |
|
44 |
|
45 void close() { |
|
46 commit() |
|
47 em.close() |
|
48 em = null |
|
49 } |
|
50 |
|
51 void commit() { |
|
52 if(tx != null && tx.isActive()) { |
|
53 tx.commit() |
|
54 } |
|
55 } |
|
56 |
|
57 void rollback() { |
|
58 if(tx != null && tx.isActive()) { |
|
59 tx.rollback() |
|
60 } |
41 } |
61 } |
42 |
62 |
43 protected static String getPersistenceUnitName() { |
63 protected static String getPersistenceUnitName() { |
44 if (persistenceUnitName != null) |
64 if (persistenceUnitName != null) |
45 return persistenceUnitName; |
65 return persistenceUnitName; |
67 OpenJPAConfiguration conf = openjpaemf.getConfiguration(); |
87 OpenJPAConfiguration conf = openjpaemf.getConfiguration(); |
68 DataSource ds = (DataSource) conf.getConnectionFactory(); |
88 DataSource ds = (DataSource) conf.getConnectionFactory(); |
69 return ds.getConnection(); |
89 return ds.getConnection(); |
70 } |
90 } |
71 |
91 |
72 void close() { |
|
73 em.close() |
|
74 em = null |
|
75 } |
|
76 |
|
77 public <T> T mergeAndRefresh(T object) { |
92 public <T> T mergeAndRefresh(T object) { |
78 def merged = em.merge(object) |
93 def merged = em.merge(object) |
79 em.refresh(merged) |
94 em.refresh(merged) |
80 |
95 |
81 merged |
96 merged |
106 boolean remove(Collection entities) { |
121 boolean remove(Collection entities) { |
107 persistAndRemove(new Object[0], entities.toArray()) |
122 persistAndRemove(new Object[0], entities.toArray()) |
108 } |
123 } |
109 |
124 |
110 synchronized boolean persistAndRemove(Object[] persistanceQueue, Object[] removeQueue) { |
125 synchronized boolean persistAndRemove(Object[] persistanceQueue, Object[] removeQueue) { |
111 EntityTransaction tx = null |
|
112 |
126 |
113 try { |
127 try { |
114 tx = em.getTransaction() |
|
115 tx.begin() |
|
116 |
128 |
117 if(persistanceQueue != null) { |
129 if(persistanceQueue != null) { |
118 for(object in persistanceQueue) { |
130 for(object in persistanceQueue) { |
119 if(object != null) { |
131 if(object != null) { |
120 em.persist(object) |
132 em.persist(object) |
127 if(object != null) { |
139 if(object != null) { |
128 em.remove(object) |
140 em.remove(object) |
129 } |
141 } |
130 } |
142 } |
131 } |
143 } |
132 |
|
133 tx.commit() |
|
134 } |
144 } |
135 catch(Throwable t) { |
145 catch(Throwable t) { |
136 log.error("Error while synchronizing with Database: ", t); |
146 log.error("Error while synchronizing with Database: ", t); |
137 |
147 return false; |
138 if(tx != null && tx.isActive()) { |
|
139 tx.rollback() |
|
140 } |
|
141 |
|
142 return false |
|
143 } |
148 } |
144 |
149 |
145 return true |
150 return true |
146 } |
151 } |
147 |
152 |
199 log.error("Error while searching object in Database: ", e); |
204 log.error("Error while searching object in Database: ", e); |
200 } |
205 } |
201 return result; |
206 return result; |
202 } |
207 } |
203 |
208 |
|
209 public <T> T find(Class<T> entityClass, String field, Object value) { |
|
210 return find(entityClass, ["${field}": value], null); |
|
211 } |
|
212 |
204 public <T> List<T> list(Class<T> entityClass) { |
213 public <T> List<T> list(Class<T> entityClass) { |
205 CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(entityClass); |
214 CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(entityClass); |
206 query.from(entityClass); |
215 query.from(entityClass); |
207 return new ArrayList(em.createQuery(query).getResultList()); |
216 return new ArrayList(em.createQuery(query).getResultList()); |
208 } |
217 } |