Package com.gtp.dao

Source Code of com.gtp.dao.UserDaoImp

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.User;

public class UserDaoImp extends JpaDaoSupport implements UserDao {

 
  @Override
  public User addUser(User u) {
    if(u==null)
      return null;
    getJpaTemplate().persist(u);
    return u;
  }

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

  @Override
  public List<User> getUsers() {
    return (List<User>)getJpaTemplate().execute(new JpaCallback<List<User>>() {
      public List<User> doInJpa(EntityManager em) {
        CriteriaBuilder criteriaBuilder =em.getCriteriaBuilder();
        CriteriaQuery<User> query= criteriaBuilder.createQuery(User.class);
        Root<User> user = query.from(User.class);
        query.select(user);
        List<User> result = em.createQuery(query).getResultList();
        return  result;
      }
      });
  }

  @Override
  public User removeUser(User u) {
    u=getJpaTemplate().merge(u);
    getJpaTemplate().remove(u);
    return u;
  }

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

Related Classes of com.gtp.dao.UserDaoImp

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.