0
0
Spring Bootframework~20 mins

Fetch types (LAZY vs EAGER) in Spring Boot - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fetch Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when accessing a LAZY fetched collection outside a transaction?

Consider a Spring Boot JPA entity with a @OneToMany(fetch = FetchType.LAZY) collection. What will happen if you try to access this collection after the transaction has closed?

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

    public List<Book> getBooks() {
        return books;
    }
}

// In service method with @Transactional
Author author = authorRepository.findById(1L).get();
// Transaction ends here
List<Book> books = author.getBooks(); // What happens here?
AReturns an empty list because no books were loaded.
BThrows a LazyInitializationException because the collection was not loaded and session is closed.
CReturns the full list of books without any error.
DThrows a NullPointerException because the collection is null.
Attempts:
2 left
💡 Hint

Think about when the data is actually loaded from the database with LAZY fetch.

state_output
intermediate
1:30remaining
What is the default fetch type for @ManyToOne in Spring Data JPA?

Given the following entity snippet, what is the default fetch type for the @ManyToOne association?

Spring Boot
public class Book {
    @ManyToOne
    private Author author;

    public Author getAuthor() {
        return author;
    }
}
ALAZY
BDepends on the database configuration
CEAGER
DFetchType is not defined by default and must be set explicitly
Attempts:
2 left
💡 Hint

Check the JPA specification defaults for @ManyToOne.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly sets a LAZY fetch type for a @OneToOne association?

Choose the correct way to declare a @OneToOne association with LAZY fetching in Spring Data JPA.

A
@OneToOne(fetch = FetchType.LAZY)
private Profile profile;
B
@OneToOne(lazy = true)
private Profile profile;
C
@OneToOne(fetchType = FetchType.LAZY)
private Profile profile;
D
@OneToOne(fetch = LAZY)
private Profile profile;
Attempts:
2 left
💡 Hint

Look carefully at the attribute name and enum usage in the annotation.

🔧 Debug
advanced
2:00remaining
Why does this EAGER fetch cause performance issues?

Given this entity relationship, why might setting fetch = FetchType.EAGER on a @OneToMany cause performance problems?

Spring Boot
public class Author {
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "author")
    private List<Book> books;
}
ABecause EAGER fetch disables caching and forces database hits every time.
BBecause EAGER fetch delays loading the books until explicitly accessed.
CBecause EAGER fetch causes the books list to be null until initialized.
DBecause EAGER fetch loads all related books immediately, causing large joins or multiple queries.
Attempts:
2 left
💡 Hint

Think about what happens when many related entities are loaded eagerly.

🧠 Conceptual
expert
2:30remaining
How does Spring Boot handle LAZY fetch proxies in REST controllers by default?

In a Spring Boot REST controller, if you return an entity with LAZY loaded associations, what is the typical behavior when serializing to JSON?

ASerialization fails with a LazyInitializationException because the session is closed.
BThe LAZY associations are automatically fetched and included in the JSON output.
CThe LAZY associations are ignored and not included in the JSON output.
DThe LAZY associations are serialized as empty objects.
Attempts:
2 left
💡 Hint

Consider when the Hibernate session is open and how Jackson serializes proxies.