JPA Basics 1

JPA overview

What is JPA?

JPA is an abbreviation for "Java Persistence (JSR 338)", ** Java EE standard O / R Mapping and DAO technology It is a specification ** (the original name was "Java Persistence API" and its abbreviation "JPA" is still commonly used, so this document also uses the name "JPA").

The version included in the current Java EE 7 is "2.1"

JPA is a technical specification included in Java EE's "Enterprise Application Technologies", but unlike Servlet / JSP and EJB, it does not adopt a container-based architecture, so it can also be used in Java SE **.

Typical implementation of JPA

The following are typical implementations of JPA.

"EclipseLink" is a JPA RI originally developed by Oracle and donated to eclipse.org. Also, "Hibernate ORM" is the original ORM Framework of the JPA specification, and it is common to use either of these two implementations.

"EclipseLink 2.5" and later and "Hibernate ORM 4.3" and later versions support JPA 2.1, but the current version "2.x" of "Apache Open JPA" only supports JPA 2.0 (JPA 2.1). Supported versions are currently under development).

JPA components

First, let's take a quick look at the main elements that make up JPA's technology.

● Entity Represents "** Persistence Object **". Specifically, there is a one-to-one correspondence with the database table. A Java class (Entity Class) and its "Entity Object". ● Entity Manager Provides lifecycle management and DAO functionality for Entity objects. ● Persistence Context Represents a "persistence context". ● Persistence Unit Generate a Persistence Context. ● Transaction Represents a "transaction". ● JPQL(Java Persistence Query Language) This is a JPA-only query language. It is a language very similar to SQL, and provides a mechanism to write type-safe queries by identifying the type of Entity.

JPA configuration file

JAP uses an XML format file called "** persistence.xml **" as shown below to describe basic settings such as connection information with the database.

persistence.xml


<?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="JPAExample">
	
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		
		<class>entity.Author</class>
		
		<properties>
			<property name="javax.persistence.jdbc.url"	value="jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=Asia/Tokyo" />
			<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.password" value="mysql" />
		</properties>
		
	</persistence-unit>

</persistence>

persistence It is the root element of the persistence.xml document and describes the JPA version, namespace reference, and so on.

persistence-unit It is a direct child element of persistence and represents a single persistence target. Specifically, it is the setting of the property related to the connection information to one database. This persistence-unit requires a description of the "name" attribute, and this name is used to uniquely identify the "persistence-unit".

provider It is a child element of persistence-unit and describes the JPA provider to use. The JPA provider is the "implementation of JPA", and in the above example, it is the description when using EclipseLink.

class Describes the Entity class that is a child element of persistence-unit and is managed by this unit.

properties Describes information related to database connections. Specifically, JDBC URI, user name, password, etc. You can also write implementation-specific configuration information.

Recommended Posts

JPA Basics 1
JPA Basics 2
Rails basics
Ruby basics
Ruby basics
Fragment basics
Docker basics
ViewPager basics
Java basics
Java basics
RSpec Basics
JavaScript basics
Hash basics
Java basics
Ruby basics
RecyclerView Basics
Rails CSV basics
Rails Routing Basics
Rails database basics
Rails Logger Basics
java programming basics
Basics of Ruby
JPA w / PostgreSQL
Java JAR basics
Object-oriented (Java) basics
vim (gitbush) basics
Regular expression basics
Java concurrency basics
Rspec Basics [Rails]
Stream API basics