Spring Data JPA Questions with Answers

28 practice questions on Spring Data JPA (Spring Boot & JPA) with answers and step-by-step solutions. Try 10 samples below, then practise all 28 free in the AptiNest app.

Sample questions

Question 1Easy

ORM stands for:

  1. Object Request Method
  2. Online Resource Manager
  3. Ordered Row Model
  4. Object-Relational Mapping

Answer: D. Object-Relational Mapping

Explanation

ORM maps Java objects to database tables.

Step-by-step solution

  1. ORM is the term for the technique that maps objects to database rows.
  2. The words behind the letters are Object-Relational Mapping.
  3. So ORM stands for option D.

Why the other options are wrong

  • A. Object Request Method is not a standard term.
  • B. Online Resource Manager is not what ORM means in databases.
  • C. Ordered Row Model is not a standard term.

Tip: ORM (Object-Relational Mapping) links Java objects with database tables; Hibernate is an ORM.

Question 2Easy

spring.jpa.show-sql=true is used to:

  1. Encrypt SQL queries
  2. Display tables in a web page
  3. Log executed SQL
  4. Create tables automatically

Answer: C. Log executed SQL

Explanation

Useful in development for spotting inefficient or N+1 queries.

Step-by-step solution

  1. Show-sql is a logging setting for JPA.
  2. It prints the SQL that Hibernate runs.
  3. It helps find slow or repeated queries in development.
  4. So it logs executed SQL.

Why the other options are wrong

  • A. The SQL is not encrypted.
  • B. Tables are not shown on a web page.
  • D. Table creation is controlled by ddl-auto.

Tip: Show-sql is for development; use proper logging in production.

Question 3Easy

Which repository method deletes an entity by its ID?

  1. erase(id)
  2. removeId(id)
  3. deleteById(id)
  4. dropById(id)

Answer: C. deleteById(id)

Explanation

CrudRepository provides deleteById.

Step-by-step solution

  1. CrudRepository offers methods for create, read, update and delete operations.
  2. Its method that deletes by ID is deleteById(id).
  3. So the answer is option C.

Why the other options are wrong

  • A. Erase is not a repository method.
  • B. RemoveId is not a repository method.
  • D. DropById is not a repository method; drop is a SQL word for removing tables.

Tip: Spring Data repositories use names such as deleteById, findById and existsById.

Question 4Easy

Which annotation maps a field to a column named full_name?

  1. @Field("full_name")
  2. @Column(name = "full_name")
  3. @Table(name = "full_name")
  4. @Name("full_name")

Answer: B. @Column(name = "full_name")

Explanation

@Column customises column mapping, including nullable and length.

Step-by-step solution

  1. A field is mapped to a column with @Column.
  2. The name attribute sets the column name.
  3. @Field, @Table and @Name are not the JPA column annotation.
  4. So the answer is @Column(name = "full_name").

Why the other options are wrong

  • A. @Field is not the JPA column annotation.
  • C. @Table names a table, not a column.
  • D. @Name is not a JPA column annotation.

Tip: @Column(name = ...) sets the column name; it also sets nullable and length.

Question 5Medium

Spring Data JPA projections (interface or DTO) are useful to:

  1. Disable lazy loading globally
  2. Fetch only the needed columns
  3. Create new tables
  4. Encrypt query results

Answer: B. Fetch only the needed columns

Explanation

Projections reduce data transfer and avoid loading full entities.

Step-by-step solution

  1. A projection returns only chosen fields.
  2. Interface or DTO projections select the columns needed.
  3. Less data is loaded and sent back.
  4. So projections fetch only the needed columns.

Why the other options are wrong

  • A. Projections do not turn off lazy loading globally.
  • C. Projections read data and do not create tables.
  • D. Projections do not encrypt results.

Tip: Use projections for read-only views that need few fields.

Question 6Medium

