2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.backend.dao;
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import io.dropwizard.hibernate.AbstractDAO;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import java.util.Collection;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import org.hibernate.SessionFactory;
|
2013-06-06 10:57:58 +02:00
|
|
|
|
2013-03-23 19:46:09 +01:00
|
|
|
import com.commafeed.backend.model.AbstractModel;
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.mysema.query.jpa.hibernate.HibernateQuery;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public abstract class GenericDAO<T extends AbstractModel> extends AbstractDAO<T> {
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2014-08-14 11:40:30 +02:00
|
|
|
protected GenericDAO(SessionFactory sessionFactory) {
|
2014-08-08 16:49:02 +02:00
|
|
|
super(sessionFactory);
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
protected HibernateQuery newQuery() {
|
|
|
|
|
return new HibernateQuery(currentSession());
|
2013-07-22 16:31:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public void saveOrUpdate(T model) {
|
|
|
|
|
persist(model);
|
2013-03-23 19:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public void saveOrUpdate(Collection<T> models) {
|
2014-12-12 10:06:23 +01:00
|
|
|
models.forEach(m -> persist(m));
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public void merge(T model) {
|
|
|
|
|
currentSession().merge(model);
|
2013-03-30 11:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public void merge(Collection<T> models) {
|
2014-12-12 10:06:23 +01:00
|
|
|
models.forEach(m -> merge(m));
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-30 11:37:57 +01:00
|
|
|
public T findById(Long id) {
|
2014-08-08 16:49:02 +02:00
|
|
|
return get(id);
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public void delete(T object) {
|
|
|
|
|
if (object != null) {
|
|
|
|
|
currentSession().delete(object);
|
2013-06-27 10:13:07 +02:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public int delete(Collection<T> objects) {
|
2014-12-12 10:06:23 +01:00
|
|
|
objects.forEach(o -> delete(o));
|
2014-08-08 16:49:02 +02:00
|
|
|
return objects.size();
|
2013-06-27 10:13:07 +02:00
|
|
|
}
|
2013-07-22 16:31:29 +02:00
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|