Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
creating a framework that provide some crud boilerplate code.
package org.jboss.saf;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
public class EntityInstanceHandler implements Serializable {
EntityManager em;
Object instance = null;
public void saveOrUpdate(Object entity) {
* verify which method should be used, merge, update or save
public <T> T findById(Class<T> entityClass, Object primaryKey) {
// could we keep a reference for object instance?
return em.find(entityClass, primaryKey);
public <T> List<T> listAll(Class<T> entity) {
CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(entity);
return em.createQuery(query).getResultList();
// we add some wrappers here.
public void remove(Object entity) {
public void delete(Object entity) {
public class EntityService implements Serializable {
EntityInstanceHandler entityHandler;
EntityExample entity = new EntityExample();
public String saveEntityExample() {
return "saflist.xhtml"; //I don't like returning string
public EntityExample getEntity() {
return entity;
public void setEntity(EntityExample entity) {
this.entity = entity;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment