Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.inetsource.jsfforum.db; import de.inetsource.jsfforum.entity.Post; import de.inetsource.jsfforum.entity.Thread; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * @author Joerg Wiesmann joerg.wiesmann@gmail.com */ @Service public class PostFacade extends AbstractFacade<Post> { @PersistenceContext(unitName = "forumPU") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } public PostFacade() { super(Post.class); } public List<Post> findPostsFromTopic(Integer threadId) { Query query = getEntityManager().createNamedQuery("Post.findByThreadId"); query.setParameter("id", threadId); return query.getResultList(); } public List<Post> findRange(Thread thread, int[] rangeArray) { Query query = getEntityManager().createNamedQuery("Post.findByThreadId"); query.setParameter("id", thread.getId()); query.setFirstResult(rangeArray[0]); query.setMaxResults(rangeArray[1]); return query.getResultList(); } public int count(Thread thread) { Query q = em.createQuery("Select count(p.id) from Post p where p.threadId = :thread"); q.setParameter("thread", thread); return ((Long) q.getSingleResult()).intValue(); } @Override @Transactional public void edit(Post entity) { super.edit(entity); } }