2
2
.
.
5
5
.
.
6
6
@
@
I
I
d
d
C
C
l
l
a
a
s
s
s
s
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
In this tutorial we will use JPA's @IdClass Annotation to create Composite Primary Key from name and age.
@IdClass keeps all Properties at the same level so that we can use @RequestBody to map JSON Properties from Request.
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
O
O
v
v
e
e
r
r
v
v
i
i
e
e
w
w
We will create PersonId Class to hold Properties from which Composite Primary Key will be created.
Inside PersonEntity we will use @IdClass(PersonId.class) to specify that PersonId Class should be used as Id.
inside PersonRepository we also need to specify that PersonId Class should be used as Id.
PersonId.java
public class PersonId implements Serializable {
private String name;
private Integer age;
PersonEntity.java
@Entity
@IdClass(PersonId.class)
public class PersonEntity {
@Id public String name;
@Id public Integer age;
PersonRepository.java
public interface PersonRepository extends CrudRepository<PersonEntity, PersonId> { }
MyController
http://localhost:8080/AddPersons
addPersons()
PersonEntity
PersonId
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 File: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
– Create Class: PersonId.java (inside package entities)
– 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
PersonId.java
package com.ivoronline.springboot_primarykey_idclass.entities;
import java.io.Serializable;
public class PersonId implements Serializable {
//COMPOSITE PRIMARY KEY
private String name;
private Integer age;
//REQUIERED NO ARGS CONSTRUCTOR
public PersonId() {}
//CONSTRUCTOR FOR findById(BookId)
public PersonId(String name, Integer age) {
this.name = name;
this.age = age;
}
}
PersonEntity.java
package com.ivoronline.springboot_primarykey_idclass.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(PersonId.class)
public class PersonEntity {
//COMPOSITE PRIMARY KEY
@Id public String name;
@Id public Integer age;
//OTHER PROPERTIES
public String book;
}
PersonRepository.java
package com.ivoronline.springboot_primarykey_idclass.respositories;
import com.ivoronline.springboot_primarykey_idclass.entities.PersonEntity;
import com.ivoronline.springboot_primarykey_idclass.entities.PersonId;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, PersonId> { }
MyController.java
package com.ivoronline.springboot_primarykey_idclass.controllers;
import com.ivoronline.springboot_primarykey_idclass.entities.PersonEntity;
import com.ivoronline.springboot_primarykey_idclass.entities.PersonId;
import com.ivoronline.springboot_primarykey_idclass.respositories.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;
//===================================================================
// ADD PERSONS
//===================================================================
@ResponseBody
@RequestMapping("/AddPersons")
public String addPersons() {
//CREATE BOOK1
PersonEntity personEntity1 = new PersonEntity();
personEntity1.name = "John";
personEntity1.age = 20;
personEntity1.book = "Book about dogs";
//CREATE BOOK2
PersonEntity personEntity2 = new PersonEntity();
personEntity2.name = "John";
personEntity2.age = 50;
personEntity2.book = "Book about cats";
//STORE BOOKS
personRepository.save(personEntity1);
personRepository.save(personEntity2);
//RETURN SOMETHING TO BROWSER
return "Persons added to DB";
}
//===================================================================
// GET PERSON
//===================================================================
@ResponseBody
@RequestMapping("/GetPerson")
public PersonEntity getPerson() {
PersonEntity personEntity = personRepository.findById(new PersonId("John", 20)).get();
return personEntity;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddPersons
http://localhost:8080/GetPerson
Open H2 Console: http://localhost:8080/h2-console - Connect - PERSON_ENTITY - Run
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>