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

Entities - Part 1



The course is part of this learning path

Entities - Part 1

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.


Basic knowledge of Java programming.


Hello dear friends. In this video, we will examine entities in JPA, let's begin. 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. In general, entity is a group of states associated together in a single unit. On adding behavior, an entity behaves as an object and becomes a major constituent of object-oriented paradigm. So, an entity is an application to find object in Java persistence library. The persistent state of an entity is represented through either persistent fields or persistent properties. These fields or properties use object relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store. A portal must follow some requirements to be an entity. These requirements are: the class must be annotated with the JavaX persistence entity annotation. The class must have a public or protected, no argument constructor.

The class may have other constructors. The class must not be declared final, no methods or persistent instance variables must be declared final. If an entity instance is passed by value as a detached object such as through a session beans remote business interface, the class must implement the serializable interface. Entities may extend both entity and non entity classes, and non entity classes may extend entity classes. Persistence instance variables must be declared private, protected, or package-private and can be accessed directly only by the entity classes methods. Clients must access the entity state through accessor or business methods. The persistent state of an entity can be accessed through either the entities instance variables or properties. If the entity class uses persistent fields, the persistence runtime accesses entity class instance variables directly. All fields not annotated JavaX Persistence transient or not marked as java transient will be persisted to the data store.

The object relational mapping annotations must be applied to the instance variables. I think this covers the definition of an entity. Let's now examine entity properties with annotations on code snips. As you can see on this example, we have an entity named Class1. Look we have an entity annotation. In entity, there are four fields as id, name, job, and age. Take note of the id annotation id. This indicates that this table's primary key is id. If we try to create a table with this entity it will look like this. Notice how the property field names in our code correspond to the names of our table columns. Also notice the class name also corresponds to the name of our table. If you would like to change a column's name, use the column's annotation for each field. In columns annotation we will use the name property. If we change the entity property values, our table will look like this. Look our table column names have been changed to match the new property field names. If you want to determine table name, you also have to use table annotation with name property. If we change entity like this, our table will be employee entity like this.

Now, let's talk about Id. As you know, primary key must be unique and the value must be filled by the program. For this, we have to use generated value annotation. In generated value annotation with strategy, we can set identity. We only need to add code like this. Now let's talk about columns annotation details. The field type gives us the tables content type. For example, in this entity employee id and age are integer, name and jobs are string. So, for some columns, we need to adjust length like Max 100 characters. For this, we can use length property of columns annotation. If we change entity like this, now our name area length will be max 100 characters and the job columns will be Max 60 characters. Now, in column sanitation, there are many properties like nullable. For example, in this entity you didn't have all the employees addresses, so you want to make this columns nullable but you need all employees name and the name area cannot be null. To adjust this, we will use nullable. If we change entity like this, the name area will be mandatory. The default value of nullable is true.

This means if you didn't add nullable in any field, the column will be nullable. For example, in your company, each employee has unique employee numbers, to make this, you can use unique. If we add unique true for company number, this will be enough for us. Of course in each table you may need date or time information. Now, for example, we have three date values in our entity like BirthDate, employee AddDate, and ProcessTime. Now, for BirthDate, we need only date value for add AddDate. We need date and time together, and for time, we need only time value. To adjust this, we need temporal annotation. With temporal annotation, we can adjust type of data with TemporalType property. Now in each table, we may need same areas like AddDate, AddUserId or LastUpdateDate. 

Now, we don't need to add these three columns for each table. If you want, we can merge any other entity with this entity. For example, we have a portal like this. Look, there is no entity annotation. With embeddable annotation, we can use this class in each entity. We will add embeddable annotation like this. Now in our entity, we can use this, it's very easy. Only we will define class instance in entity. Now, to merge this, we have to use embedded annotation over the instances like this. Now if we create table with this entity, our table will be like this and as you can see, we have all columns inside entity and the other class. Let's take a break here. In the following video, we will continue examining entities. See you in the following video.


About the Author
Learning Paths

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