2
2
.
.
5
5
.
.
4
4
@
@
E
E
n
n
t
t
i
i
t
t
y
y
I
I
n
n
f
f
o
o
In this tutorial we will use JPA's @Entity Annotation to indicate that instances of this Class should be stored in DB.
Without this Hibernate will not be able to pick up this Class and do its magic with it by automatically creating Tables,
Columns and then taking care od persistent its instances in selected DB.
Classes annotated with @Entity must have Property that is declared as Primary Key by using @Id Annotation.
As reminder: JPA API is interface that defines @Entity and Hibernate API is actual implementation that stores Entity in DB.
For each @Entity Class Hibernate creates DB Table
that has the same name as the name of the Class
that has Columns with the same names as Class Properties
When you include JPA Spring Boot Starter you also need to select some DB Spring Boot Starter: H2, MySQL, PostgreSQL.
Otherwise Spring Boot will not be able to initialize JPA and your Application would not run.
This tutorial includes H2 in-memory DB for which no additional installation and configuration is needed.
JPA Annotations can only be used on SQL DBs (H2, MySQL, Oracle) and can't be used on NoSQL DBs (MongoDB).
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat Server
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 DB
MyController
http://localhost:8080/addPerson
addPerson()
PersonEntity
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: entity_entity (add Spring Boot Starters from the table)
Edit: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
Create Class: PersonEntity.java (inside package entities)
Create Package: repositories (inside main package)
Create Interface: PersonRepository.java (inside package repositories)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
application.properties
# H2 DATABASE
spring.datasource.url = jdbc:h2:mem:testdb
spring.h2.console.enabled = true
PersonEntity.java
package com.ivoronline.entity_entity.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class PersonEntity {
@Id
public Integer id;
public String name;
public Integer age;
}
PersonRepository.java
package com.ivoronline.entity_entity.repositories;
import com.ivoronline.entity_entity.entities.PersonEntity;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, Integer> { }
MyController.java
package com.ivoronline.entity_entity.controllers;
import com.ivoronline.entity_entity.entities.PersonEntity;
import com.ivoronline.entity_entity.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired
PersonRepository personRepository;
@ResponseBody
@RequestMapping("/addPerson")
public String addPerson() {
//CREATE ENTITY OBJECT
PersonEntity personEntity = new PersonEntity();
personEntity.id = 1;
personEntity.name = "John";
personEntity.age = 20;
//STORE ENTITY OBJECT INTO DB
personRepository.save(personEntity);
//RETURN SOMETHING TO BROWSER
return personEntity.name + " was stored into DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/addPerson
http://localhost:8080/h2-console - Connect - PERSON_ENTITY - Run (H2 Console)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>