From 3fda2ec694b6337d15a02564d8d4feb7d522515b Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Thu, 14 Jul 2022 17:07:04 +0100 Subject: [PATCH] [JAVA-13294] Add missing code for article --- .../hibernate/operations/MovieService.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/operations/MovieService.java diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/operations/MovieService.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/operations/MovieService.java new file mode 100644 index 0000000000..eca5807dec --- /dev/null +++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/operations/MovieService.java @@ -0,0 +1,31 @@ +package com.baeldung.hibernate.operations; + +import com.baeldung.hibernate.pojo.Movie; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public class MovieService { + + @PersistenceContext + private EntityManager entityManager; + + public void saveMovie() { + entityManager.getTransaction() + .begin(); + + Movie movie = new Movie(); + movie.setId(1L); + movie.setMovieName("The Godfather"); + movie.setReleaseYear(1972); + movie.setLanguage("English"); + + entityManager.persist(movie); + entityManager.getTransaction() + .commit(); + } + + public Movie getMovie(Long movieId) { + return entityManager.find(Movie.class, movieId); + } +}