Package com.gtp.dao

Source Code of com.gtp.dao.ArticleDaoImp

package com.gtp.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;  
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.gtp.domain.Article;

import commons.ExistedException;
import commons.NotFoundException;
public class ArticleDaoImp extends JpaDaoSupport implements ArticleDao
 
  @Override
  public Article addArticle(Article a) throws ExistedException {
    getJpaTemplate().persist(a);
    return a;
  }

  @Override
  public Article removeArticle(Article a) throws NotFoundException {
    a=getJpaTemplate().merge(a);
    getJpaTemplate().remove(a);
    return a;
  }
 
  @Override
  public Article updateArticle(Article a) throws NotFoundException, ExistedException {
    getJpaTemplate().merge(a);
    return a;
  }

  @Override
  public Article findByName(final String name) {
    return (Article)getJpaTemplate().execute(new JpaCallback<Article>() {
      public Article doInJpa(EntityManager em) {
        CriteriaBuilder criteriaBuilder =em.getCriteriaBuilder();
        CriteriaQuery<Article> query= criteriaBuilder.createQuery(Article.class);
        Root<Article> auth = query.from(Article.class);
        Article Article=null;
        query.select(auth).where(criteriaBuilder.equal(auth.get("name"),name));
        List<Article> result = em.createQuery(query).getResultList();
        if(result.isEmpty())
          return null;
        Article=(Article) result.get(0);
        return Article;
      }
      });
  }
 
  @Override
  public List<Article> findAllArticles() {
    return (List<Article>)getJpaTemplate().execute(new JpaCallback<List<Article>>() {
      public List<Article> doInJpa(EntityManager em) {
        CriteriaBuilder criteriaBuilder =em.getCriteriaBuilder();
        CriteriaQuery<Article> query= criteriaBuilder.createQuery(Article.class);
        Root<Article> auth = query.from(Article.class);
        query.select(auth);
        List<Article> result = em.createQuery(query).getResultList();
        return  result;
      }
      });
  }

  @Override
  public Article find(int id) {
    return getJpaTemplate().find(Article.class, id);
  }
}
TOP

Related Classes of com.gtp.dao.ArticleDaoImp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.