bc8.movies.dao.MovieDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for bc8.movies.dao.MovieDaoImpl.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 bc8.movies.dao;

import bc8.movies.entities.Movie;
import bc8.movies.utils.Constants;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Catalin
 */
@Repository
public class MovieDaoImpl {

    @PersistenceContext
    EntityManager em;

    public boolean addMovie(Movie movie) {
        if (!movie.getName().contains(Constants.INVALID_STRING)) {
            em.persist(movie);

            return true;
        } else {
            return false;
        }
    }

    public boolean isMovieInDB(String movieName) {
        Query query = em.createQuery("select movie from Movie movie where movie.name= :movieName");

        query.setParameter("movieName", movieName);

        return !query.getResultList().isEmpty();
    }

    public List<Movie> getMovies() {
        Query query = em.createQuery("select movie from Movie movie");

        return query.getResultList();
    }

    public Movie getMovie(String movieName) {
        Query query = em.createQuery("select movie from Movie movie where movie.name= :movieName");

        query.setParameter("movieName", movieName);

        return (Movie) query.getSingleResult();
    }
}