Java Data. Creating an entity with JPA and Hibernate
Tags: java, database, beginners, tristan
Published: 2022-02-11 23:15:54

###Introduction
- This series is going to be all things Java and data related. I just want to do some big things with big data in Java

###What is an entity?
- So before we can talk about an Entity we first need to talk about Java classes and database tables. We know that we have to turn the classes into tables but how? Well the technique that is used to do this is know as `Object Relational Mapping`ORM. It basically means that we are mapping our Java class to the database table.

![Java class to database table diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egvax0wu5vhho25ij1np.png)
 



###How do we use ORM?
- Well I can tell you that we are not going to code it ourselves, that would be very hard. However, what we can do is use a open source technology like `Hibernate`. Hibernate is a tool that aims to be the complete solution when it comes to persistence in Java, including object relational mapping

###How do we use Hibernate?
- We could implement it ourselves, however, if you are using Spring Boot like I am. The JPA dependency automatically adds it for us. Ok, but how do we talk to Hibernate? We do so through the `Java Persistence API`(JPA). JPA is simply specification(large interface) that persistence tools, like Hibernate implement. This is really cool because as long as the persistence tool implements the JPA specification, we can swap out any persistence tool we like and our code will still be the same. 
- Also, I want to point out that you will be seeing a lot of annotations, such as `@Entity ` and `@Id `. Those annotations come from the JPA specification.

###Entity Metadata
- The last thing we need to talk about before we start creating a entity, is metadata. Entity metadata is usually stored inside the `.class` file and is used to tell the persistence layer(hibernate) how to recognize, interpret and manage the entity. There are two ways that we can define entity metadata:

**1)Annotations**: such as, `@Entity ` and `@Id `(This is what we will use)

**2)XML file**: Yuck, we will not be using.


###Creating the Entity
- So to create an Entity we only need two annotations:

**1) @Entity :** tells the persistence layer(Hibernate) that implementations of this class are to be mapped to a database table

**2)  `@Id ` :** creates a unique persistence identifier(primary key in SQL) for the entity and it also signifies that all other fields in this class should be saved as a row in the database table. 

```

@Entity
public class User {


    @Id 
    private Long id;

    private String username;
    private String password;

    public User() {
    }

    public User(String username, String password){
        this.username = username;
        this.password = password;
    }
}
//ALL GETTERS AND SETTERS ARE BELOW

```
- I would like to point out the empty no args constructor for this class the `public User()`. This is done per the Hibernate documentation, which states: `All persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using Constructor.newInstance().`


###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/TristanJava).