Java Academy · Lesson

JPA/H2 & Repositories

Use Spring Data JPA with an embedded H2 database and repositories.

Lesson 1 of 38 steps

JPA/H2 & Repositories is a free Java Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Intro

Spring Data JPA makes database access simple. Use it with the in-memory H2 database for quick demos.

Dependencies

Add spring-boot-starter-data-jpa and com.h2database:h2 to Maven/Gradle.

Code: Entity

An Entity represents a table row in the database.

import jakarta.persistence.*;@Entity
public class User {
  @Id @GeneratedValue
  private Long id;
  private String name;
  // getters and setters
  public Long getId() { return id; }
  public void setId(Long id) { this.id = id; }
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
}

Code: Repository

JpaRepository gives CRUD methods without writing SQL.

import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {}

Code: Controller

The controller uses the repository to fetch and save users.

import org.springframework.web.bind.annotation.*;import java.util.*;@RestController
@RequestMapping("/users")
public class UserController {
  private final UserRepository repo;
  public UserController(UserRepository repo) { this.repo = repo; }

  @GetMapping
  public List<User> all() { return repo.findAll(); }

  @PostMapping
  public User add(@RequestBody User u) { return repo.save(u); }
}

H2 Console

Enable spring.h2.console.enabled=true in application.properties to view data in the H2 console.

Repository check

Quick check: Which interface does Spring Data provide for basic CRUD operations?

Recap

Recap: Added dependencies, created an entity, repository, and controller, and tested with H2 console.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “JPA/H2 & Repositories” lesson free?

Yes — the full text of “JPA/H2 & Repositories” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “JPA/H2 & Repositories”?

Use Spring Data JPA with an embedded H2 database and repositories. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “JPA/H2 & Repositories” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. JPA/H2 & Repositories
  2. CRUD Service Layers
  3. Error Handling & Advice
← Back to Java Academy