Created
September 27, 2011 13:28
-
-
Save joserodolfofreitas/1245041 to your computer and use it in GitHub Desktop.
creating a framework that provide some crud boilerplate code.
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.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); | |
} | |
} |
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
@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