0
0
Spring Bootframework~10 mins

Why relationships matter in JPA in Spring Boot - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a one-to-many relationship in JPA.

Spring Boot
@Entity
public class Author {
    @Id
    private Long id;
    
    @OneToMany(mappedBy = [1])
    private List<Book> books;
}
Drag options to blanks, or click blank then click option'
Aauthor
BauthorId
Cbooks
Dbook
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name in mappedBy causes errors.
Confusing the collection name with the mappedBy value.
2fill in blank
medium

Complete the code to specify the owning side of a many-to-one relationship.

Spring Boot
@Entity
public class Book {
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = [1])
    private Author author;
}
Drag options to blanks, or click blank then click option'
A"book_id"
B"id"
C"author_id"
D"authorName"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column name causes database errors.
Omitting quotes around the column name.
3fill in blank
hard

Fix the error in the code to correctly define a bidirectional one-to-one relationship.

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

    @OneToOne(mappedBy = [1])
    private Profile profile;
}

@Entity
public class Profile {
    @Id
    private Long id;

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;
}
Drag options to blanks, or click blank then click option'
Auser
BprofileId
Cprofile
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name in mappedBy causes runtime errors.
Confusing the owning and inverse sides.
4fill in blank
hard

Fill both blanks to create a many-to-many relationship with a join table.

Spring Boot
@Entity
public class Student {
    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name = [1],
        joinColumns = @JoinColumn(name = [2]))
    private Set<Course> courses;
}
Drag options to blanks, or click blank then click option'
A"student_course"
B"student_id"
C"course_id"
D"course_student"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up join table and join column names.
Using the wrong entity name in joinColumns.
5fill in blank
hard

Fill all three blanks to define a unidirectional one-to-many relationship with cascade and fetch type.

Spring Boot
@Entity
public class Department {
    @Id
    private Long id;

    @OneToMany(cascade = [1], fetch = [2])
    @JoinColumn(name = [3])
    private List<Employee> employees;
}
Drag options to blanks, or click blank then click option'
ACascadeType.ALL
BFetchType.LAZY
C"department_id"
DFetchType.EAGER
Attempts:
3 left
💡 Hint
Common Mistakes
Using FetchType.EAGER can cause performance issues.
Forgetting to specify the join column name.