JPA Relationships & Fetching Questions with Answers

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

Sample questions

Question 1Medium

orphanRemoval = true on a @OneToMany collection means:

  1. Children are never deleted
  2. The parent is deleted when a child is removed
  3. Removed children get deleted
  4. Only new children are saved

Answer: C. Removed children get deleted

Explanation

Removing a child from the collection triggers a DELETE for it.

Step-by-step solution

  1. orphanRemoval = true treats a child removed from the parent's collection as an orphan.
  2. Hibernate then issues a DELETE for that removed child.
  3. So removed children get deleted, option C.

Why the other options are wrong

  • A. Children are not kept forever; removing one from the collection deletes it.
  • B. Removing a child does not delete the parent.
  • D. Saving only new children describes cascade behaviour, not orphanRemoval.

Tip: OrphanRemoval deletes children that are dropped from the collection; cascade passes operations such as save or delete to children.

Question 2Medium

In a bidirectional @OneToMany(mappedBy = "customer") / @ManyToOne relationship, which side owns it?

  1. The @ManyToOne side
  2. Both sides equally
  3. Neither
  4. The @OneToMany side

Answer: A. The @ManyToOne side

Explanation

The many side holds the foreign key column; mappedBy marks the inverse side.

Step-by-step solution

  1. MappedBy marks a side as the inverse side.
  2. The many side holds the foreign key column.
  3. So the @ManyToOne side owns the relationship.
  4. The answer is the @ManyToOne side.

Why the other options are wrong

  • B. Only one side owns the foreign key, so both sides are not equal.
  • C. One side must own the relationship.
  • D. The @OneToMany side is the inverse side with mappedBy.

Tip: The side without mappedBy, which holds the foreign key, is the owner.

Question 3Medium

A @ManyToMany relationship between Student and Course is usually mapped using:

  1. A join table
  2. A foreign key in Course
  3. A foreign key in Student
  4. An embedded collection

Answer: A. A join table

Explanation

@JoinTable defines the link table with two foreign keys.

Step-by-step solution

  1. Student and Course are both many-to-many, so one FK in either table cannot link many rows to many.
  2. A join table holds one row per student-course pair, with two foreign keys.
  3. @JoinTable defines that link table, so the answer is option A.

Why the other options are wrong

  • B. One foreign key in Course can point to only one student, so it cannot model many students.
  • C. One foreign key in Student can point to only one course, so it cannot model many courses.
  • D. An embedded collection stores values inside the owner's table, not links between two entities.

Tip: Many-to-many needs a join table; in a one-to-many, the foreign key sits on the many side.

Question 4Medium

What does spring.jpa.open-in-view=true (the default) do?

  1. Keeps the session open per request
  2. Logs SQL to the browser
  3. Enables HTTPS
  4. Opens the database console

Answer: A. Keeps the session open per request

Explanation

It allows lazy loading in the web layer but can hide N+1 issues and hold connections longer; Boot logs a warning about it.

Step-by-step solution

  1. Open-in-view is on by default for web apps.
  2. It keeps the JPA EntityManager/session open for the whole request.
  3. So lazy associations can load while rendering the view.

Why the other options are wrong

  • B. It has nothing to do with the browser.
  • C. HTTPS is a server setting.
  • D. It does not open a console.

Question 5Medium

cascade = CascadeType.PERSIST on @OneToMany(mappedBy = "order") List<OrderLine> lines means:

  1. The order is saved twice
  2. Saving an order saves new lines
  3. Lines are deleted when the order loads
  4. Lines cannot be saved

Answer: B. Saving an order saves new lines

Explanation

Cascades propagate operations from parent to children.

Step-by-step solution

  1. cascade = CascadeType.PERSIST passes the persist operation from the parent to its children.
  2. Saving the Order therefore also persists its new OrderLine entries in the list.
  3. So the meaning is option B.

Why the other options are wrong

  • A. The order is not saved twice; one persist call cascades to the lines.
  • C. PERSIST cascade does not delete anything; lines are not removed on load.
  • D. Lines can be saved; the cascade is exactly what saves them with the order.

