JPA/Hibernate: All About Entity Manager
??- ?????????????????????????? ?????????????????? ???? ??????
The EntityManager interface in Java Persistence API (JPA) is a key component that serves as the primary interface for interacting with the persistence context. It provides methods to manage the lifecycle of entity instances, similar to how a database connection would manage the lifecycle of database records. The main responsibilities of the EntityManager include:
1- ???????????????????? ????????????????:
You can use the persist() method to make a transient instance persistent.
2- ?????????????? ????????????????:
The find() method retrieves an entity based on its primary key.
3- ???????????????? ????????????????:
The remove() method is used to delete an entity from the database.
4- ???????????????? ????????????????????????:
The getTransaction() method (in the context of container-managed entity managers) enables you to commit or roll back transactions.
5- Querying:
The createQuery() method allows you to define and execute JPQL (Java Persistence Query Language) queries.
6- Loading States:
It provides functionality to manage entities in different states, like transient, managed, detached, and removed.
????- ???????????????? ???? ???????????? ???? ??????
In JPA, an entity is defined by creating a Java class and annotating it with the @Entity annotation. Here’s how you typically define an entity:
import javax.persistence.Entity;
import javax.persistence.Id ;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Constructors, getters, setters, etc.
}
??????- ?????????????????? ???????????? ???? ???? ???????????? ???? ??????
领英推荐
An entity can exist in one of several states during its lifecycle. The main states are:
1- ??????????????????:
The entity is created but not associated with any persistence context. It is not yet persisted to the database. For example, when you instantiate a new object of an entity class but haven't called entityManager.persist() on it.
Student student= new Student (); // Transient state
2- ??????????????:
The entity is associated with a persistence context, which means it is being tracked for changes. When you call persist() or find(), the entity becomes managed.
entityManager.persist(student); // student enters managed state
3- ????????????????:
The entity was once managed but is now disconnected from the persistence context. This can happen when the entity manager is closed or when detach() is called. Changes made to a detached entity are not automatically persisted to the database unless it is merged or reattached to a persistence context.
entityManager.detach(student); // Student enters detached state
4- ??????????????:
The entity is marked for deletion. When remove() is called on a managed entity, the entity moves to the removed state. However, it remains in the persistence context until the transaction is committed.
entityManager.remove(student); // Student enters removed state
????- ???????? ???? ?????? ?????????????? ???? ?????? @???????????? ?????????????????????
The @Entity annotation in Java is part of the Java Persistence API (JPA) and is used to designate a class as an entity that is to be persisted in a relational database. Here are the key purposes and aspects of the @Entity annotation:
?????????????? ???? ???????????????? ??????????: When you annotate a class with @Entity, it signifies that an instance of this class corresponds to a row in a database table. The class members (fields) are mapped to the columns of the table.
?????????????????????? ??????????????:
Entities are managed within a persistence context, which means that the lifecycle of the entity is tracked by the Entity Manager, and operations like saving, updating, or deleting can be performed.
?????????????????????????? ???? ???????????? ????????????????:
The @Entity annotation can be paired with various other annotations to customize the behavior of the entity, such as specifying the table name, primary keys, relationships with other entities, and constraints.
???????????????? ????????????????:
Each entity should have a unique identifier, typically annotated with @Id, which indicates the primary key of the table for that entity.
?????????????? ????????????????:
If no specific table name is provided, the default name of the database table is typically derived from the name of the entity class.
??- ????????????????????
Understanding the EntityManager interface, how to define entities, and the different states of these entities is fundamental in working with JPA, which facilitates the management of relational data in Java applications through an object-oriented approach.
NB: fi this content intersted you, here is my previous article: