com.tamnd.app.core.repositories.jpa.AccountRoleRepoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.tamnd.app.core.repositories.jpa.AccountRoleRepoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnd.app.core.repositories.jpa;

import com.tamnd.app.core.entities.AccountRole;
import com.tamnd.app.core.repositories.AccountRoleRepo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author nguye_000
 */
@Repository
public class AccountRoleRepoImpl implements AccountRoleRepo {

    @PersistenceContext
    private EntityManager em;

    @Override
    public AccountRole createAccountRole(AccountRole role) {
        em.persist(role);
        return role;
    }

    @Override
    public AccountRole findAccountRole(Integer id) {
        return em.find(AccountRole.class, id);
    }

    @Override
    public AccountRole findAccountRoleByName(String roleName) {
        Query query = em.createQuery("SELECT a FROM AccountRole a WHERE a.roleName=?1");
        query.setParameter(1, roleName);
        List<AccountRole> list = query.getResultList();
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }
}