JPA Tutorials

JPA Tutorials

In this detailed Resource page, we feature an abundance of JPA Tutorials!

The Java Persistence API (JPA) is a Java application programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition.

Persistence in this context covers three areas:

  • the API itself, defined in the javax.persistence package
  • the Java Persistence Query Language (JPQL)
  • object/relational metadata

The reference implementation for JPA is EclipseLink.

A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. Instances of such an entity correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.

Note
If you wish to build up your JPA knowledge first, check out our JPA Tutorial – The ULTIMATE Guide.

JPA Tutorials – Getting Started

Simple examples based on the Java Persistence API

  • JPA persistence.xml Example
    In this standalone JPA example, we are using EclipseLink with MySQL Database. EclipseLink is a popular open source ORM (Object Relation Mapping) tool for Java platform used for mapping an entity to a traditional relational database like Oracle, MySQL etc.
  • JPA EntityManager Example
    In this example, we shall try to demonstrate how to use JPA EntityManager. As the name suggests, an EntityManager is a class that manages the state of the Entity(Persist/Update/Delete etc).
  • Single table inheritance in JPA
    In this example we shall show you how to create a single table inheritance in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Set column name and length
    In this example we shall show you how to set the column name and length in an entity field in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • JPA CriteriaBuilder Example
    The Criteria API is a predefined API used to define queries for entities. It is the alternative way of defining a JPQL query. These queries are type-safe, portable and easy to modify by changing the syntax i.e. the JPA queries are mainly used for building the dynamic queries whose exact structure is only known at the runtime. In this tutorial, we will show how to implement the JPA Criteria API with EclipseLink and MySQL in Java.
  • JPA CRUD Example
    This is an example of how to perform CRUD functionality in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.

JPA Tutorials – Functions

Learn the most famous functionalities and operations of the Java Persistence API

  • Persist object with JPA
    With this example we are going to demonstrate how to persist an object using the JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Find by id in JPA
    In this example we shall show you how to find an object by id in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Find by class in JPA
    This is an example of how to find an object by class in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Get single object in JPA
    With this example we are going to demonstrate how to get a single object in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Remove Entity with JPA
    This is an example of how to remove an entity using the JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Delete with condition in JPA
    With this example we are going to demonstrate how to delete with a condition in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Embedded compound primary key in JPA
    With this example we are going to demonstrate how to create an embedded compound primary key in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications. A composite primary key is usually made up of two or more primitive or JDK object types. Composite primary keys typically arise when mapping from legacy databases when the database key is comprised of several columns. You can specify such a composite primary key with a separate composite primary key class.
  • Map Java Enum type in JPA
    In this example we shall show you how to map a Java Enum type in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.

JPA Tutorials – Queries

Learn how to call queries with Java Persistence API

  • JPA mappedBy Example
    In this tutorial, we will show how to implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.
  • Named query with entity in JPA
    This is an example of how to create a named query with an entity in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications. A named query is a predefined query that you create and associate with a container-managed entity. The named queries are stored on the EntityManager. At run time, you can use the EntityManager to acquire, configure, and execute a named query. Here we create an entity with a named query and use in with EntityManager.
  • JPA Relationship Annotations Example
    This article takes you through the relationships between Entities. Generally the relations are more effective between tables in the database and as we already know that in RDBMS every entity(table) must be related to other entities to become meaningful. Here the entity classes are treated as relational tables (concept of JPA).
  • JPA Named Query Example
    In this article you will learn how to use the JPA @NamedQuery and @NamedNativeQuery annotations. We will start by introducing some general aspects about named queries, then creating simple examples, using both JPQL and native SQL queries, passing parameters to named queries and using vendor-specific query hints. You will then learn how to execute named queries and use optional elements to make use of vendor-specific query hints.
  • JPA SQL Stored Procedure Example
    Here we will discuss about the JPA SQL Stored Procedures, i.e. using database Stored Procedures from JPA (Java Persistance API) along with Cursors, Multiple Parameters (IN & OUT) etc. We have used EclipseLink for our project. However this can also be done with other JPA providers such as Hibernate, Toplink, Spring Data JPA, etc. JPA 2.1 supports calling database stored procedures using the StoredProcedureQuery and @NamedStoredProcedureQuery annotation or XML element.
  • JPA Native SQL Queries Example
    In this article we will explore the JPA bidirectional @ManyToMany association in a SQL and NoSQL fashion. A @ManyToMany association occurs when multiple records in one table, each correspond to multiple records in a related table. If the records from the second table have an inverse association back to the first table, we say that we have a bidirectional @ManyToMany association.
  • JPA One-to-One Example
    In this article we will explore the JPA @OneToOne association in a SQL and NoSQL fashion. A @OneToOne association occurs when there is exactly one record in a table that corresponds to exactly one record in a related table. In this case, both tables will contain the same number of records and each row of the first table is linked to another row in the second table.
  • JPA One-to-Many Example
    In this article we will explore the JPA @OneToMany and @ManyToOne associations in a SQL and NoSQL fashion. A @OneToMany association occurs when each record in one table corresponds to multiple records in a related table. If the records from the second table have an inverse association back to the first table, we say that we have a bidirectional @ManyToOne association.
  • JPA Many-to-Many association in SQL and NoSQL databases under Payara
    In this article we will explore the JPA bidirectional @ManyToMany association in a SQL and NoSQL fashion. A @ManyToMany association occurs when multiple records in one table, each correspond to multiple records in a related table. If the records from the second table have an inverse association back to the first table, we say that we have a bidirectional @ManyToMany association.
  • Many To One Mapping in JPA
    In this example we shall show you how to create a many to one mapping in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • One To Many unidirectional mapping in JPA
    This is an example of how to use One to Many unidirectional mapping in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • One To Many bidirectional mapping in JPA
    With this example we are going to demonstrate how to use One to Many bidirectional mapping in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • How to map a Composite Primary Key with JPA and Hibernate Example
    In this tutorial, we will show how to map the Composite Primary Key with JPA and Hibernate using Eclipse Link and MySQL in Java.

