|
indvdum@2
|
1 |
package ru.indvdum.jpa.dao
|
|
indvdum@2
|
2 |
import java.sql.Connection
|
|
indvdum@2
|
3 |
import java.sql.SQLException
|
|
indvdum@2
|
4 |
import java.util.ResourceBundle;
|
|
indvdum@2
|
5 |
import java.util.Map.Entry
|
|
indvdum@2
|
6 |
|
|
indvdum@2
|
7 |
import javax.persistence.EntityManager
|
|
indvdum@2
|
8 |
import javax.persistence.EntityManagerFactory
|
|
indvdum@2
|
9 |
import javax.persistence.EntityTransaction
|
|
indvdum@2
|
10 |
import javax.persistence.NoResultException
|
|
indvdum@2
|
11 |
import javax.persistence.Persistence
|
|
indvdum@2
|
12 |
import javax.persistence.Query
|
|
indvdum@2
|
13 |
import javax.persistence.criteria.CriteriaBuilder
|
|
indvdum@2
|
14 |
import javax.persistence.criteria.CriteriaQuery
|
|
indvdum@2
|
15 |
import javax.persistence.criteria.Predicate
|
|
indvdum@2
|
16 |
import javax.persistence.criteria.Root
|
|
indvdum@2
|
17 |
import javax.sql.DataSource
|
|
indvdum@2
|
18 |
|
|
indvdum@2
|
19 |
import org.apache.openjpa.conf.OpenJPAConfiguration
|
|
indvdum@2
|
20 |
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI
|
|
indvdum@2
|
21 |
import org.apache.openjpa.persistence.OpenJPAPersistence
|
|
indvdum@2
|
22 |
import org.slf4j.Logger
|
|
indvdum@2
|
23 |
import org.slf4j.LoggerFactory
|
|
indvdum@2
|
24 |
|
|
indvdum@2
|
25 |
import ru.indvdum.jpa.props.Props;
|
|
indvdum@2
|
26 |
|
|
indvdum@2
|
27 |
/**
|
|
indvdum@2
|
28 |
* @author indvdum (gotoindvdum[at]gmail[dot]com)
|
|
indvdum@2
|
29 |
* @since 08.11.2012 23:35:04
|
|
indvdum@5
|
30 |
*
|
|
indvdum@2
|
31 |
*/
|
|
indvdum@2
|
32 |
public class JPADataAccessObject {
|
|
indvdum@2
|
33 |
|
|
indvdum@2
|
34 |
protected static String persistenceUnitName = null;
|
|
indvdum@2
|
35 |
protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select());
|
|
indvdum@2
|
36 |
protected EntityManager em = emf.createEntityManager();
|
|
indvdum@2
|
37 |
final static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName());
|
|
indvdum@2
|
38 |
|
|
indvdum@2
|
39 |
JPADataAccessObject() {
|
|
indvdum@2
|
40 |
em.isOpen();
|
|
indvdum@2
|
41 |
}
|
|
indvdum@2
|
42 |
|
|
indvdum@2
|
43 |
protected static String getPersistenceUnitName() {
|
|
indvdum@2
|
44 |
if (persistenceUnitName != null)
|
|
indvdum@2
|
45 |
return persistenceUnitName;
|
|
indvdum@2
|
46 |
|
|
indvdum@2
|
47 |
if (persistenceUnitName == null)
|
|
indvdum@2
|
48 |
persistenceUnitName = System.getProperty(Props.PERSISTANCE_UNIT_NAME_PROPERTY);
|
|
indvdum@2
|
49 |
if (persistenceUnitName == null)
|
|
indvdum@2
|
50 |
persistenceUnitName = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.PERSISTANCE_UNIT_NAME_PROPERTY);
|
|
indvdum@2
|
51 |
if (persistenceUnitName == null)
|
|
indvdum@2
|
52 |
persistenceUnitName = "database";
|
|
indvdum@2
|
53 |
return persistenceUnitName;
|
|
indvdum@2
|
54 |
}
|
|
indvdum@2
|
55 |
|
|
indvdum@2
|
56 |
public static Connection getSQLConnection() throws SQLException {
|
|
indvdum@2
|
57 |
OpenJPAEntityManagerFactorySPI openjpaemf = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(emf);
|
|
indvdum@2
|
58 |
OpenJPAConfiguration conf = openjpaemf.getConfiguration();
|
|
indvdum@2
|
59 |
DataSource ds = (DataSource) conf.getConnectionFactory();
|
|
indvdum@2
|
60 |
return ds.getConnection();
|
|
indvdum@2
|
61 |
}
|
|
indvdum@2
|
62 |
|
|
indvdum@2
|
63 |
void close() {
|
|
indvdum@2
|
64 |
em.close()
|
|
indvdum@2
|
65 |
em = null
|
|
indvdum@2
|
66 |
}
|
|
indvdum@2
|
67 |
|
|
indvdum@2
|
68 |
public <T> T mergeAndRefresh(T object) {
|
|
indvdum@2
|
69 |
def merged = em.merge(object)
|
|
indvdum@2
|
70 |
em.refresh(merged)
|
|
indvdum@2
|
71 |
|
|
indvdum@2
|
72 |
merged
|
|
indvdum@2
|
73 |
}
|
|
indvdum@2
|
74 |
|
|
indvdum@2
|
75 |
public <T> T merge(T object) {
|
|
indvdum@2
|
76 |
em.merge(object)
|
|
indvdum@2
|
77 |
}
|
|
indvdum@2
|
78 |
|
|
indvdum@2
|
79 |
void refresh(Object ... refreshObjects) {
|
|
indvdum@2
|
80 |
for(object in refreshObjects) {
|
|
indvdum@2
|
81 |
em.refresh(object)
|
|
indvdum@2
|
82 |
}
|
|
indvdum@2
|
83 |
}
|
|
indvdum@2
|
84 |
|
|
indvdum@2
|
85 |
boolean persist(Object ... persistanceQueue) {
|
|
indvdum@2
|
86 |
persistAndRemove(persistanceQueue, new Object[0])
|
|
indvdum@2
|
87 |
}
|
|
indvdum@2
|
88 |
|
|
indvdum@2
|
89 |
boolean persist(Collection entities) {
|
|
indvdum@2
|
90 |
persistAndRemove(entities.toArray(), new Object[0])
|
|
indvdum@2
|
91 |
}
|
|
indvdum@2
|
92 |
|
|
indvdum@2
|
93 |
boolean remove(Object ... removeQueue) {
|
|
indvdum@2
|
94 |
persistAndRemove(new Object[0], removeQueue)
|
|
indvdum@2
|
95 |
}
|
|
indvdum@2
|
96 |
|
|
indvdum@2
|
97 |
boolean remove(Collection entities) {
|
|
indvdum@2
|
98 |
persistAndRemove(new Object[0], entities.toArray())
|
|
indvdum@2
|
99 |
}
|
|
indvdum@2
|
100 |
|
|
indvdum@2
|
101 |
synchronized boolean persistAndRemove(Object[] persistanceQueue, Object[] removeQueue) {
|
|
indvdum@2
|
102 |
EntityTransaction tx = null
|
|
indvdum@2
|
103 |
|
|
indvdum@2
|
104 |
try {
|
|
indvdum@2
|
105 |
tx = em.getTransaction()
|
|
indvdum@2
|
106 |
tx.begin()
|
|
indvdum@2
|
107 |
|
|
indvdum@2
|
108 |
if(persistanceQueue != null) {
|
|
indvdum@2
|
109 |
for(object in persistanceQueue) {
|
|
indvdum@2
|
110 |
if(object != null) {
|
|
indvdum@2
|
111 |
em.persist(object)
|
|
indvdum@2
|
112 |
}
|
|
indvdum@2
|
113 |
}
|
|
indvdum@2
|
114 |
}
|
|
indvdum@2
|
115 |
|
|
indvdum@2
|
116 |
if(removeQueue != null) {
|
|
indvdum@2
|
117 |
for(object in removeQueue) {
|
|
indvdum@2
|
118 |
if(object != null) {
|
|
indvdum@2
|
119 |
em.remove(object)
|
|
indvdum@2
|
120 |
}
|
|
indvdum@2
|
121 |
}
|
|
indvdum@2
|
122 |
}
|
|
indvdum@2
|
123 |
|
|
indvdum@2
|
124 |
tx.commit()
|
|
indvdum@2
|
125 |
}
|
|
indvdum@2
|
126 |
catch(Throwable t) {
|
|
indvdum@2
|
127 |
log.error("Error while synchronizing with Database: ", t);
|
|
indvdum@2
|
128 |
|
|
indvdum@2
|
129 |
if(tx != null && tx.isActive()) {
|
|
indvdum@2
|
130 |
tx.rollback()
|
|
indvdum@2
|
131 |
}
|
|
indvdum@2
|
132 |
|
|
indvdum@2
|
133 |
return false
|
|
indvdum@2
|
134 |
}
|
|
indvdum@2
|
135 |
|
|
indvdum@2
|
136 |
return true
|
|
indvdum@2
|
137 |
}
|
|
indvdum@2
|
138 |
|
|
indvdum@2
|
139 |
public <T> T find(Class<T> entityClass, Object primaryKey) {
|
|
indvdum@2
|
140 |
return em.find(entityClass, primaryKey);
|
|
indvdum@2
|
141 |
}
|
|
indvdum@2
|
142 |
|
|
indvdum@2
|
143 |
public <T> List<T> list(Class<T> entityClass, Map<String, Object> equalProperties, Map<String, Object> notEqualProperties) {
|
|
indvdum@2
|
144 |
CriteriaBuilder cb = em.getCriteriaBuilder();
|
|
indvdum@2
|
145 |
CriteriaQuery<T> query = cb.createQuery(entityClass);
|
|
indvdum@2
|
146 |
Root<T> root = query.from(entityClass);
|
|
indvdum@2
|
147 |
Collection<Predicate> predicates = new HashSet<Predicate>();
|
|
indvdum@2
|
148 |
if(equalProperties != null) {
|
|
indvdum@2
|
149 |
for(Entry<String, Object> entry: equalProperties.entrySet()){
|
|
indvdum@2
|
150 |
String property = entry.getKey();
|
|
indvdum@2
|
151 |
Object value = entry.getValue();
|
|
indvdum@2
|
152 |
predicates.add(cb.equal(root.get(property), value));
|
|
indvdum@2
|
153 |
}
|
|
indvdum@2
|
154 |
}
|
|
indvdum@2
|
155 |
if(notEqualProperties != null) {
|
|
indvdum@2
|
156 |
for(Entry<String, Object> entry: equalProperties.entrySet()){
|
|
indvdum@2
|
157 |
String property = entry.getKey();
|
|
indvdum@2
|
158 |
Object value = entry.getValue();
|
|
indvdum@2
|
159 |
predicates.add(cb.notEqual(root.get(property), value));
|
|
indvdum@2
|
160 |
}
|
|
indvdum@2
|
161 |
}
|
|
indvdum@2
|
162 |
query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
|
|
indvdum@2
|
163 |
return em.createQuery(query).getResultList();
|
|
indvdum@2
|
164 |
}
|
|
indvdum@2
|
165 |
|
|
indvdum@2
|
166 |
public <T> T find(Class<T> entityClass, Map<String, Object> equalProperties, Map<String, Object> notEqualProperties) {
|
|
indvdum@2
|
167 |
CriteriaBuilder cb = em.getCriteriaBuilder();
|
|
indvdum@2
|
168 |
CriteriaQuery<T> query = cb.createQuery(entityClass);
|
|
indvdum@2
|
169 |
Root<T> root = query.from(entityClass);
|
|
indvdum@2
|
170 |
Collection<Predicate> predicates = new HashSet<Predicate>();
|
|
indvdum@2
|
171 |
if(equalProperties != null) {
|
|
indvdum@2
|
172 |
for(Entry<String, Object> entry: equalProperties.entrySet()){
|
|
indvdum@2
|
173 |
String property = entry.getKey();
|
|
indvdum@2
|
174 |
Object value = entry.getValue();
|
|
indvdum@2
|
175 |
predicates.add(cb.equal(root.get(property), value));
|
|
indvdum@2
|
176 |
}
|
|
indvdum@2
|
177 |
}
|
|
indvdum@2
|
178 |
if(notEqualProperties != null) {
|
|
indvdum@2
|
179 |
for(Entry<String, Object> entry: equalProperties.entrySet()){
|
|
indvdum@2
|
180 |
String property = entry.getKey();
|
|
indvdum@2
|
181 |
Object value = entry.getValue();
|
|
indvdum@2
|
182 |
predicates.add(cb.notEqual(root.get(property), value));
|
|
indvdum@2
|
183 |
}
|
|
indvdum@2
|
184 |
}
|
|
indvdum@2
|
185 |
query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
|
|
indvdum@2
|
186 |
T result = null;
|
|
indvdum@2
|
187 |
try {
|
|
indvdum@2
|
188 |
result = em.createQuery(query).getSingleResult();
|
|
indvdum@2
|
189 |
} catch (NoResultException e) {
|
|
indvdum@2
|
190 |
log.error("Error while searching object in Database: ", e);
|
|
indvdum@2
|
191 |
}
|
|
indvdum@2
|
192 |
return result;
|
|
indvdum@2
|
193 |
}
|
|
indvdum@2
|
194 |
|
|
indvdum@2
|
195 |
public <T> List<T> list(Class<T> entityClass) {
|
|
indvdum@2
|
196 |
CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(entityClass);
|
|
indvdum@2
|
197 |
query.from(entityClass);
|
|
indvdum@2
|
198 |
return new ArrayList(em.createQuery(query).getResultList());
|
|
indvdum@2
|
199 |
}
|
|
indvdum@2
|
200 |
|
|
indvdum@2
|
201 |
public boolean contains(Collection entities) {
|
|
indvdum@5
|
202 |
boolean res = true;
|
|
indvdum@5
|
203 |
entities.each {res &= contains(it)}
|
|
indvdum@5
|
204 |
return res
|
|
indvdum@5
|
205 |
}
|
|
indvdum@5
|
206 |
|
|
indvdum@5
|
207 |
public boolean contains(Object entity) {
|
|
indvdum@5
|
208 |
return em.contains(entity);
|
|
indvdum@5
|
209 |
}
|
|
indvdum@5
|
210 |
|
|
indvdum@5
|
211 |
private Object getSingleResult(Query query) {
|
|
indvdum@5
|
212 |
try {
|
|
indvdum@5
|
213 |
return query.getSingleResult()
|
|
indvdum@5
|
214 |
}
|
|
indvdum@5
|
215 |
catch (NoResultException e) {
|
|
indvdum@5
|
216 |
return null
|
|
indvdum@5
|
217 |
}
|
|
indvdum@5
|
218 |
}
|
|
indvdum@5
|
219 |
}
|