Entitymanager instantiation Method

Source: Internet
Author: User
Configure the entitymanager via a persistence. xml file
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">  <persistence-unit name="movie-unit">    <jta-data-source>movieDatabase</jta-data-source>    <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>    <class>org.superbiz.injection.jpa.Movie</class>    <properties>      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>    </properties>  </persistence-unit></persistence>

Notice thatMovieEntity is listed via<class>Element. This is not required,

But can help when testing or whenMovieClass is located in a different jar than the jar containingpersistence.xmlFile.

If persistence. xml and entity are in the same jar package, class elements are not required.

 

Injection via @ persistencecontext

TheEntityManagerItself is created by the container using the information inpersistence.xml, So to use it at runtime, we simply need to request it be injected into one of our components. We do this@PersistenceContext

The@PersistenceContextAnnotation can be used on any CDI bean, EJB, Servlet, Servlet listener, Servlet filter, or JSF managedbean. If you don't use an EJB you will need to useUserTransactionBegin and commit transactions manually. A transaction is required for any of the CREATE, update or delete methods of the entitymanager to work.

 

package org.superbiz.injection.jpa;import javax.ejb.Stateful;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.PersistenceContextType;import javax.persistence.Query;import java.util.List;@Statefulpublic class Movies {    @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)    private EntityManager entityManager;    public void addMovie(Movie movie) throws Exception {        entityManager.persist(movie);    }    public void deleteMovie(Movie movie) throws Exception {        entityManager.remove(movie);    }    public List<Movie> getMovies() throws Exception {        Query query = entityManager.createQuery("SELECT m from Movie as m");        return query.getResultList();    }}

This participatesEntityManagerIs injected asEXTENDEDPersistence context, which simply means thatEntityManagerIs created when@StatefulBean is created and destroyed when@StatefulBean is destroyed. Simply put, the data inEntityManagerIs cached for the lifetime of@StatefulBean.

The useEXTENDEDPersistence contexts isOnlyAvailable@StatefulBeans. See the JPA concepts page for an high level explanation of what a "persistence context" really is and how it is significant to JPA.

Entitymanager instantiation Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.