An entity is the representation of information that we need in our application.
This entity could be a user, a product, or any data that our application needs to keep persistently to retrieve when needed.
What is an entity for SpringBoot
An entity is an object, element, or ‘thing’ with particular attributes that distinguish it. For example, it could be a ‘user’ of which we need to know its attributes such as names, age, email, etc.
How to define an entity in SpringBoot
Let’s think of a simple entity. For example, an entity to store the users of the application.
Pay attention to these points in this example of entity:
- You define the entity with an annotation @Entity
- It is necessary a primary key (PK) with @ID
- The value of this PK is generated automatically with this annotation @GeneratedValue with params AUTO
There are others optional annotations that you can use. For example, you can add:
- @Table to define explicitly the name of the table.
- @Column to define the name of the column.
If you don’t use these annotations (Table, Column), Spring will decide by convention those names.
How to use an entity in Spring Boot
Spring’s primary use entities to recognize wish classes represents the ‘model’ of data. With this, Spring can perform the usual action with the database, such as insert, delete, search, etc.
One to Many and Many to One Entity Relation
An entity may have one or more than one relationship with another entity.
Let’s suppose we have as the entity ‘User’ and the entity ‘Task’ in own application. A User can perform ‘one or many’ Task
SQL CREATE TABLE Statement for one-to-many / manu-to-one Relation
How to define a ‘one to many’ between two entities?
In the first entity, you have to create a ‘list of Tasks’ and annotate it with @OneToMany.
Using mappedBy we indicate that the relationship is unidirectional. In other words, a ‘User’ has many ‘Tasks’, but one task will not have many users.
How to define a ‘many to one’ between two entities?
In the second entity, you have to create a reference to ‘User’ and annotate with @ManyToOne
@JoinColumn refers to the column that will establish the relationship with the user.
One to One Entity Relation
In a One-to-One relationship we have an attribute on one entity that references another entity.
A one-to-many or many-to-one relationship is not possible here.
We will have an object that contains a reference to another object.
SQL CREATE TABLE Statement for one-to-one Relation
In the first entity ‘User’ you have to declare an ‘UserDetail’ attribute.
In the second entity ‘UserDetail’, you declare which will be the column that will relate it to the ‘User’.
Note, you have to indicate the column like ‘unique’.
With that, you don’t have more than one ‘UserDetail’ per User. To constrain 1-1 relationship between tables and do not allow more than UserDetail record per User.
- @OneToMany. Define a one-to-many relationship.
- mappedBy. Indicate which entity owns the one-to-many relationship uniquely.
- @ManyToOne. You use it to define the many-to-one relationship.
- @JoinColumn. To define the column that will be used as a key to achieving the relationship to the main entity.
- @OneToOne. Define a one-to-one relationship.
- unique = true. To create unique constrain
We learned how to create entities and how to map two entities to each other using one-to-many, many-to-one, one-to-one relationships.
Thanks for your feedback :)