-
Notifications
You must be signed in to change notification settings - Fork 4
Entity Manager API
The EntityManager API is great, but how does the server know which database it is supposed to save / update / query the entity objects? How do we configure the underlying object-relational-mapping engine and cache for better performance and trouble shooting? The persistence.xml file gives you complete flexibility to configure the EntityManager.
The persistence.xml file is a standard configuration file in JPA. It has to be included in the META-INF directory inside the JAR file that contains the entity beans. The persistence.xml file must define a persistence-unit with a unique name in the current scoped classloader.
The org.hibernate.annotations package contains some annotations which are offered by Hibernate, on top of the standard Jakarta Persistence annotations.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.7.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
</dependency>
<persistence-unit name="paypalcampus-repository">
<class>User</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:tcp://localhost/~/dataBaseH2/campusPayPal" />
<property name="javax.persistence.jdbc.user"
value="albert" />
<property name="javax.persistence.jdbc.password"
value="1234" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.show_sql"
value="true" />
<property name="hibernate.hbm2ddl.auto"
value="create" />
</properties>
</persistence-unit>
//create a manager to do all the CRUD operations with student object
//i can create manager cause I created JPAUtils
EntityManager manager = JPAUtils.getEntityManger();
//manager call Transaction, that is, it is a state to persist
EntityTransaction transaction = manager.getTransaction();
//let s start with begin the operations, thanks to transaction object
transaction.begin();
//Student studentCreated =new Student(1,"Alex", "Pixel", 25, "[email protected]");
Student studentCreated =new Student(2,"Ricard", "Jones", 15, "[email protected]");
//prepares the operation to be done
manager.persist(studentCreated);
//this operation WRITES the object on the actual table
transaction.commit();
manager.close();
JPA uses the EntityManager API for runtime usage. The EntityManager represents the application session or dialog with the database. Each request, or each client will use its own EntityManager to access the database. The EntityManager also represents a transaction context, and in a typical stateless model a new EntityManager is created for each transaction. In a stateful model, an EntityManager may match the lifecycle of a client's session.
The EntityManager provides an API for all required persistence operations. These include the following CRUD operations:
- persist (INSERT)
- merge (UPDATE)
- remove (DELETE)
- find (SELECT)
The EntityManager is an object-oriented API, so does not map directly onto database SQL or DML operations. For example to update an object, you just need to read the object and change its state through its set methods, and then call commit on the transaction . The EntityManager figures out which objects you changed and performs the correct updates to the database, there is no explicit update operation in JPA.
Entity manager operations. The table describes entity manager operations. The main operations that can be performed by an entity manager:
-
POJO (Plain Ordinary Java Object) : it can be understood as a simple entity class and used as auxiliary classes to support business logic.
-
JavaBean (Presentation Layer) : Java Bean is a specification/standard, that is, a Java object that contains a set of set and get methods:
- All properties are private (use getters/setters)
- A public no-argument constructor
- Implements Serializable.
-
Entity (Service layer) : An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.
-
DAO (data access layer) : The Data Access Object (DAO) pattern is a structural pattern that allows us to isolate the application/business layer from the persistence layer (usually a relational database) using an abstract API.
-
DTO (Data Transfer Object) : When POJO is transferred between systems, one way is to serialize the POJO and transfer it. The POJO in this transfer state is the DTO.
by Java Cifo 2022 IFCD53 Desenvolupament en Java amb framework Spring
by Java Cifo 2022 IFCD53 Desenvolupament en Java amb framework Spring