net.groupbuy.dao.impl.AdminDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.dao.impl.AdminDaoImpl.java

Source

/*
 * 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 javax.persistence.FlushModeType;
import javax.persistence.NoResultException;

import net.groupbuy.dao.AdminDao;
import net.groupbuy.entity.Admin;

import org.springframework.stereotype.Repository;

/**
 * Dao - ?
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Repository("adminDaoImpl")
public class AdminDaoImpl extends BaseDaoImpl<Admin, Long> implements AdminDao {

    public boolean usernameExists(String username) {
        if (username == null) {
            return false;
        }
        String jpql = "select count(*) from Admin admin where lower(admin.username) = lower(:username)";
        Long count = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT)
                .setParameter("username", username).getSingleResult();
        return count > 0;
    }

    public Admin findByUsername(String username) {
        if (username == null) {
            return null;
        }
        try {
            String jpql = "select admin from Admin admin where lower(admin.username) = lower(:username)";
            return entityManager.createQuery(jpql, Admin.class).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("username", username).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

}