Tip: PERSIST cascades saves; REMOVE cascades deletes. Check the cascade type to know which operation reaches the children.

Question 6Medium

What is the default fetch type of @OneToMany?

  1. LAZY
  2. EAGER
  3. JOIN
  4. SUBSELECT

Answer: A. LAZY

Explanation

Collections are loaded lazily by default.

Step-by-step solution

  1. @OneToMany is a collection association.
  2. Collections are loaded lazily by default.
  3. EAGER, JOIN and SUBSELECT are not the default.
  4. So the default is LAZY.

Why the other options are wrong

  • B. EAGER is not the default for @OneToMany.
  • C. JOIN is a query strategy, not the fetch type.
  • D. SUBSELECT is a fetching strategy, not the default fetch type.

Tip: To-many is LAZY by default; to-one is EAGER by default.

Question 7Medium

What is the default fetch type of @ManyToOne?

  1. EAGER
  2. LAZY
  3. BATCH
  4. NONE

Answer: A. EAGER

Explanation

To-one associations default to EAGER; to-many associations default to LAZY.

Step-by-step solution

  1. To-one associations such as @ManyToOne default to EAGER.
  2. EAGER loads the related entity with the parent.
  3. LAZY and BATCH are not the default for @ManyToOne.
  4. So the default is EAGER.

Why the other options are wrong

  • B. LAZY must be set explicitly for @ManyToOne.
  • C. BATCH is a fetching strategy setting, not the fetch type.
  • D. NONE is not a fetch type.

Tip: Set fetch = FetchType.LAZY on @ManyToOne to avoid loading related rows by default.

Question 8Medium

Which JPA annotation maps an embeddable value object such as Address inside a Customer table?

  1. @Transient
  2. @Embedded
  3. @OneToOne only
  4. @Lob

Answer: B. @Embedded

Explanation

Embeddables store their fields in the owning entity's table.

Step-by-step solution

  1. Address is a value object that has no identity of its own.
  2. @Embedded marks it as an embeddable whose fields are stored in the owner's table.
  3. So the annotation is option B, @Embedded.

Why the other options are wrong

  • A. @Transient stops a field from being persisted, which is the opposite of what is needed here.
  • C. @OneToOne links two entities with a relationship; the word only makes this option wrong.
  • D. @Lob marks a large column type, not an embedded object.

Tip: Use @Embedded for value objects owned by one entity; use an entity relation for objects with their own identity.

Question 9Hard

Adding @Version to an entity field enables:

  1. Automatic schema versioning
  2. Pessimistic row locks
  3. API versioning
  4. Optimistic locking

Answer: D. Optimistic locking

Explanation

Updates check the version; a concurrent modification causes an optimistic lock exception instead of a lost update.

Step-by-step solution

  1. @Version adds a version field that Hibernate checks on each update.
  2. If another change has already bumped the version, the update fails instead of overwriting data.
  3. That is optimistic locking, option D.

Why the other options are wrong

  • A. @Version does not version the database schema.
  • B. Pessimistic row locks come from lock modes, not from @Version.
  • C. @Version tracks row changes, not API versions.

Tip: @Version means optimistic locking; use a pessimistic lock mode when you must block other writers.

Question 10Hard

Accessing a lazy collection after the transaction and persistence context have closed (with open-in-view disabled) typically throws:

  1. OptimisticLockException
  2. LazyInitializationException
  3. SQLSyntaxErrorException
  4. NullPointerException

Answer: B. LazyInitializationException

Explanation

The proxy cannot load data without an open session; fetch what you need inside the transaction.

Step-by-step solution

  1. The collection is lazy, so Hibernate loads it only when it is first accessed.
  2. After the transaction and persistence context close, there is no open session to load it with.
  3. Accessing it then throws LazyInitializationException, option B.

Why the other options are wrong

  • A. OptimisticLockException comes from a version conflict on update, not from reading a lazy field.
  • C. SQLSyntaxErrorException means the SQL text is invalid, not that a session has closed.
  • D. NullPointerException is a generic Java error; this case is a Hibernate lazy-load failure.

Tip: Fetch what you need inside the transaction, for example with a fetch join, before the method returns.

Practise all 15 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