|
| 1 | +package com.baeldung.hibernate.operations; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javax.persistence.EntityManager; |
| 6 | +import javax.persistence.EntityManagerFactory; |
| 7 | +import javax.persistence.Persistence; |
| 8 | + |
| 9 | +import com.baeldung.hibernate.pojo.Movie; |
| 10 | + |
| 11 | +/** |
| 12 | + * |
| 13 | + *Class to illustrate the usage of EntityManager API. |
| 14 | + */ |
| 15 | +public class HibernateOperations { |
| 16 | + |
| 17 | + private static final EntityManagerFactory emf; |
| 18 | + |
| 19 | + /** |
| 20 | + * Static block for creating EntityManagerFactory. The Persistence class looks for META-INF/persistence.xml in the classpath. |
| 21 | + */ |
| 22 | + static { |
| 23 | + emf = Persistence.createEntityManagerFactory("com.baeldung.movie_catalog"); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Static method returning EntityManager. |
| 28 | + * @return EntityManager |
| 29 | + */ |
| 30 | + public static EntityManager getEntityManager() { |
| 31 | + return emf.createEntityManager(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Saves the movie entity into the database. Here we are using Application Managed EntityManager, hence should handle transactions by ourselves. |
| 36 | + */ |
| 37 | + public void saveMovie() { |
| 38 | + EntityManager em = HibernateOperations.getEntityManager(); |
| 39 | + em.getTransaction() |
| 40 | + .begin(); |
| 41 | + Movie movie = new Movie(); |
| 42 | + movie.setId(1L); |
| 43 | + movie.setMovieName("The Godfather"); |
| 44 | + movie.setReleaseYear(1972); |
| 45 | + movie.setLanguage("English"); |
| 46 | + em.persist(movie); |
| 47 | + em.getTransaction() |
| 48 | + .commit(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Method to illustrate the querying support in EntityManager when the result is a single object. |
| 53 | + * @return Movie |
| 54 | + */ |
| 55 | + public Movie queryForMovieById() { |
| 56 | + EntityManager em = HibernateOperations.getEntityManager(); |
| 57 | + Movie movie = (Movie) em.createQuery("SELECT movie from Movie movie where movie.id = ?1") |
| 58 | + .setParameter(1, new Long(1L)) |
| 59 | + .getSingleResult(); |
| 60 | + return movie; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Method to illustrate the querying support in EntityManager when the result is a list. |
| 65 | + * @return |
| 66 | + */ |
| 67 | + public List<?> queryForMovies() { |
| 68 | + EntityManager em = HibernateOperations.getEntityManager(); |
| 69 | + List<?> movies = em.createQuery("SELECT movie from Movie movie where movie.language = ?1") |
| 70 | + .setParameter(1, "English") |
| 71 | + .getResultList(); |
| 72 | + return movies; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Method to illustrate the usage of find() method. |
| 77 | + * @param movieId |
| 78 | + * @return Movie |
| 79 | + */ |
| 80 | + public Movie getMovie(Long movieId) { |
| 81 | + EntityManager em = HibernateOperations.getEntityManager(); |
| 82 | + Movie movie = em.find(Movie.class, new Long(movieId)); |
| 83 | + return movie; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Method to illustrate the usage of merge() function. |
| 88 | + */ |
| 89 | + public void mergeMovie() { |
| 90 | + EntityManager em = HibernateOperations.getEntityManager(); |
| 91 | + Movie movie = getMovie(1L); |
| 92 | + em.detach(movie); |
| 93 | + movie.setLanguage("Italian"); |
| 94 | + em.getTransaction() |
| 95 | + .begin(); |
| 96 | + em.merge(movie); |
| 97 | + em.getTransaction() |
| 98 | + .commit(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Method to illustrate the usage of remove() function. |
| 103 | + */ |
| 104 | + public void removeMovie() { |
| 105 | + EntityManager em = HibernateOperations.getEntityManager(); |
| 106 | + em.getTransaction() |
| 107 | + .begin(); |
| 108 | + Movie movie = em.find(Movie.class, new Long(1L)); |
| 109 | + em.remove(movie); |
| 110 | + em.getTransaction() |
| 111 | + .commit(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments