net.roseindia.dao
This package is the part of Spring and Hibernate JPA integration application which contains DAO service interface and it's implementation class.
For returning to main example(Spring Jpa Hibernate). Click here.
ArticleDao.java
package net.roseindia.dao; import java.util.List; import net.roseindia.model.Article; public interface ArticleDao { // To Save the article detail public void saveArticle ( Article Article ); // To get list of all articles public List<Article> listArticles(); }
ArticleDaoImpl.java
package net.roseindia.dao; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import net.roseindia.model.Article; import org.hibernate.Session; import org.springframework.stereotype.Repository; @Repository("articleDao") public class ArticleDaoImpl implements ArticleDao { private EntityManager em; @PersistenceContext public void setEntityManager(EntityManager em) { this.em = em; } // To Save the article detail public void saveArticle(Article article) { article.setAddedDate(new Date()); em.persist(article); } // To get list of all articles @SuppressWarnings("unchecked") public List<Article> listArticles() { Session session = (Session) em.getDelegate(); return (List<Article>) session.createCriteria(Article.class).list(); } }