Hibernate and JPA map Java classes to database tables, so you can save and read objects without writing every SQL statement by hand. Spring Boot projects use them constantly.
The idea
A class marked as an entity becomes a table. Each object becomes a row. Fields become columns. JPA is the standard, and Hibernate is the most common tool that implements it.
Key annotations
@Entity: marks the class as a table.@Id: marks the primary key.@OneToManyand@ManyToOne: describe relationships between tables.
The lazy-loading trap
Related data is often loaded only when you use it. Reading it outside a transaction can cause errors, and reading it in a loop can cause many extra queries. Check the SQL the application runs.
Practice
Map a Customer and an Order class to two tables, save one customer with three orders, and read them back. Print the SQL that runs.
Next step: see our Java Full Stack course at ScholarLogic, or book a free demo class. A counsellor will share the fee and the next batch dates.
