Spring JPA Annotations

Zehra Gökçe Aynacı
4 min readJul 24, 2023

--

As I believe we are struggling with learning the terms, a step-by-step approach is beneficial, given that we are having difficulty understanding them.

What is JPA?

JPA, which stands for Java Persistence API, is not a tool or framework on its own; instead, it provides a set of concepts and guidelines that directly implements how to manage relational data in Java applications. Metadata annotations are provided by JPA in order to define a mapping between objects and relational databases.

What is annotation?

Annotation is supplemental info about the program. Developers use annotations to inform JPA.

JPA Annotations

JPA annotations are used in mapping from Java objects to the database tables. Hibernate is the most popular ORM library that uses JPA specifications and provides some additional annotations. Annotations can be added to source code and allowed to be retained by the JVM at run-time.

SOME MAPPING ANNOTATIONS

Let’s take a look at some annotations with examples.

@ Entity

Specifies that the class is an entity. This annotation can be applied to Class, Interface of Enums.

@ Table

It is used to specify the table name. In the example above the data will be stored in the “users” table.

@ Column

This annotation is used to define the column name of the table.

@ Id

This annotation specifies the primary key of the entity. In the example below, we are understood that “prod_ofr_id” is the primary key.

@ OneToMany

When an entity is linked to many other entities, one-to-many relationships are used.

In the example below, the userId primary key from the User Entity Class is also the foreign key of the Customer Order Entity Class. So we can understand from the example that there can be more than one customer order for a user.

Using the one-to-many relationship, we indicate that one userId in the user class can have many customer orders from our other class called customer order. The list structure is used to express the multiplicity while specifying this.

@ ManyToOne

This annotation is used when various attributes in a table are associated with one attribute only.

More than one customer order can go to a single user. In the example below, after specifying the relationship of the Customer Order Entity Class with the User Entity Class, we specify where to connect it to the column by saying @ JoinColumn. Then we finish our definitions by creating an object from the class we want to combine.

@ OneToOne

When each record in one entity (table) is linked to exactly one record in another entity, and vice versa.

Each id (in our example the id is from the cust_ord_id Customer Order Entity Class) can only have one char value (in Customer Order Character Value Entity Class).

In summary, using this annotation, we connect the id (cust_ord_id) of the Customer Order Entity Class with the id of the Customer Order Char Value Entity Class using a one-to-one relation.

We see this in the example below:

@ ManyToMany

When multiple records in a table are associated with multiple records in another table, there is a many-to-many relationship.

In conclusion, Spring JPA annotations revolutionize Java’s database interaction, simplifying data access and boosting productivity. Stay ahead in modern development by embracing these powerful tools.

Looking forward to exploring more with you soon!

To follow me on Github:

To follow me on LinkedIn

--

--

Zehra Gökçe Aynacı
Zehra Gökçe Aynacı

Written by Zehra Gökçe Aynacı

Learning, growing, and chasing excellence—one challenge at a time.

No responses yet