0
0
Spring Bootframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OneToOne Mastery
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 User with a @OneToOne mapped Address?

Given the following Spring Boot entities, what will be printed when fetching a User and printing its Address street?

Spring Boot
public class User {
  @Id
  private Long id;

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "address_id")
  private Address address;

  public Address getAddress() { return address; }
}

public class Address {
  @Id
  private Long id;
  private String street;

  public String getStreet() { return street; }
}

// Assume userRepository.findById(1L) returns a User with Address street = "Main St"
Anull
BThrows LazyInitializationException
C"Main St"
D"" (empty string)
Attempts:
2 left
💡 Hint

Consider the default fetch type of @OneToOne and if the session is open when accessing the address.

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

Choose the correct code snippet that sets up a bidirectional @OneToOne relationship between User and Profile entities.

A
public class User {
  @OneToOne
  @JoinColumn(name = "profile_id")
  private Profile profile;
}

public class Profile {
  @OneToOne(mappedBy = "profile")
  private User user;
}
B
public class User {
  @OneToOne(mappedBy = "user")
  private Profile profile;
}

public class Profile {
  @OneToOne
  @JoinColumn(name = "user_id")
  private User user;
}
C
public class User {
  @OneToOne
  private Profile profile;
}

public class Profile {
  @OneToOne
  private User user;
}
D
public class User {
  @OneToOne(mappedBy = "profile")
  private Profile profile;
}

public class Profile {
  @OneToOne
  @JoinColumn(name = "user_id")
  private User user;
}
Attempts:
2 left
💡 Hint

Remember the owning side has the @JoinColumn annotation and the inverse side uses mappedBy.

🔧 Debug
advanced
2:00remaining
Why does this @OneToOne mapping cause a runtime error?

Consider this code snippet:

public class Employee {
  @OneToOne
  private ParkingSpot spot;
}

public class ParkingSpot {
  @OneToOne
  private Employee employee;
}

When saving an Employee, a runtime exception occurs. What is the cause?

AMissing @JoinColumn on owning side causes Hibernate to fail determining the foreign key.
BCascadeType.ALL is missing, so related entities are not saved automatically.
CThe entities must use @MapsId to share the same primary key.
DThe fetch type must be LAZY to avoid infinite recursion.
Attempts:
2 left
💡 Hint

Check which side owns the relationship and if the foreign key column is defined.

state_output
advanced
2:00remaining
What is the state of the database after saving a User with a @OneToOne Address?

Given these entities:

public class User {
  @Id
  private Long id;

  @OneToOne(cascade = CascadeType.PERSIST)
  @JoinColumn(name = "address_id")
  private Address address;
}

public class Address {
  @Id
  private Long id;
  private String city;
}

If you create a User and set a new Address (without saving Address separately) and then save User, what happens in the database?

AUser is saved with address_id null; Address is saved separately later.
BOnly User is saved; Address is ignored because it was not saved explicitly.
CSaving User fails with a foreign key constraint error because Address ID is null.
DBoth User and Address are saved; Address gets an ID and User's address_id points to it.
Attempts:
2 left
💡 Hint

Think about what CascadeType.PERSIST does when saving related entities.

🧠 Conceptual
expert
2:00remaining
Which statement about @OneToOne fetch types is true?

Consider the default fetch types in JPA for @OneToOne and @ManyToOne. Which statement is correct?

A@OneToOne defaults to EAGER fetch, which can cause performance issues if not managed.
B@OneToOne defaults to LAZY fetch, so related entities load only when accessed.
C@ManyToOne defaults to LAZY fetch, while @OneToOne defaults to LAZY as well.
D@OneToOne and @ManyToOne both default to EAGER fetch for simplicity.
Attempts:
2 left
💡 Hint

Recall the JPA specification defaults for fetch types on these annotations.