Skip to content

Instantly share code, notes, and snippets.

@Snugglepantz
Created November 4, 2013 19:28
Show Gist options
  • Save Snugglepantz/7307882 to your computer and use it in GitHub Desktop.
Save Snugglepantz/7307882 to your computer and use it in GitHub Desktop.
Modified Netbeans Abstract Facade
package me.slashfix.angular.rest;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
/**
*
* @author jhoffma5
*/
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public T create(T entity) {
getEntityManager().persist(entity);
getEntityManager().flush();
return entity;
}
public T edit(T entity) {
getEntityManager().merge(entity);
getEntityManager().flush();
return entity;
}
public void delete(Object id) {
Object ref = getEntityManager().getReference(entityClass, id);
getEntityManager().remove(ref);
}
public T remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
getEntityManager().flush();
return entity;
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
public List<T> findWithNamedQuery(String namedQueryName) {
return getEntityManager().createNamedQuery(namedQueryName).getResultList();
}
public List findWithNamedQuery(String namedQueryName, Map<String, Object> parameters, int resultLimit) {
Set<Map.Entry<String, Object>> rawParameters = parameters.entrySet();
Query query = getEntityManager().createNamedQuery(namedQueryName);
if (resultLimit > 0) {
query.setMaxResults(resultLimit);
}
for (Map.Entry<String, Object> entry : rawParameters) {
query.setParameter(entry.getKey(), entry.getValue());
}
return query.getResultList();
}
public List<T> findWithQuery(String queryName) {
return getEntityManager().createQuery(queryName).getResultList();
}
public List<T> findByNativeQuery(String sql) {
return getEntityManager().createNativeQuery(sql, entityClass).getResultList();
}
public T findSingleWithNamedQuery(String namedQueryName) {
T result = null;
try {
result = (T) getEntityManager().createNamedQuery(namedQueryName).getSingleResult();
} catch (NoResultException e) {
}
return result;
}
public T findSingleWithNamedQuery(String namedQueryName, Map<String, Object> parameters) {
Set<Map.Entry<String, Object>> rawParameters = parameters.entrySet();
Query query = getEntityManager().createNamedQuery(namedQueryName);
for (Map.Entry<String, Object> entry : rawParameters) {
query.setParameter(entry.getKey(), entry.getValue());
}
T result = null;
try {
result = (T) query.getSingleResult();
} catch (NoResultException e) {
}
return result;
}
}
@nkcr
Copy link

nkcr commented Mar 11, 2016

You made my day, thanks!

@ljnelson
Copy link

ljnelson commented Jul 6, 2016

Just a quick comment: in line 31 you must return the return value from EntityManager#merge(Object), not the original entity. See my article on "the bag" for details.

@darieldap
Copy link

Hello guys im in a problem, Im trying to use this example and no result and in the web I cant find nothing related, Im doing a web service where I need to excecute a Stored procedure to do the WS I follow this guide https://netbeans.org/kb/docs/websvc/rest.html, but there is no info about how to add the exceution of a stored procedure, or at least excecute a query Im trying with this but no rusult so far, can you help me with this thz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment