Java tutorial
/* * 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.Account; import com.tamnd.app.core.repositories.AccountRepo; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.springframework.stereotype.Repository; /** * * @author tamnd */ @Repository public class AccountRepoImpl implements AccountRepo { @PersistenceContext private EntityManager em; @Override public Account findAccount(Integer id) { return em.find(Account.class, id); } @Override public Account createAccount(Account acc) { em.persist(acc); return acc; } @Override public List<Account> findAllAccounts() { Query query = em.createQuery("SELECT a FROM Account a"); return query.getResultList(); } @Override public Account findAccountByName(String name) { Query query = em.createQuery("SELECT a FROM Account a WHERE a.userName=?1"); query.setParameter(1, name); List<Account> accounts = query.getResultList(); if (accounts.isEmpty()) { return null; } else { return accounts.get(0); } } }