Created
October 15, 2012 08:51
-
-
Save asicfr/3891525 to your computer and use it in GitHub Desktop.
struts2RestJpaBootstrap - dao generic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.demo.util; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Query; | |
import javax.persistence.TypedQuery; | |
public abstract class GenericJpaDao<T, PK extends java.io.Serializable> { | |
private final Class<T> persistentClass; | |
/** | |
* @param persistentClass | |
*/ | |
public GenericJpaDao(final Class<T> persistentClass) { | |
this.persistentClass = persistentClass; | |
} | |
/** | |
* @param <T> | |
* @param id | |
* @return | |
*/ | |
public <T> T findById(PK id) { | |
final EntityManager em = getEntityManager(); | |
return (T) em.find(this.persistentClass, id); | |
} | |
/** | |
* @param queryName | |
* @param queryParams | |
* @return | |
*/ | |
public List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams) { | |
final EntityManager em = getEntityManager(); | |
final Query query = em.createNamedQuery(queryName); | |
final Iterator<String> i = queryParams.keySet().iterator(); | |
while (i.hasNext()) { | |
String key = i.next(); | |
query.setParameter(key, queryParams.get(key)); | |
} | |
return query.getResultList(); | |
} | |
/** | |
* @param <T> | |
* @param query | |
* @return | |
*/ | |
public <T> Collection<T> loadList(String query) { | |
final EntityManager em = getEntityManager(); | |
final TypedQuery q = em.createQuery(query, this.persistentClass); | |
return q.getResultList(); | |
} | |
/** | |
* @param <T> | |
* @param query | |
* @return | |
*/ | |
public <T> List<T> loadAll() { | |
final EntityManager em = getEntityManager(); | |
final Query query = em.createQuery("from " + persistentClass.getName()); | |
return query.getResultList(); | |
} | |
/** | |
* @param <T> | |
* @return | |
*/ | |
public <T> T insert(T entity) { | |
final EntityManager em = getEntityManager(); | |
em.persist(entity); | |
return (entity); | |
} | |
/** | |
* @param <T> | |
* @param entity | |
* @return | |
*/ | |
public <T> T update(T entity) { | |
final EntityManager em = getEntityManager(); | |
entity = em.merge(entity); | |
return entity; | |
} | |
/** | |
* @param pk | |
*/ | |
public void delete(PK pk) { | |
final EntityManager em = getEntityManager(); | |
final T entity = em.find(this.persistentClass, pk); | |
if (entity != null) { | |
em.remove(entity); | |
} | |
} | |
/** | |
* @param entity | |
*/ | |
public void delete(T entity) { | |
final EntityManager em = getEntityManager(); | |
if (entity != null) { | |
em.refresh(entity); | |
em.remove(entity); | |
} | |
} | |
// *************************************************************************************** | |
protected EntityManager getEntityManager() { | |
return EntityManagerHelper.getEntityManager(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment