EntityManager 執行個體化方法

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   java   strong   ar   

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 that the Movie entity is listed via a <class> element. This is not required,

but can help when testing or when the Movie class is located in a different jar than the jar containing the persistence.xml file.

persistence.xml 和 Entity在同一個jar包下的話,則不需要標註class元素。

 

Injection via @PersistenceContext

The EntityManager itself is created by the container using the information in the persistence.xml, so to use it at runtime, we simply need to request it be injected into one of our components. We do this via @PersistenceContext

The @PersistenceContext annotation 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 use a UserTransaction begin 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 particular EntityManager is injected as an EXTENDED persistence context, which simply means that the EntityManager is created when the @Statefulbean is created and destroyed when the @Stateful bean is destroyed. Simply put, the data in the EntityManager is cached for the lifetime of the @Statefulbean.

The use of EXTENDED persistence contexts is only available to @Stateful beans. 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 執行個體化方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.