cz.fi.muni.pa036.airticketbooking.dao.impl.CityDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for cz.fi.muni.pa036.airticketbooking.dao.impl.CityDaoImpl.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 cz.fi.muni.pa036.airticketbooking.dao.impl;

import cz.fi.muni.pa036.airticketbooking.dao.CityDao;
import cz.fi.muni.pa036.airticketbooking.entity.Airport;
import cz.fi.muni.pa036.airticketbooking.entity.City;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Luk Valach
 */
@Repository
public class CityDaoImpl implements CityDao {

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;

    public CityDaoImpl() {
    }

    @Override
    public City getById(Long id) {
        try {
            if (id == null) {
                throw new IllegalArgumentException("Id cannot be null.");
            }

            City objectTemp = (City) em.find(City.class, id);
            return objectTemp;
        } catch (PersistenceException | IllegalArgumentException ex) {
            throw new DataAccessException(ex.getMessage(), ex) {
            };
        }
    }

    @Override
    public List<City> getAll() {
        try {
            Query q = em.createQuery("FROM City");
            List<City> objectTemp = q.getResultList();

            return Collections.unmodifiableList(objectTemp);
        } catch (PersistenceException | IllegalArgumentException ex) {
            throw new DataAccessException(ex.getMessage(), ex) {
            };
        }
    }
}