Creating a database entity in Spring Boot
Tags: java, webdev, database, tristan
Published: 2022-01-04 00:32:12

#Introduction
- This series will be everything I have learned while building my website with spring boot. I want my website to be a tutorial based subscription service, where for a flat monthly fee people can sign up to get weekly text and video base tutorials on Java development.
- This tutorial in particular will be everything I have learned about data persistence in Spring boot.

### A quick word
- This tutorial will **NOT** include the basic set up of Spring boot and database integration. If you need to get your backend setup I would highly recommend this video, [HERE](https://www.youtube.com/watch?v=8SGI_XS5OPw).

### Definitions 
- Here are a few definitions which will be used frequently through out this tutorial.

`Object Relational Mapping (ORM)`: ORM is the automated persistence of Java objects in an application to a table in a relational database.


`Hibernate`: is an ORM tool that handles all the low level data transactions that are involved with converting a Java object in a relational database table. Hibernate implements the JPA specification

`Java Persistence API (JPA)`: is an official API specification released by oracle for persistence in Java. Meaning that any ORM tool, such as hibernate, can implement this specification and we the Java developer can immediately understand how to use the ORM tool. Without this specification we would have to learn a new API for each different ORM tool. 

### The Simple User Entity
- I think it will make more sense for us to work backwards, so I will show the finished entity first and then explain what it going on.
![User entity class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0myrqa46up8ce8w9i5a.png)

### Entity Metadata
- As you can see from the picture above, we are using a few annotations, but what exactly are they doing? Well in addition to the state of the object each entity will hold some metadata associated with it. This metadata exists in the saved class file and is not persisted in the database. This data allows the persistence layer (hibernate) to properly recognize, interpret and map the entity. So anytime you see annotations, know that we are configuring the metadata which is used to tell hibernate how to persist the object.

### @Entity and `@Id`
- in order to properly map a object to a table we need two annotations:

`@Entity`: this is a marker to hibernate stating this class is to be persisted to the database. Any fields inside an entity class will automatically be mapped to columns of the entity's table. Since user is a reserved keyword in postgres, we can use `@Entity(name="USERS")` to change the name of the table to `users`. 

`@Id`: indicates which field should be used for the primary key which is used for identifying the Entity. Marking a field with this annotation creates 3 rules: `1)` the value is never null, `2)` the value must be unique to the table, `3)` the value is immutable(never changed).
- A id annotation by itself means that we the developer have to worry about creating the id and verifying its uniqueness, which is a lot of work and highly error prone. That is why it is highly recommended that we use the annotation `@GeneratedValue`.

`@GeneratedValue`: marking a id field will tell hibernate to pick the appropriate strategy for creating unique identifier values.

### Primary key generation strategy
- with `@GeneratedValue` we can also specify the generation strategy and we get 4 strategies to choose from:

`1) strategy = GenerationType.AUTO`:  this is the default if no strategy is specified and means that we tell hibernate to pick the generation strategy itself.

`2) strategy = GenerationType.TABLE`: hibernate will use an extra table in our database to hold the next primary key value, one for each entity class. This table will be automatically updated and read accordingly.

`3) strategy = GenerationType.SEQUENCE`: hibernate will create a sequence(database mechanism used to create unique values). The sequence will be called separately before every INSERT, providing unique sequential numeric values.

`3) strategy = GenerationType.IDENTITY`: This will set the id data type to `bigserial` which is an auto incrementing numeric data type handled by postgres. If you are using a different database provider, you may have a different result.


###@Column 
- technically you do not need this annotation, what `@Column ` allows us to do, is to define extra metadata that hibernate can use to customize the field to column mapping. `unique = true` means that the column value must be unique in the table and `nullable = false` means this can no be left blank.


###Conclusion
- Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on [Twitter](https://twitter.com/TristAndroidDev).