What does a method returning Page<Product> findByCategory(String c, Pageable p) provide beyond a List?

  1. Only the first row
  2. The SQL text
  3. Content plus totals
  4. A stream of all rows

Answer: C. Content plus totals

Explanation

Page runs an extra count query to report totals; Slice avoids that cost.

Step-by-step solution

  1. A List returns only the rows.
  2. A Page also returns the content for the page.
  3. It adds total counts, such as total elements and total pages.
  4. So a Page gives content plus totals.

Why the other options are wrong

  • A. A Page returns a page of rows, not just the first row.
  • B. A Page does not return the SQL text.
  • D. A Page is a page of data, not a stream of all rows.

Tip: Use Slice when you do not need totals and want to skip the count query.

Question 7Medium

What does spring.jpa.hibernate.ddl-auto=validate do?

  1. Drops and recreates tables each start
  2. Disables JPA completely
  3. Only checks the schema
  4. Updates tables to match entities

Answer: C. Only checks the schema

Explanation

validate fails startup if mappings and schema disagree; create, create-drop and update modify the schema.

Step-by-step solution

  1. Ddl-auto=validate checks the database schema against the entities.
  2. It does not create, drop or change tables.
  3. Startup fails if the schema and mappings disagree.
  4. So validate only checks the schema.

Why the other options are wrong

  • A. Drop and recreate is create or create-drop.
  • B. JPA is not disabled by this setting.
  • D. Updating tables is the update setting, not validate.

Tip: validate = check only; update = change the schema.

Question 8Medium

Which annotation stores an enum by its name rather than its ordinal?

  1. @Enumerated(EnumType.ORDINAL)
  2. @Enumerated(EnumType.STRING)
  3. @EnumName
  4. @Column(enum = true)

Answer: B. @Enumerated(EnumType.STRING)

Explanation

Ordinal storage breaks if enum constants are reordered; STRING is safer.

Step-by-step solution

  1. @Enumerated controls how an enum is stored.
  2. EnumType.STRING stores the constant's name.
  3. EnumType.ORDINAL stores its position, which breaks if the order changes.
  4. So the answer is @Enumerated(EnumType.STRING).

Why the other options are wrong

  • A. ORDINAL stores the position number, not the name.
  • C. @EnumName is not a JPA annotation.
  • D. @Column has no enum attribute for this.

Tip: Always use EnumType.STRING for enums in JPA.

Question 9Hard

A @Query method performs an UPDATE. Which extra annotation is required on it?

  1. @Modifying
  2. @Write
  3. @Update
  4. @Lock

Answer: A. @Modifying

Explanation

@Modifying tells Spring Data the query changes data; it must also run within a transaction.

Step-by-step solution

  1. A @Query that changes data is an UPDATE or DELETE.
  2. Spring Data needs to know it modifies data.
  3. @Modifying marks the query as a write.
  4. So the answer is @Modifying.

Why the other options are wrong

  • B. @Write is not a Spring Data annotation.
  • C. @Update is not a Spring Data annotation.
  • D. @Lock controls locking and does not mark a write.

Tip: Add @Modifying (and a transaction) to @Query methods that change data.

Question 10Hard

Inside a @Transactional method, you load an entity and change one field without calling save(). What happens at commit?

  1. An exception is thrown
  2. A duplicate row is inserted
  3. It is saved
  4. The change is lost

Answer: C. It is saved

Explanation

Managed entities are dirty-checked and updated automatically on flush.

Step-by-step solution

  1. Inside a transaction, the loaded entity is managed.
  2. A change to a managed entity is detected at flush or commit.
  3. Hibernate then issues the UPDATE automatically.
  4. So the change is saved at commit.

Why the other options are wrong

  • A. No exception is thrown for a normal change.
  • B. The entity is updated, not duplicated.
  • D. The change is not lost; dirty checking saves it.

Tip: Managed entities inside @Transactional are saved automatically on change.

Practise all 28 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Spring Boot & JPA topics · All topics