mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
removed wicket and tomee, use dropwizard instead. remove wro4j, use gulp instead
This commit is contained in:
@@ -1,160 +1,59 @@
|
||||
package com.commafeed.backend.dao;
|
||||
|
||||
import java.util.Arrays;
|
||||
import io.dropwizard.hibernate.AbstractDAO;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.metamodel.Attribute;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import com.commafeed.backend.model.AbstractModel;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.mysema.query.jpa.hibernate.HibernateQuery;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class GenericDAO<T extends AbstractModel> {
|
||||
public abstract class GenericDAO<T extends AbstractModel> extends AbstractDAO<T> {
|
||||
|
||||
private TypeToken<T> type = new TypeToken<T>(getClass()) {
|
||||
};
|
||||
|
||||
@PersistenceContext
|
||||
protected EntityManager em;
|
||||
|
||||
protected CriteriaBuilder builder;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
builder = em.getCriteriaBuilder();
|
||||
public GenericDAO(SessionFactory sessionFactory) {
|
||||
super(sessionFactory);
|
||||
}
|
||||
|
||||
public Session getSession() {
|
||||
Session session = em.unwrap(Session.class);
|
||||
return session;
|
||||
protected HibernateQuery newQuery() {
|
||||
return new HibernateQuery(currentSession());
|
||||
}
|
||||
|
||||
public void saveOrUpdate(Collection<? extends AbstractModel> models) {
|
||||
Session session = getSession();
|
||||
int i = 1;
|
||||
for (AbstractModel model : models) {
|
||||
session.saveOrUpdate(model);
|
||||
if (i % 50 == 0) {
|
||||
session.flush();
|
||||
session.clear();
|
||||
}
|
||||
public void saveOrUpdate(T model) {
|
||||
persist(model);
|
||||
}
|
||||
|
||||
public void saveOrUpdate(Collection<T> models) {
|
||||
for (T model : models) {
|
||||
persist(model);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveOrUpdate(AbstractModel... models) {
|
||||
saveOrUpdate(Arrays.asList(models));
|
||||
public void merge(T model) {
|
||||
currentSession().merge(model);
|
||||
}
|
||||
|
||||
public void delete(AbstractModel object) {
|
||||
if (object != null) {
|
||||
object = em.merge(object);
|
||||
em.remove(object);
|
||||
}
|
||||
}
|
||||
|
||||
public int delete(Collection<? extends AbstractModel> objects) {
|
||||
for (AbstractModel object : objects) {
|
||||
delete(object);
|
||||
}
|
||||
return objects.size();
|
||||
}
|
||||
|
||||
public void deleteById(Long id) {
|
||||
Object ref = em.getReference(getType(), id);
|
||||
if (ref != null) {
|
||||
em.remove(ref);
|
||||
public void merge(Collection<T> models) {
|
||||
for (T model : models) {
|
||||
merge(model);
|
||||
}
|
||||
}
|
||||
|
||||
public T findById(Long id) {
|
||||
T t = em.find(getType(), id);
|
||||
return t;
|
||||
return get(id);
|
||||
}
|
||||
|
||||
public List<T> findAll() {
|
||||
CriteriaQuery<T> query = builder.createQuery(getType());
|
||||
query.from(getType());
|
||||
return em.createQuery(query).getResultList();
|
||||
}
|
||||
|
||||
public List<T> findAll(int startIndex, int count) {
|
||||
CriteriaQuery<T> query = builder.createQuery(getType());
|
||||
query.from(getType());
|
||||
TypedQuery<T> q = em.createQuery(query);
|
||||
q.setMaxResults(count);
|
||||
q.setFirstResult(startIndex);
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
public List<T> findAll(int startIndex, int count, String orderBy, boolean asc) {
|
||||
|
||||
CriteriaQuery<T> query = builder.createQuery(getType());
|
||||
Root<T> root = query.from(getType());
|
||||
|
||||
if (asc) {
|
||||
query.orderBy(builder.asc(root.get(orderBy)));
|
||||
} else {
|
||||
query.orderBy(builder.desc(root.get(orderBy)));
|
||||
}
|
||||
|
||||
TypedQuery<T> q = em.createQuery(query);
|
||||
q.setMaxResults(count);
|
||||
q.setFirstResult(startIndex);
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
protected <V> List<T> findByField(Attribute<T, V> field, V value) {
|
||||
return findByField(field, value, false);
|
||||
}
|
||||
|
||||
protected <V> List<T> findByField(Attribute<T, V> field, V value, boolean cache) {
|
||||
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);
|
||||
}
|
||||
return em.createQuery(query).getResultList();
|
||||
}
|
||||
|
||||
protected <Q> void limit(TypedQuery<Q> query, int offset, int limit) {
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
public void delete(T object) {
|
||||
if (object != null) {
|
||||
currentSession().delete(object);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
public int delete(Collection<T> objects) {
|
||||
for (T object : objects) {
|
||||
delete(object);
|
||||
}
|
||||
return objects.size();
|
||||
}
|
||||
|
||||
protected void setTimeout(javax.persistence.Query query, int queryTimeout) {
|
||||
@@ -162,9 +61,4 @@ public abstract class GenericDAO<T extends AbstractModel> {
|
||||
query.setHint("javax.persistence.query.timeout", queryTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class<T> getType() {
|
||||
return (Class<T>) type.getRawType();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user