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

54 lines
1.1 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend.dao;
import io.dropwizard.hibernate.AbstractDAO;
2013-03-20 20:33:42 +01:00
import java.util.Collection;
2013-03-20 20:33:42 +01: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;
import com.mysema.query.jpa.hibernate.HibernateQuery;
2013-03-20 20:33:42 +01: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) {
super(sessionFactory);
2013-03-20 20:33:42 +01:00
}
protected HibernateQuery newQuery() {
return new HibernateQuery(currentSession());
}
public void saveOrUpdate(T model) {
persist(model);
2013-03-23 19:46:09 +01: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
}
public void merge(T model) {
currentSession().merge(model);
2013-03-30 11:37:57 +01: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) {
return get(id);
2013-03-20 20:33:42 +01:00
}
public void delete(T object) {
if (object != null) {
currentSession().delete(object);
}
2013-03-20 20:33:42 +01:00
}
public int delete(Collection<T> objects) {
2014-12-12 10:06:23 +01:00
objects.forEach(o -> delete(o));
return objects.size();
}
2013-03-20 20:33:42 +01:00
}