summaryrefslogtreecommitdiffstats
path: root/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
blob: c5c2d91f7e82af79f2b4acab71b401a1d42cc9cd (plain) (blame)
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
package nu.xss.jpa.dao;

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

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class GenericJpaDao<E,K> implements Dao<E,K>, Serializable {
	
	private static final long serialVersionUID = 4998055731089977476L;
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@PersistenceContext
	protected EntityManager entityManager;
	
	protected Class<E> entity;
	
	@SuppressWarnings("unchecked")
	public GenericJpaDao() {
		ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
		this.entity = (Class<E>) genericSuperclass.getActualTypeArguments()[0];
	}
	
	public void save(E entity) {
		entityManager.merge(entity);
	}

	public void delete(E entity) {
		entityManager.remove(entity);
	}

	public E findById(K id) {
		return entityManager.find(entity, id);
	}

	public List<E> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

}