0
0
Spring Bootframework~20 mins

Why JPA matters for database access in Spring Boot - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JPA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use JPA instead of plain SQL in Spring Boot?
Which of the following best explains why JPA is preferred over writing plain SQL queries directly in Spring Boot applications?
AJPA only works with NoSQL databases, so it is not suitable for relational databases.
BJPA requires writing more SQL queries manually, giving developers full control over database access.
CJPA disables transaction management, making database operations faster but less safe.
DJPA automatically maps Java objects to database tables, reducing boilerplate code and improving maintainability.
Attempts:
2 left
💡 Hint
Think about how JPA helps connect Java code with database tables.
component_behavior
intermediate
2:00remaining
JPA Entity Behavior in Spring Boot
Given a JPA entity with a field annotated as @Id and @GeneratedValue, what happens when you save a new entity instance using a Spring Data JPA repository?
Spring Boot
import jakarta.persistence.*;

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

  // getters and setters
}

// In a Spring Boot service:
Product p = new Product();
p.setName("Book");
productRepository.save(p);

Long savedId = p.getId();
AThe id field remains null because JPA does not generate IDs automatically.
BThe id field is automatically assigned a unique value by the database after saving.
CAn exception is thrown because the id field must be set manually before saving.
DThe id field is set to zero by default after saving.
Attempts:
2 left
💡 Hint
Consider what @GeneratedValue does in JPA.
📝 Syntax
advanced
2:00remaining
Correct JPQL Query Syntax in Spring Data JPA
Which of the following JPQL queries correctly selects all users with the last name 'Smith' using Spring Data JPA?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
  @Query("?1")
  List<User> findByLastName();
}
A"SELECT u FROM User u WHERE u.lastName = 'Smith'"
B"SELECT * FROM users WHERE lastName = 'Smith'"
C"FROM User WHERE lastName = 'Smith'"
D"SELECT u FROM User u WHERE lastName = 'Smith'"
Attempts:
2 left
💡 Hint
JPQL uses entity names and fields, not table names or *.
state_output
advanced
2:00remaining
Effect of Transactional Annotation on JPA Repository Save
In a Spring Boot service method annotated with @Transactional, what happens if an exception is thrown after saving an entity with a JPA repository?
Spring Boot
@Service
public class UserService {
  @Autowired
  private UserRepository userRepository;

  @Transactional
  public void createUser(String name) {
    User user = new User();
    user.setName(name);
    userRepository.save(user);
    if (true) {
      throw new RuntimeException("Fail");
    }
  }
}
AThe save operation is rolled back, so the user is not saved in the database.
BThe user is saved despite the exception because save() commits immediately.
CThe exception is ignored and the transaction commits normally.
DThe database connection is closed but the user is saved.
Attempts:
2 left
💡 Hint
Think about what @Transactional does when exceptions occur.
🔧 Debug
expert
3:00remaining
Diagnosing LazyInitializationException in JPA
A Spring Boot application using JPA throws a LazyInitializationException when accessing a collection property of an entity outside a transaction. Which option best explains why this happens?
AThe entity class is missing the @Entity annotation, so JPA does not manage it.
BThe entity was saved without an ID, causing the collection to be null.
CThe collection was not fetched eagerly and the session is closed when accessed, so JPA cannot load it lazily.
DThe database connection was lost, so JPA cannot fetch any data.
Attempts:
2 left
💡 Hint
LazyInitializationException usually relates to session and fetching strategy.