Files associates with BAEL-2343
This commit is contained in:
parent
5648013c05
commit
138b22a7dd
|
@ -0,0 +1,114 @@
|
|||
package com.baeldung.hibernate.operations;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import com.baeldung.hibernate.pojo.Movie;
|
||||
|
||||
/**
|
||||
*
|
||||
*Class to illustrate the usage of EntityManager API.
|
||||
*/
|
||||
public class HibernateOperations {
|
||||
|
||||
private static final EntityManagerFactory emf;
|
||||
|
||||
/**
|
||||
* Static block for creating EntityManagerFactory. The Persistence class looks for META-INF/persistence.xml in the classpath.
|
||||
*/
|
||||
static {
|
||||
emf = Persistence.createEntityManagerFactory("com.baeldung.movie_catalog");
|
||||
}
|
||||
|
||||
/**
|
||||
* Static method returning EntityManager.
|
||||
* @return EntityManager
|
||||
*/
|
||||
public static EntityManager getEntityManager() {
|
||||
return emf.createEntityManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the movie entity into the database. Here we are using Application Managed EntityManager, hence should handle transactions by ourselves.
|
||||
*/
|
||||
public void saveMovie() {
|
||||
EntityManager em = HibernateOperations.getEntityManager();
|
||||
em.getTransaction()
|
||||
.begin();
|
||||
Movie movie = new Movie();
|
||||
movie.setId(1L);
|
||||
movie.setMovieName("The Godfather");
|
||||
movie.setReleaseYear(1972);
|
||||
movie.setLanguage("English");
|
||||
em.persist(movie);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to illustrate the querying support in EntityManager when the result is a single object.
|
||||
* @return Movie
|
||||
*/
|
||||
public Movie queryForMovieById() {
|
||||
EntityManager em = HibernateOperations.getEntityManager();
|
||||
Movie movie = (Movie) em.createQuery("SELECT movie from Movie movie where movie.id = ?1")
|
||||
.setParameter(1, new Long(1L))
|
||||
.getSingleResult();
|
||||
return movie;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to illustrate the querying support in EntityManager when the result is a list.
|
||||
* @return
|
||||
*/
|
||||
public List<?> queryForMovies() {
|
||||
EntityManager em = HibernateOperations.getEntityManager();
|
||||
List<?> movies = em.createQuery("SELECT movie from Movie movie where movie.language = ?1")
|
||||
.setParameter(1, "English")
|
||||
.getResultList();
|
||||
return movies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to illustrate the usage of find() method.
|
||||
* @param movieId
|
||||
* @return Movie
|
||||
*/
|
||||
public Movie getMovie(Long movieId) {
|
||||
EntityManager em = HibernateOperations.getEntityManager();
|
||||
Movie movie = em.find(Movie.class, new Long(movieId));
|
||||
return movie;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to illustrate the usage of merge() function.
|
||||
*/
|
||||
public void mergeMovie() {
|
||||
EntityManager em = HibernateOperations.getEntityManager();
|
||||
Movie movie = getMovie(1L);
|
||||
em.detach(movie);
|
||||
movie.setLanguage("Italian");
|
||||
em.getTransaction()
|
||||
.begin();
|
||||
em.merge(movie);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to illustrate the usage of remove() function.
|
||||
*/
|
||||
public void removeMovie() {
|
||||
EntityManager em = HibernateOperations.getEntityManager();
|
||||
em.getTransaction()
|
||||
.begin();
|
||||
Movie movie = em.find(Movie.class, new Long(1L));
|
||||
em.remove(movie);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.hibernate.pojo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "MOVIE")
|
||||
public class Movie {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String movieName;
|
||||
|
||||
private Integer releaseYear;
|
||||
|
||||
private String language;
|
||||
|
||||
public String getMovieName() {
|
||||
return movieName;
|
||||
}
|
||||
|
||||
public void setMovieName(String movieName) {
|
||||
this.movieName = movieName;
|
||||
}
|
||||
|
||||
public Integer getReleaseYear() {
|
||||
return releaseYear;
|
||||
}
|
||||
|
||||
public void setReleaseYear(Integer releaseYear) {
|
||||
this.releaseYear = releaseYear;
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
public void setLanguage(String language) {
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
|
||||
version="2.0">
|
||||
<persistence-unit name="com.baeldung.movie_catalog">
|
||||
<description>Hibernate EntityManager Demo</description>
|
||||
<class>com.baeldung.hibernate.pojo.Movie</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/moviecatalog"/>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.password" value="root"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
Loading…
Reference in New Issue