JPA Tutorials – Integrations

Learn how to use Java Persistence API with the most powerful 3rd party frameworks

  • Spring Data JPA Example
    The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. If we try to implement a data access layer of an application on our own, we will end up with lots of boilerplate code.
  • Spring Data JPARepository Example
    In this example, I will demonstrate how to use Spring Data JpaRepository to create, update, delete, and search contact data in a relational database.
  • Spring Boot and JPA Example
    This example is very straight forward and simple, it should be easy to understand. Let’s begin with our Spring Boot JPA example.
  • JSF JPA CRUD Tutorial
    This example will show the implementation of basic CRUD (Create, Read, Update, Delete) operations and lets you manage the school’s database in jsf using JPA with EclipseLink & MySQL.
  • Batoo JPA – 15x Faster Than The Leading JPA Provider
    Batoo JPA deviates from the specification almost zero, making it easy for existing JPA applications be migrated to Batoo JPA, while requiring no additional learning phase to start using it.
  • JBoss 4.2.x Spring 3 JPA Hibernate Tutorial
    In this tutorial we will talk about how to integrate Spring with JPA and Hibernate.
  • JBoss 4.2.x Spring 3 JPA Hibernate Tutorial Part #2
    In this tutorial we will demonstrate how to create a Spring service to expose functionality to the end user.
  • GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial
    This step by step guide will present how to develop a simple web application using Google’s Web Toolkit (GWT) for the rich client and Spring as the back – end, server side framework.
  • GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial – Eclipse and Maven 2 showcase
    This step by step guide will present how to develop a simple web application using Google’s Web Toolkit (GWT) for the rich client and Spring as the back – end, server side framework. The sample web application will provide functionality to make CRUD (Create Retrieve Update Delete) operations to a database. For the data access layer we will use JPA over Hibernate and for a database we will use Hypersonic.
  • Spring 3 RESTful Web Services
    We are going to use our GWTSpringInfinispanHornetQ project and expose our “employeeService” CRUD functionality as a RESTful Web service. Of course you can follow this article just to be informed on how to expose your Spring services as RESTful Web services.
  • Spring Data JPA Example with Spring Boot
    In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database for this lesson. Spring Data JPA offers a set of very powerful and highly-abstracted interfaces which are used to interact with any underlying database.
  • Spring Data JPA Tutorial
    Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of boilerplate code that should be simplified in order to reduce the number of lines of code and make the code reusable. In this tutorial, we will discuss spring data’s implementation of JPA.
  • Spring Data JPA with central exception handling and VO validations – framework
    In this article let’s see how to write a Spring based RESTful API’s using a full stack spring technology; Spring Boot, Spring Validations and Spring data JPA with an example.
  • Hibernate JPA DAO Example
    This is an example of how to create Data Access Objects (DAOs), making use of the Hibernate implementation for the Java Persistence API (JPA) specification.

[undereg]

Back to top button