Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
creating a framework that provide some crud boilerplate code.
package org.jboss.saf;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
public class EntityInstanceHandler implements Serializable {
@PersistenceContext
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);
query.from(entity);
return em.createQuery(query).getResultList();
}
// we add some wrappers here.
public void remove(Object entity) {
em.remove(entity);
}
public void delete(Object entity) {
this.remove(entity);
}
}
@Model
@Stateful
public class EntityService implements Serializable {
@Inject
EntityInstanceHandler entityHandler;
EntityExample entity = new EntityExample();
public String saveEntityExample() {
entityHandler.saveOrUpdate(entity);
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
You can’t perform that action at this time.