blob: 2078fef02879dcc846d7853c5424a3a4c1782bdc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package org.fedoraproject.candlepin.model;
import java.io.Serializable;
import javax.persistence.EntityManager;
import org.hibernate.Session;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wideplay.warp.persist.Transactional;
public class AbstractHibernateRepository<E> {
@Inject protected Provider<EntityManager> entityManager;
private final Class<E> entityType;
protected AbstractHibernateRepository(Class<E> entityType) {
//entityType = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
this.entityType = entityType;
}
public E find(Serializable id) {
return get(entityType, id);
}
@Transactional
public E create(E entity) {
save(entity);
flush();
return entity;
}
protected final <T> T get(Class<T> clazz, Serializable id) {
return clazz.cast(currentSession().get(clazz, id));
}
protected final void save(Object anObject) {
currentSession().save(anObject);
}
protected final void flush() {
entityManager.get().flush();
}
protected Session currentSession() {
return (Session) entityManager.get().getDelegate();
}
protected Provider<EntityManager> getEntityManager() {
return entityManager;
}
protected void setEntityManager(Provider<EntityManager> entityManager) {
this.entityManager = entityManager;
}
}
|