1 |
45046
|
panagiotis
|
package eu.dnetlib.goldoa.service.dao;
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
import java.io.Serializable;
|
5 |
|
|
import java.lang.reflect.ParameterizedType;
|
6 |
|
|
|
7 |
|
|
import org.hibernate.Criteria;
|
8 |
|
|
import org.hibernate.Session;
|
9 |
|
|
import org.hibernate.SessionFactory;
|
10 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
11 |
45057
|
panagiotis
|
|
12 |
45046
|
panagiotis
|
public abstract class AbstractDao<PK extends Serializable, T> {
|
13 |
45057
|
panagiotis
|
|
14 |
|
|
@Autowired
|
15 |
|
|
private SessionFactory sessionFactory;
|
16 |
|
|
|
17 |
45046
|
panagiotis
|
private final Class<T> persistentClass;
|
18 |
|
|
|
19 |
|
|
@SuppressWarnings("unchecked")
|
20 |
|
|
public AbstractDao(){
|
21 |
|
|
this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
protected Session getSession(){
|
25 |
|
|
return sessionFactory.getCurrentSession();
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
@SuppressWarnings("unchecked")
|
29 |
|
|
public T getByKey(PK key) {
|
30 |
|
|
return (T) getSession().get(persistentClass, key);
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
public void persist(T entity) {
|
34 |
|
|
getSession().persist(entity);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
public void delete(T entity) {
|
38 |
|
|
getSession().delete(entity);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
protected Criteria createEntityCriteria(){
|
42 |
|
|
return getSession().createCriteria(persistentClass);
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
}
|