0
0
Spring Bootframework~20 mins

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

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

Given the following JPA entities, what will be printed when fetching a Book entity and accessing its author field?

Spring Boot
import jakarta.persistence.*;

@Entity
public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    // getters and setters
}

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;

    @ManyToOne
    @JoinColumn(name = "author_id")
    private Author author;

    // getters and setters
}

// In a service method:
Book book = entityManager.find(Book.class, 1L);
System.out.println(book.getAuthor().getName());
APrints null because the author is not fetched automatically
BPrints the name of the author linked to the book with id 1
CThrows NullPointerException because author is not initialized
DThrows a compilation error due to missing annotations
Attempts:
2 left
💡 Hint

Consider the default fetch type of @ManyToOne and what happens when you access the related entity.

📝 Syntax
intermediate
1:30remaining
Which option correctly defines a @ManyToOne relationship with a custom join column?

Choose the correct code snippet that defines a @ManyToOne relationship with a join column named category_id in a Product entity.

A
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
B
@ManyToOne
@JoinColumn("category_id")
private Category category;
C
@ManyToOne
@JoinColumn(columnName = "category_id")
private Category category;
D
@ManyToOne
@JoinColumn(name = category_id)
private Category category;
Attempts:
2 left
💡 Hint

Remember the correct attribute name for specifying the column name in @JoinColumn.

🔧 Debug
advanced
2:30remaining
Why does this @ManyToOne mapping cause a runtime LazyInitializationException?

Consider this code snippet:

Book book = entityManager.find(Book.class, 1L);
entityManager.close();
System.out.println(book.getAuthor().getName());

Why does accessing book.getAuthor().getName() throw a LazyInitializationException?

ABecause the default fetch type of @ManyToOne is LAZY and the entity manager is closed before accessing the author
BBecause the Book entity is detached and cannot access any fields
CBecause the @JoinColumn is missing, so the relationship is not mapped
DBecause the author field is null and accessing getName() causes the exception
Attempts:
2 left
💡 Hint

Check the default fetch type of @ManyToOne and when the related entity is loaded.

state_output
advanced
2:30remaining
What is the state of the owning side after this code runs?

Given these entities:

@Entity
class Order {
  @Id
  Long id;

  @ManyToOne
  Customer customer;
}

@Entity
class Customer {
  @Id
  Long id;

  String name;
}

And this code:

Order order = new Order();
Customer customer = new Customer();
customer.setName("Alice");
order.setCustomer(customer);
entityManager.persist(customer);
entityManager.persist(order);
entityManager.flush();

What is true about the database state after flush?

AOrder is saved but customer_id in Order is null because Customer has no id yet
BOnly Customer is saved because Order depends on Customer but is not persisted
CNeither entity is saved because Customer must be saved before setting in Order
DBoth Customer and Order are saved, and Order's customer_id column references the Customer's id
Attempts:
2 left
💡 Hint

Think about the order of persisting and how JPA handles relationships.

🧠 Conceptual
expert
3:00remaining
Which statement about @ManyToOne and cascading is correct?

Consider a @ManyToOne relationship from Invoice to Client. Which of these statements about cascading operations is true?

ACascadeType.REMOVE on @ManyToOne deletes the Invoice when Client is deleted
BCascadeType.ALL on @ManyToOne is required to save the Client when saving Invoice
CCascadeType.ALL on @ManyToOne causes operations on Invoice to propagate to Client, which can lead to unintended deletes
DCascadeType.PERSIST on @ManyToOne prevents saving Invoice without an existing Client
Attempts:
2 left
💡 Hint

Think about the direction of cascading and what happens when you delete the owning entity.