JPA (b): Entitymanager

Source: Internet
Author: User

We know what JPA is, and the next article will focus on one of the important things about JPA: Entitymanager. Any action on the entity is done through it.

First of all, say what is Entitymanager. As the name implies, Entitymanager is the object responsible for managing entity. It is a mediator of the Java entity object's interaction with the database, which manages a set of corresponding entities, including CRUD operations for this set of entities. At the same time, Entitymanager, the entity Manager, is also responsible for interacting with the persistence context, which enables the conversion of entities to different States.

take a look at a more vivid image of the role of Entitymanager through a picture.


When you know the Entity manager, where does the entity manager come from? How do we create an entity manager?

before detailing the acquisition of Entitymanager objects, the reader should first clarify two concepts, namely the Java EE Environment and the J2SE environment.

One, the Java EE environment, including the EJB container and the Web container.

(1) Web container: A container that only runs Web apps, such as Tomcat, which is an open-source web container that can run JSPs, Servlets, and so on.

(2) EJB container: A container running in an EJB component that provides system-level services such as state management of EJB components, transaction management, thread management, remote Data resource access, connection management, and security management. For example, JBoss combines the EJB container and the Web container (the Web container is integrated with Tomcat).

the jar packages deployed in the EJB container can be thought of as running in the EJB container. But Web applications in JBoss, such as those in a war package, are not running in the EJB container, but are running in a Web container.

Second, J2SE Environment

The most common Java runtime environment, such as a HelloWorld Java program, is running in a J2SE environment, typically using the main entry method as a trigger for program initiation.

is the relationship between Java EE and the J2SE environment:


We can have the following two types of Entitymanager, depending on how the Entitymanager object is managed.

a container-managed (container-managed) Entitymanager Object

Container-managed Entitymanager objects are the simplest, and programmers do not need to consider the release of Entitymanager connections, as well as complex issues such as transactions, all of which are given to the container to manage. Container-managed Entitymanager objects must run in the EJB container, not in the Web container and the J2SE environment. By injecting @persistencecontext annotations to get Entitymanager objects, the way to get Entitymanager objects is container-managed.

ii. Application-managed (application-managed) Entitymanager objects

by applying managed Entitymanager objects, programmers need to manually control their release and connection, manually control transactions, and so on. However, this approach to the application of managed Entitymanager objects can be applied not only in the EJB container, but also from the EJB container and into any Java environment, such as the Web container, the J2SE environment, and so on. So in some ways, this is the basis for JPA to run independently of the EJB environment.

Ideally, it would be preferable to choose a container-managed Entitymanager object, but in a special environment, you would still need to use the managed Entitymanager object in the same way.

It is because of the connection release of the application managed Entitymanager object, the transaction control is more complex, so the related content is more involved in the use.

two different ways to get entitymanager, in fact the principle is still consistent. In the final analysis, it is created by Entitymanagerfactory. It is only in the container-managed entitymanager that the container creates the entitymanagerfactory for us, but in the Entitymanager managed by the app, we need to create the entitymanagerfactory manually.

as a factory in Entitymanager,entitymanagerfactory contains metadata information for the current o-r mappings, each entitymanagerfactory, Can be called a Persistence unit (persistenceunit), each persistence unit can be considered a mapping of a data source (so-called data source, can be understood as a database, we can configure multiple data sources in the application server, Using different persistenceunit to map these data sources makes it easy to implement transactional operations across multiple databases! )


say here, do not know reader friend to JPA of Entitymanager gain some understanding?


JPA (b): Entitymanager

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.