001package org.woehlke.javaee7.petclinic.dao;
002
003import org.woehlke.javaee7.petclinic.entities.PetType;
004
005import javax.ejb.Stateless;
006import javax.persistence.EntityManager;
007import javax.persistence.PersistenceContext;
008import javax.persistence.TypedQuery;
009import java.util.List;
010import java.util.logging.Logger;
011
012/**
013 * Created with IntelliJ IDEA.
014 * User: Fert
015 * Date: 06.01.14
016 * Time: 11:51
017 * To change this template use File | Settings | File Templates.
018 */
019@Stateless
020public class PetTypeDaoImpl implements PetTypeDao {
021
022    private static Logger log = Logger.getLogger(PetTypeDaoImpl.class.getName());
023
024    @PersistenceContext(unitName="javaee7petclinic")
025    private EntityManager entityManager;
026
027
028    @Override
029    public List<PetType> getAll() {
030        TypedQuery<PetType> q = entityManager.createQuery("select pt from PetType pt order by pt.name",PetType.class);
031        List<PetType> list =  q.getResultList();
032        return list;
033    }
034
035    @Override
036    public void delete(long id) {
037        PetType petType = entityManager.find(PetType.class, id);
038        entityManager.remove(petType);
039    }
040
041    @Override
042    public void addNew(PetType petType) {
043        log.info("addNewPetType: "+petType.toString());
044        entityManager.persist(petType);
045    }
046
047    @Override
048    public PetType findById(long id) {
049        PetType petType = entityManager.find(PetType.class, id);
050        return petType;
051    }
052
053    @Override
054    public void update(PetType petType) {
055        entityManager.merge(petType);
056    }
057}