1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
|
4
|
import org.hibernate.Criteria;
|
5
|
import org.hibernate.Session;
|
6
|
import org.hibernate.SessionFactory;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
|
9
|
import java.io.Serializable;
|
10
|
import java.lang.reflect.ParameterizedType;
|
11
|
|
12
|
public abstract class AbstractDao<PK extends Serializable, T> {
|
13
|
|
14
|
@Autowired
|
15
|
private SessionFactory sessionFactory;
|
16
|
|
17
|
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
|
public 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
|
|
46
|
|
47
|
}
|