Files
Athou_commafeed/src/main/java/com/commafeed/backend/dao/GenericDAO.java

171 lines
4.3 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend.dao;
import java.util.Arrays;
import java.util.Collection;
2013-03-20 20:33:42 +01:00
import java.util.List;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
2013-04-11 20:49:08 +02:00
import javax.persistence.TypedQuery;
2013-03-20 20:33:42 +01:00
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
2013-04-12 10:44:41 +02:00
import javax.persistence.metamodel.Attribute;
2013-03-20 20:33:42 +01:00
import org.hibernate.Query;
2013-06-06 10:57:58 +02:00
import org.hibernate.Session;
2013-03-23 19:46:09 +01:00
import com.commafeed.backend.model.AbstractModel;
2013-03-20 20:33:42 +01:00
import com.google.common.reflect.TypeToken;
2013-03-21 08:55:53 +01:00
@SuppressWarnings("serial")
public abstract class GenericDAO<T extends AbstractModel> {
2013-03-20 20:33:42 +01:00
private TypeToken<T> type = new TypeToken<T>(getClass()) {
};
@PersistenceContext
protected EntityManager em;
protected CriteriaBuilder builder;
@PostConstruct
public void init() {
builder = em.getCriteriaBuilder();
}
public Session getSession() {
Session session = em.unwrap(Session.class);
return session;
}
public void saveOrUpdate(Collection<? extends AbstractModel> models) {
Session session = getSession();
2013-06-12 05:08:27 +02:00
int i = 1;
2013-04-25 14:43:37 +02:00
for (AbstractModel model : models) {
2013-06-06 10:57:58 +02:00
session.saveOrUpdate(model);
2013-06-12 05:08:27 +02:00
if (i % 50 == 0) {
session.flush();
session.clear();
}
2013-04-25 14:43:37 +02:00
}
}
public void saveOrUpdate(AbstractModel... models) {
2013-06-06 09:54:17 +02:00
saveOrUpdate(Arrays.asList(models));
2013-03-23 19:46:09 +01:00
}
public void delete(AbstractModel object) {
2013-03-30 11:57:11 +01:00
if (object != null) {
object = em.merge(object);
em.remove(object);
}
2013-03-20 20:33:42 +01:00
}
2013-11-16 07:30:01 +01:00
public int delete(Collection<? extends AbstractModel> objects) {
for (AbstractModel object : objects) {
2013-03-30 11:37:57 +01:00
delete(object);
}
2013-11-16 07:30:01 +01:00
return objects.size();
2013-03-30 11:37:57 +01:00
}
public void deleteById(Long id) {
2013-03-20 20:33:42 +01:00
Object ref = em.getReference(getType(), id);
2013-03-30 11:57:11 +01:00
if (ref != null) {
em.remove(ref);
}
2013-03-20 20:33:42 +01:00
}
2013-03-30 11:37:57 +01:00
public T findById(Long id) {
2013-03-20 20:33:42 +01:00
T t = em.find(getType(), id);
return t;
}
public List<T> findAll() {
2013-06-08 16:15:11 +02:00
CriteriaQuery<T> query = builder.createQuery(getType());
query.from(getType());
return em.createQuery(query).getResultList();
2013-03-20 20:33:42 +01:00
}
public List<T> findAll(int startIndex, int count) {
2013-06-08 16:15:11 +02:00
CriteriaQuery<T> query = builder.createQuery(getType());
query.from(getType());
TypedQuery<T> q = em.createQuery(query);
q.setMaxResults(count);
q.setFirstResult(startIndex);
return q.getResultList();
2013-03-20 20:33:42 +01:00
}
2013-07-25 09:17:33 +02:00
public List<T> findAll(int startIndex, int count, String orderBy, boolean asc) {
2013-06-08 16:15:11 +02:00
CriteriaQuery<T> query = builder.createQuery(getType());
Root<T> root = query.from(getType());
2013-03-29 12:59:21 +01:00
if (asc) {
2013-06-08 16:15:11 +02:00
query.orderBy(builder.asc(root.get(orderBy)));
2013-03-29 12:59:21 +01:00
} else {
2013-06-08 16:15:11 +02:00
query.orderBy(builder.desc(root.get(orderBy)));
2013-03-29 12:59:21 +01:00
}
2013-06-08 16:15:11 +02:00
TypedQuery<T> q = em.createQuery(query);
q.setMaxResults(count);
q.setFirstResult(startIndex);
return q.getResultList();
2013-03-29 12:59:21 +01:00
}
2013-03-20 20:33:42 +01:00
public long getCount() {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Long> query = builder.createQuery(Long.class);
Root<T> root = query.from(getType());
query.select(builder.count(root));
return em.createQuery(query).getSingleResult();
}
2013-06-08 16:15:11 +02:00
protected <V> List<T> findByField(Attribute<T, V> field, V value) {
return findByField(field, value, false);
}
2013-07-25 09:17:33 +02:00
protected <V> List<T> findByField(Attribute<T, V> field, V value, boolean cache) {
2013-06-08 16:15:11 +02:00
CriteriaQuery<T> query = builder.createQuery(getType());
Root<T> root = query.from(getType());
query.where(builder.equal(root.get(field.getName()), value));
TypedQuery<T> q = em.createQuery(query);
if (cache) {
cache(q);
}
2013-06-08 16:15:11 +02:00
return em.createQuery(query).getResultList();
2013-03-20 20:33:42 +01:00
}
protected <Q> void limit(TypedQuery<Q> query, int offset, int limit) {
2013-04-11 20:49:08 +02:00
if (offset > -1) {
query.setFirstResult(offset);
}
if (limit > -1) {
query.setMaxResults(limit);
}
}
protected <Q> TypedQuery<Q> readOnly(TypedQuery<Q> query) {
query.unwrap(Query.class).setReadOnly(true);
return query;
}
protected <Q> TypedQuery<Q> cache(TypedQuery<Q> query) {
query.unwrap(Query.class).setCacheable(true);
return query;
}
2013-07-01 21:15:32 +02:00
protected void setTimeout(javax.persistence.Query query, int queryTimeout) {
if (queryTimeout > 0) {
query.setHint("javax.persistence.query.timeout", queryTimeout);
}
}
2013-03-20 20:33:42 +01:00
@SuppressWarnings("unchecked")
protected Class<T> getType() {
return (Class<T>) type.getRawType();
}
}