Spring Data JPA & Database Access
Test your knowledge of Spring Data JPA: repositories, queries, transactions, and entity mapping.
Questions in this quiz
10 questions on Spring Data JPA & Database Access. Work through them here, then start the quiz to answer them and see your score.
1.What does extending JpaRepository<User, Long> give you?
- AA read-only repository with no write operations
- BCRUD methods, paging, sorting, and flush operations out of the box
- CA reactive repository for non-blocking database access
- DOnly save() and findById()
2.What does the method name findByEmailAndActiveTrue() generate?
- ASELECT * FROM users WHERE email = ? AND active = true
- BSELECT * FROM users WHERE email = ? OR active = true
- CA compile error — method names cannot contain "True"
- DSELECT * FROM users WHERE email = ? AND active IS NOT NULL
3.When is @Transactional required on a service method?
- AOnly when using stored procedures
- BWhen multiple database operations must succeed or fail together as a unit
- CWhenever you call a repository method
- DOnly for write operations — never for reads
4.What is the N+1 SELECT problem?
- AUsing more than one JOIN in a single query
- BQuerying the same table N times in a loop
- CFetching N parent entities then issuing N separate queries for their children
- DA query that returns N+1 rows instead of N
5.What does @Query(nativeQuery = true) do?
- AQueries the database without going through Hibernate
- BRuns the query in a separate thread
- CBypasses transaction management
- DExecutes a raw SQL query instead of JPQL
6.What does @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) do?
- ACreates a join table between the two entities
- BMaps a one-to-many relationship, cascades all operations to children, and deletes orphan children
- CCreates a foreign key from the child table to the parent
- DFetches children eagerly on every query
7.What is the difference between save() and saveAndFlush() in JpaRepository?
- AsaveAndFlush() validates the entity before saving
- BThey are identical in behaviour
- CsaveAndFlush() immediately synchronises changes to the database; save() defers until commit
- Dsave() is for inserts only; saveAndFlush() handles both insert and update
8.What does spring.jpa.hibernate.ddl-auto=validate do?
- ACreates missing tables but never drops existing ones
- BChecks that the database schema matches the entity mappings but makes no changes
- CDrops and recreates the schema on every startup
- DDisables schema validation completely
9.What is a "detached entity" in JPA?
- AAn entity whose lifecycle is no longer managed by the current persistence context
- BAn entity that has no primary key
- CAn entity that was never saved to the database
- DAn entity loaded from a different database
10.How do you implement pagination in Spring Data?
- APass a Pageable parameter and return Page<T> from a repository method
- BUse LIMIT and OFFSET in @Query
- CUse List<T> and call subList() on the result
- DAnnotate the method with @Pageable