- Java EE 8 and Angular
- Prashant Padmanabhan
- 307字
- 2021-07-02 19:22:35
Java Persistence API (JPA)
Before we start wielding JPA and CDI together, let’s get the basics out of the way for JPA. The Java Persistence API allows for the modelling of the domain objects for accessing, persisting, and managing data between POJOs and a relational database. JPA is the standard for working with object-relational mapping (ORM) solutions. Popular ORM solutions include Hibernate, EclipseLink (the reference implementation for all JPA versions), Apache OpenJPA, and DataNucleus. JPA puts the focus back on the domain model for retrieving and persisting data without having to deal with resource management and vendor specific SQL.
Most developers would be accustomed to hearing about Hibernate in articles and projects; it also shows up as a skill sought by employers. While Hibernate and the like can be used directly, using JPA helps us avoid falling in the vendor lock-in pit and in turn maintains portability. Hibernate is bundled as the default ORM for RedHat/JBoss servers, while EclipseLink (RI) is bundled as part of Glassfish and Payara. Similarly, Apache OpenJPA is used in TomEE.
JPA has been tagged as a maintenance release, which has led to minor but noteworthy updates in JPA 2.2. This release brings with it support for Java 8 and better CDI integration.
It all starts with an EntityManager. To work with JPA, you must obtain an EntityManager instance, which acts as the gateway to perform any database operation. The steps are outlined here:
- Create persistence.xml and define one or more persistence-unit in it:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="taskDb" transaction-type="JTA">
<jta-data-source>jdbc/AppDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
- Obtain an EntityManager in a non-managed environment (using CDI):
@Produces public EntityManager create() {
return Persistence.createEntityManagerFactory("taskDb")
.createEntityManager();
}
public void close(@Disposes EntityManager em) {
em.close();
}
- Obtaining an EntityManager in a managed environment can be done like so:
@PersistenceContext(unitName="taskDb") EntityManager em;
- Use the EntityManager instance within a transaction to perform the operations on an entity.
- Facebook Application Development with Graph API Cookbook
- 摩登創客:與智能手機和平板電腦共舞
- vSphere High Performance Cookbook
- Java EE框架整合開發入門到實戰:Spring+Spring MVC+MyBatis(微課版)
- 深入淺出WPF
- Python語言程序設計
- Learning Flask Framework
- Visual Basic程序設計教程
- 零基礎學MQL:基于EA的自動化交易編程
- Java EE 7 Development with NetBeans 8
- C++ 從入門到項目實踐(超值版)
- Keras深度學習實戰
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- JavaScript腳本特效編程給力起飛
- 深入理解C指針