Book entity class with fields id, title, and authorBookRepositoryJump into concepts and practice - no test required
Book entity class with fields id, title, and authorBookRepositoryBook in the package com.example.library. Add private fields Long id, String title, and String author. Annotate the class with @Entity and the id field with @Id and @GeneratedValue.Use @Entity on the class and @Id with @GeneratedValue on the id field to let JPA know this is a database entity.
BookRepository in the package com.example.library. Make it extend JpaRepository<Book, Long> to enable basic CRUD operations.Extend JpaRepository with the entity Book and primary key type Long to get database methods automatically.
@Service class called LibraryService in package com.example.library, inject BookRepository and create a method getAllBooks() that returns List<Book> by calling bookRepository.findAll().Use constructor injection for BookRepository and call findAll() inside getAllBooks().
src/main/resources/application.properties: spring.datasource.url=jdbc:h2:mem:testdb, spring.datasource.driverClassName=org.h2.Driver, spring.jpa.database-platform=org.hibernate.dialect.H2Dialect, and spring.h2.console.enabled=true.These properties configure Spring Boot to use an in-memory H2 database and enable the H2 console for easy access.
Why is JPA important when working with databases in Spring Boot?
Which of the following is the correct way to declare a JPA entity class in Spring Boot?
?Given this Spring Data JPA repository interface:
public interface UserRepository extends JpaRepository<User, Long> {}What happens when you call userRepository.findAll()?
What is wrong with this JPA entity code snippet?
@Entity
public class Product {
@Id
private Long id;
private String name;
public Product(String name) {
this.name = name;
}
}You want to fetch all users whose name starts with 'A' using Spring Data JPA. Which repository method signature should you add?