0% found this document useful (0 votes)
618 views

Data With JPA - Spring Data and JPA Cheatsheet - Codecademy

Spring Data JPA provides Java Persistence API integration for Spring. It allows defining entities through JPA annotations, generating repositories, and executing common data access operations like save, find, and delete. Repositories provide CRUD operations and can be injected into services. The H2 console provides a way to view the embedded H2 database used by Spring Boot apps.

Uploaded by

IliasAhmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
618 views

Data With JPA - Spring Data and JPA Cheatsheet - Codecademy

Spring Data JPA provides Java Persistence API integration for Spring. It allows defining entities through JPA annotations, generating repositories, and executing common data access operations like save, find, and delete. Repositories provide CRUD operations and can be injected into services. The H2 console provides a way to view the embedded H2 database used by Spring Boot apps.

Uploaded by

IliasAhmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Data with JPA

Spring Data and JPA


Spring Java Data Model
A data model is the set of objects that represents the
concepts in your problem domain, whose data you will
want to ultimately store in a database.
For example in a music playlist application, the data
model would consist of artists, albums, and tracks, and
the possible relations between them.

Spring JPA Entity


Applying the @Entity annotation to a class with JPA
declares that the class definition will correspond to a
database table with a similar name.

Spring JPA @Id


The @Id annotation can be applied to a member of a
class to designate that this member will uniquely identify @Entity
the entity in the database. @Table(name="Library")
public class Book {
  @Id
  private Integer id;

  @Column(name="Title")
  private String Title;
}

Spring JPA @GeneratedValue


The @GeneratedValue annotation can be used with
parameters alongside @Id to designate how an entity’s @Entity
unique ID value will be generated. If no parameters are @Table(name="TELEVISIONS")
provided, the ID will be generated according to the public class Television {
default algorithm used by the underlying database.
  @Id
  @GeneratedValue(strategy=GenerationType.
IDENTITY)
  private Integer id;

  @Column(name="Brand")
  private String brand;
}
Spring JPA Repository
With Spring Data JPA, the developer creates a data
repository by writing a repository interface and adding import
custom finder methods. Spring provides the org.springframework.data.repository.CrudRe
implementation automatically. pository;
import
com.codecademy.people.entities.Person;

public interface PersonRepository extends


CrudRepository<Person, Integer> {}

Spring JPA Repository Injection


With Spring Data JPA, listing repositories as properties
allows them to be made available to other classes. This import
functionality is facilitated by the repository instance being org.springframework.web.bind.annotation.Re
injected into the class at runtime. stController;
import
com.codecademy.people.repositories.PersonR
epository;

@RestController
public class PersonController {
  private final PersonRepository
personRepository;

  public PersonController(final
PersonRepository personRepository) {
    this.personRepository
= personRepository;
}

Spring JPA Save Method


The save method of the CrudRepository can be used to
create or update an entity in the database. It returns the @PostMapping("/people")
newly-saved / updated entity. public Person createNewPerson(@RequestBody
Person person) {
  Person newPerson
= this.personRepository.save(person);
  return newPerson;
}
Spring JPA FindAll Method
The findAll method of the CrudRepository returns an
iterable collection of entities from the database. @GetMapping("/people")
public Iterable<Person> getAllPeople() {
  return this.personRepository.findAll();
}

Spring JPA Delete Method


The delete method of the CrudRepository removes a
given entity. @DeleteMapping("/people/{id}")
public Person
deletePerson(@PathVariable("id") Integer
id) {
  Optional<Person> personToDeleteOptional
= this.personRepository.findById(id);
  if (!personToDeleteOptional.isPresent())
{
    return null;
  }
  Person personToDelete
= personToDeleteOptional.get();
  this.personRepository.delete(personToDel
ete);
  return personToDelete;
}

Spring JPA FindByID Method


The findById method of the CrudRepository retrieves
an entity by its ID. @GetMapping("/people/{id}")
public Optional<Person>
getPersonById(@PathVariable("id") Integer
id) {
  return
this.personRepository.findById(id);
}

Spring JPA H2 Console


Spring Boot configures an H2 console that allows
developers to inspect the application’s database. The
console can be accessed via the browser at the URI /h2-
console .

You might also like