Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.groupbuy.dao.impl; import java.math.BigDecimal; import java.util.Collections; import java.util.Date; import java.util.List; import javax.persistence.FlushModeType; import javax.persistence.NoResultException; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Join; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import net.groupbuy.dao.MemberDao; import net.groupbuy.entity.Member; import net.groupbuy.entity.Order; import net.groupbuy.entity.Product; import net.groupbuy.entity.Order.OrderStatus; import net.groupbuy.entity.Order.PaymentStatus; import org.springframework.stereotype.Repository; /** * Dao - * * @author SHOP++ Team * @version 3.0 */ @Repository("memberDaoImpl") public class MemberDaoImpl extends BaseDaoImpl<Member, Long> implements MemberDao { public boolean usernameExists(String username) { if (username == null) { return false; } String jpql = "select count(*) from Member members where lower(members.username) = lower(:username)"; Long count = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT) .setParameter("username", username).getSingleResult(); return count > 0; } public boolean emailExists(String email) { if (email == null) { return false; } String jpql = "select count(*) from Member members where lower(members.email) = lower(:email)"; Long count = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT) .setParameter("email", email).getSingleResult(); return count > 0; } public Member findByUsername(String username) { if (username == null) { return null; } try { String jpql = "select members from Member members where lower(members.username) = lower(:username)"; return entityManager.createQuery(jpql, Member.class).setFlushMode(FlushModeType.COMMIT) .setParameter("username", username).getSingleResult(); } catch (NoResultException e) { return null; } } public List<Member> findListByEmail(String email) { if (email == null) { return Collections.<Member>emptyList(); } String jpql = "select members from Member members where lower(members.email) = lower(:email)"; return entityManager.createQuery(jpql, Member.class).setFlushMode(FlushModeType.COMMIT) .setParameter("email", email).getResultList(); } public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Member> member = criteriaQuery.from(Member.class); Join<Product, Order> orders = member.join("orders"); criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance"), criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(orders.<Date>get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(orders.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid")))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); } }