0
0
Spring Bootframework~20 mins

@OneToMany relationship in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of @OneToMany Relationships
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when fetching a parent entity with @OneToMany lazy loading?

Consider a Spring Boot JPA entity Author with a @OneToMany relationship to Book marked as fetch = FetchType.LAZY. When you fetch an Author from the database and access the list of Book entities for the first time, what happens?

Spring Boot
public class Author {
    @OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
    private List<Book> books;

    // getters and setters
}

// In service:
Author author = authorRepository.findById(1L).get();
List<Book> books = author.getBooks();
AThe list of books is always empty unless explicitly fetched with a separate query.
BThe list of books is fetched immediately when the author is fetched, regardless of getBooks() call.
CAn exception is thrown because lazy loading is not supported in Spring Boot.
DThe list of books is fetched from the database only when getBooks() is called for the first time.
Attempts:
2 left
💡 Hint

Think about what FetchType.LAZY means for loading related entities.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly defines a bidirectional @OneToMany relationship?

Choose the correct way to define a bidirectional @OneToMany relationship between Author and Book entities in Spring Boot JPA.

AIn Author: <code>@OneToMany private List<Book> books;</code><br>In Book: <code>@ManyToOne(mappedBy = "author") private Author author;</code>
BIn Author: <code>@ManyToOne private List<Book> books;</code><br>In Book: <code>@OneToMany private Author author;</code>
CIn Author: <code>@OneToMany(mappedBy = "author") private List<Book> books;</code><br>In Book: <code>@ManyToOne private Author author;</code>
DIn Author: <code>@OneToMany(mappedBy = "books") private List<Book> books;</code><br>In Book: <code>@ManyToOne private Author author;</code>
Attempts:
2 left
💡 Hint

Remember that mappedBy is used on the inverse side to point to the owning side's field.

🔧 Debug
advanced
2:00remaining
Why does adding a new Book to Author's list not persist the relationship?

Given the following code, why does adding a new Book to the Author's books list not save the relationship in the database?

Author author = authorRepository.findById(1L).get();
Book newBook = new Book();
newBook.setTitle("New Book");
author.getBooks().add(newBook);
authorRepository.save(author);
ABecause the <code>Book</code> entity's <code>author</code> field is not set, so JPA does not know about the relationship.
BBecause <code>authorRepository.save()</code> does not cascade saves to the <code>books</code> list by default.
CBecause the <code>Author</code> entity must be deleted before adding new books.
DBecause the <code>books</code> list is immutable and cannot be modified.
Attempts:
2 left
💡 Hint

Think about which side owns the relationship and what fields must be set for JPA to persist it.

state_output
advanced
2:00remaining
What is the size of the books list after this code runs?

Given the following entities and code, what is the size of author.getBooks() after execution?

@Entity
class Author {
  @OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
  private List books = new ArrayList<>();
}

@Entity
class Book {
  @ManyToOne
  private Author author;
}

// Code:
Author author = new Author();
Book book1 = new Book();
book1.setAuthor(author);
Book book2 = new Book();
// book2.setAuthor(author); // missing

author.getBooks().add(book1);
author.getBooks().add(book2);

int size = author.getBooks().size();
A2
B1
C0
DThrows NullPointerException
Attempts:
2 left
💡 Hint

Consider what the books list contains after adding both books.

🧠 Conceptual
expert
2:00remaining
What is the effect of omitting 'mappedBy' in a @OneToMany relationship?

In a Spring Boot JPA entity, what happens if you define a @OneToMany relationship without specifying the mappedBy attribute?

AJPA throws a runtime exception because <code>mappedBy</code> is mandatory for @OneToMany.
BJPA creates a join table to manage the relationship instead of using a foreign key in the child table.
CThe relationship is ignored and no database mapping is created.
DJPA treats the relationship as unidirectional and uses the foreign key in the child table automatically.
Attempts:
2 left
💡 Hint

Think about how JPA manages ownership of relationships and what happens without mappedBy.