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 cz.muni.pa165.carparkapp.DAOImpl; import cz.muni.pa165.carparkapp.DAO.BranchDAO; import cz.muni.pa165.carparkapp.Entities.Branch; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.Transactional; import org.springframework.stereotype.Repository; /** * * @author coldfront */ @Repository("branchDAO") @Transactional public class BranchDAOImpl implements BranchDAO { @PersistenceContext private EntityManager em; public BranchDAOImpl() { } public EntityManager getEm() { return em; } public void setEm(EntityManager em) { this.em = em; } @Override public Branch createBranch(Branch branch) { if (branch == null) throw new NullPointerException("Argument cannot be null"); em.persist(branch); return branch; } @Override public boolean updateBranch(Branch branch) { if (branch == null) throw new NullPointerException("Argument cannot be null"); if (findBranch(branch.getId()) == null) return false; em.merge(branch); return true; } @Override public Branch findBranch(int id) { if (id < 1) throw new IllegalArgumentException("id must be positive number"); Branch branch = em.find(Branch.class, id); return branch; } @Override public boolean deleteBranch(Branch branch) { if (branch == null) throw new NullPointerException("Argument cannot be null"); Branch attached = em.find(Branch.class, branch.getId()); if (attached == null) { return false; } em.remove(attached); return true; } @Override public List<Branch> getAllBranches() { List<Branch> allBranches = em.createQuery("SELECT b FROM Branch b", Branch.class).getResultList(); return allBranches; } }