Enhance your understanding of Spring Boot JPA and Hibernate integration with this engaging quiz. Assess your grasp of annotations, configuration, entity management, and transaction handling within the Java Persistence API and Hibernate framework contexts.
Which annotation is used to mark a Java class as a JPA entity for mapping to a database table?
Explanation: @Entity is the required annotation to identify a class as a JPA-managed entity. @Table specifies the table's name but does not itself suffice to make a class an entity. @Repository is used for repository classes, and @Document is for document-based data stores, not relational tables.
How do you specify the primary key in a JPA entity class?
Explanation: @Id annotation is used to define the primary key of a JPA entity. @PrimaryKey and @Key are not valid JPA annotations, and @PK is not part of the specification. Only @Id will correctly identify a field as the primary key for the entity.
Which interface should a Spring Data repository extend to gain basic CRUD operations with JPA entities?
Explanation: JpaRepository provides all CRUD methods and additional JPA-specific features. CrudRepository offers basic CRUD operations but lacks JPA-specific extensions. PagingAndSortingRepository supports sorting and paging, but JpaRepository includes its functionality plus more. RepositoryBean is not a recognized interface.
In Hibernate and JPA, which annotation defines a many-to-one relationship between two entities?
Explanation: @ManyToOne specifies that multiple instances of one entity are associated with a single instance of another. @OneToMany is for the reverse, @OneToOne is for one-to-one relationships, and @ManyToMany is for relationships where both sides have many related entities.
What is the main purpose of the persistence context in JPA?
Explanation: The persistence context keeps track of entity objects and their changes, ensuring proper synchronization with the database. It does not handle HTTP caching or create SQL queries directly. Error handling in the database is a concern outside the persistence context.
What is the default fetch policy for @ManyToOne relationships in JPA?
Explanation: By default, @ManyToOne associations are fetched eagerly, meaning the related entity is loaded at the same time. LAZY is common for @OneToMany and @ManyToMany, not @ManyToOne by default. DEFAULT and IMMEDIATE are not fetch types in JPA.
Which dependency simplifies integrating JPA and Hibernate in a Spring Boot application?
Explanation: The spring-boot-starter-data-jpa includes everything needed to work with JPA and Hibernate in Spring Boot. The web MVC starter is for web functionality, not persistence. jpa-hibernate-integration and starter-jpa-data are not official starter dependencies.
Which property in application.properties automatically updates the database schema based on entity changes?
Explanation: spring.jpa.hibernate.ddl-auto controls schema updates based on JPA entity definitions. spring.datasource.url is for database connection, show-sql only logs SQL queries, and generate-schema is not a valid property in this context.
Which annotation enables declarative transaction management for JPA repositories?
Explanation: @Transactional indicates transactional boundaries, ensuring atomic database operations. @PersistenceUnit is for injecting entity managers, @EnableJPA does not exist, and @EntityManager injects the manager itself but does not control transactions.
Which annotation do you use to auto-generate primary key values for a JPA entity?
Explanation: @GeneratedValue tells JPA or Hibernate to generate the primary key automatically. @AutoIncrement and the other similar-sounding annotations are not valid in JPA, even though they sound plausible. Only @GeneratedValue is correct for this purpose.