summaryrefslogtreecommitdiffstats
path: root/proxy/code/src/org/fedoraproject/candlepin/model/AbstractHibernateRepository.java
blob: 87c58f42d0186e21e0a01891e1377ffa0c2604b8 (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
package org.fedoraproject.candlepin.model;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import org.hibernate.Session;

public abstract class AbstractHibernateRepository<E> {
    
    protected final Session session;
    private final Class<E> entityType;
    
    @SuppressWarnings("unchecked")
    protected AbstractHibernateRepository(Session session) {
        this.session = session;
        entityType = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }
    
    public E find(Serializable id) {
        return get(entityType, id);
    }
    
    public E create(E entity) {
        save(entity);
        flush();
        return entity;
    }
    
    protected final <T> T get(Class<T> clazz, Serializable id) {
        return clazz.cast(session.get(clazz, id));
    }
    
    protected final void save(Object anObject) {
        session.save(anObject);
    }
    
    protected final void flush() {
        session.flush();
    }
}