2
2
.
.
5
5
.
.
1
1
1
1
R
R
e
e
c
c
o
o
m
m
m
m
e
e
n
n
d
d
e
e
d
d
A
A
n
n
n
n
o
o
t
t
a
a
t
t
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
This tutorial show how to create Entity that uses all of the previously discussed functionalities
Entity - Private Properties (protect Properties by declaring them private)
Entity - @Component (Spring) (use @Component annotation so that Spring can use DI to Instantiate Class)
Entity - @Data (Lombok) (use @Data annotation so that Lombok can create helper methods)
Entity - @Entity (JPA) (use @Entity so that JPA can remind you to declare Primary Key with @Id)
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @RequestMapping (includes Tomcat Server)
Developer Tools
Lombok
Enables @Data (generates helper methods: setters, getters, ...)
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Some DB is needed to initialize JPA
PersonEntity
http://localhost:8080/Hello
Tomcat
Browser
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: entity_recommended (add Spring Boot Starters from the table)
Create Package: entities (inside main package)
Create Class: PersonEntity.java (inside package entities)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
PersonEntity.java
package com.ivoronline.springboot.entity_recommended.entitites;
import lombok.Data;
import org.springframework.stereotype.Component;
import javax.persistence.Entity;
import javax.persistence.Id;
@Data
@Entity
@Component
public class PersonEntity {
@Id
private Long id;
private String name;
private Integer age;
}
MyController.java
package com.ivoronline.springboot.entity_recommended.cntrollers;
import com.ivoronline.springboot.entity_recommended.entitites.PersonEntity;
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
PersonEntity personEntity;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
personEntity.setName("John");
String name = personEntity.getName();
return "Hello " + name;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure (Lombok generated methods are shown under Structure View)
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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>