1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Working with Java Persistence APIs

Usage of Entities

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
35m
Students
2
Description

This course covers the Java Persistence API, or JPA, which is designed to persist data between Java objects and relational databases.

Learning Objectives

  • Learn the fundamentals of the Java Persistence API
  • Learn about entities, entity inheritance, and JPA queries

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.

Prerequisites

Basic knowledge of Java programming.

Transcript

Hi there, in this lesson, we will examine the usage of entities, let's begin. In the previous video, we learnt about entities, and now I'd like to show how to use them. First, we have to modify our persistence.xml file so we are able to connect to any database. Now this image shows us a connection for MySQL database. Before you add these codes, make sure that you have added the MySQL connector Java to your project. Let's take a look at these codes one by one. These configuration codes can be found on the Internet. You don't have to learn them. The persistence unit name is the name of your connection, which we will use in entity manager. Each entity in your project needs to be defined in the persistence unit area. If any entities are left out, you won't be able to create them or perform any operations on them.

You must first define your database type, which I have done for MySQL. The second property tag is where your database address is defined. Because I'm using my local machine, I have used my localhost, MySQL address, but you may use any database IP address. The other property tags are username and password, to help you connect your database. The last property is very important. My value is create or extends table. If you leave the value as drop and create, your database will be deleted and recreated for each operation, and you may not be able to access your old data. Now, the configuration is like this. Let's use entities, in web project or any main project. Let's examine this code snip for create tables. First, you need an entity manager factory from persistence, and you have to set your persistence unit name in this line. This is important. After that, you can create your entity manager from factory.

Following that, you must begin your transaction for operations. Try catch blocks are very important in JPA Operations. If there is a connection or other error, you must roll back your operations. So that in catch block we have used roll back. Now this will be enough creation. Now, let's look Insert Operation. To insert data into any table, you must first create an instance of the table entity class. After that, you must fill all necessary areas. After that, you will add it with persistence just like this code. After operation, you will commit your operation. If we run this code, we will add a new data to our employee table. For example, you may want to get employee with ID number, you need this code snip. With entity manager, find you have to set entity name and ID parameter. This code snip will return us the employee who has three ID numbers and you can see all details in the console.

To update, you first have to get data from database like this. After that, you have to set all updated value in entity. After operations end, you have to commit again. If you run this code, you will see two different ages on the console. Just like update, to delete any data, you first have to get the data from the database and like insert, you have to remove from entity manager. With commit, your data will be deleted, done. That's it for now. In the next video, we will examine queries in JPA. See you in the next video.

 

About the Author
Students
413
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics