Skip to content

Instantly share code, notes, and snippets.

@senthilmuthiah
Created March 25, 2013 10:36
Show Gist options
  • Save senthilmuthiah/5236275 to your computer and use it in GitHub Desktop.
Save senthilmuthiah/5236275 to your computer and use it in GitHub Desktop.
Address Book Using JPA +ZK + Spring.
package addressbook.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.springframework.stereotype.Repository;
@Repository
public class CRUDDaoImpl implements CRUDDao {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> klass) {
return em.createQuery("Select t from " + klass.getSimpleName() + " t")
.getResultList();
}
public <T> T save(T t) {
T newRecord = null;
newRecord = em.merge(t);
return newRecord;
}
public <T> void delete(T t) {
em.remove(em.merge(t));
em.flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment