JPA/H2 & Repositories
Use Spring Data JPA with an embedded H2 database and repositories.
JPA/H2 & Repositories is a free Java Academy lesson on CoddyKit — 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
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, and the Java Academy course includes 3 lessons in total. 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.
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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
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
- JPA/H2 & Repositories
- CRUD Service Layers
- Error Handling & Advice