Spring Boot Entities

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.

Entity

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.

Entity

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.

Entity

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.

Probably the most common use is with repositories. Please, review this article in order to understand about repositories.

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

Entity Relation

SQL CREATE TABLE Statement for one-to-many / manu-to-one Relation sql-table-one-to-many

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. Entity-one-to-many


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. Entity-many-to-one.png

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.

Entity-one-to-one

SQL CREATE TABLE Statement for one-to-one Relation sql-table-one-to-one

In the first entity ‘User’ you have to declare an ‘UserDetail’ attribute.

Entity

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.

Entity

In summary:

  • @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

Conclusion

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.

If you wish to send me any corrections or suggestions, please contact me.
Thanks for your feedback :)

See also