Hibernate Online Quiz

This blog post offers a comprehensive online quiz consisting of 25 multiple-choice questions (MCQs) about Hibernate, a popular object-relational mapping (ORM) framework for Java applications. This quiz is designed to assess and enhance your understanding of Hibernate's core functionalities, features, and best practices. Whether you are a beginner looking to get started with ORM or an experienced developer seeking to test your Hibernate knowledge, this quiz will provide valuable insights.

1. What is the primary purpose of the SessionFactory in Hibernate?

a) Managing the JDBC connection pool
b) Creating Session objects
c) Handling transaction management processes
d) Validating object data before saving it to the database

2. Which annotation is used to declare a class as an entity in Hibernate?

a) @Entity
b) @Table
c) @Column
d) @EntityModel

3. What does the @Column annotation define in Hibernate?

a) The table associated with an entity class
b) The constructor of an entity class
c) A specific column in a database table mapped to an entity field
d) The primary key field of an entity class

4. What is the role of the @Id annotation in Hibernate?

a) To define a property as transitory
b) To define the column mapping for a property
c) To define the primary key of an entity
d) To optimize the performance of queries

5. Which of the following is not a valid Fetch type in Hibernate?

a) EAGER
b) LAZY
c) IMMEDIATE
d) DEFAULT

6. What does the @Transient annotation imply for a field in Hibernate?

a) The field is mapped to a column in the database table
b) The field is a primary key
c) The field is not persistent and not to be stored in the database
d) The field is automatically updated whenever the entity is persisted

7. In Hibernate, what is the function of the @ManyToOne annotation?

a) Defines a many-to-one relationship between entity classes
b) Maps a collection of instances in an entity class
c) Specifies that an entity class can inherit properties from multiple parent entities
d) Indicates that multiple rows in a table can relate to one row in another table

8. What is the Hibernate Query Language (HQL) used for?

a) Defining database schemas
b) Creating triggers and stored procedures
c) Object-oriented querying of data
d) Managing database connections

9. Which method in the Session interface is used to start a new transaction in Hibernate?

a) beginTransaction()
b) openTransaction()
c) startTransaction()
d) newTransaction()

10. What is the use of the @GeneratedValue annotation in Hibernate?

a) It specifies that a value is generated for an @Id field automatically
b) It generates SQL to optimize database operations
c) It creates a new value for every new transaction
d) It validates generated values against the schema

11. How is Session.flush() method used in Hibernate?

a) To commit the transaction
b) To refresh the session with updated values from the database
c) To clear the session cache
d) To synchronize the session's persistence context to the underlying database

12. What is the impact of the cascade attribute in a relationship mapping?

a) It specifies that operations must be isolated to the primary entity only
b) It prevents changes to the related entities
c) It indicates that operations are to be cascaded to the related entities
d) It strictly separates entity associations

13. Which statement is true about the Criteria API in Hibernate?

a) It is used to execute native SQL queries
b) It provides a programmatic way to build queries
c) It is less efficient than using HQL
d) It can only be used for updating data

14. What is the purpose of the version property in Hibernate?

a) To specify a custom version of Hibernate framework
b) To facilitate optimistic locking for concurrency control
c) To track changes in entity relationships
d) To maintain compatibility with different database versions

15. In Hibernate, the @Embeddable annotation is used to indicate:

a) That a class should be embedded by other entities
b) A field that references another entity
c) That a class cannot be instantiated directly
d) A superclass that other classes cannot extend

16. What does the @NamedQuery annotation allow you to do in Hibernate?

a) Map the query result to a non-entity class
b) Define a reusable HQL query with a name
c) Execute native SQL queries efficiently
d) Update multiple entities with a single query

17. The cascade option CascadeType.ALL implies what behavior in Hibernate?

a) Only update operations will be cascaded.
b) All operations, including save, delete, and update, are cascaded.
c) No operations are cascaded; manual handling is required.
d) Only fetch operations are cascaded.

18. In Hibernate, what is the significance of the @DiscriminatorColumn annotation?

a) It differentiates rows in a single table that represent subclasses of a class hierarchy.
b) It is used to specify a unique constraint on a column.
c) It is used to mark a column as indexed.
d) It distinguishes between transient and persistent properties.

19. What is the purpose of the @JoinColumn annotation in Hibernate?

a) To define a foreign key column that joins two tables
b) To mark a column as not nullable
c) To specify a column for sorting data
d) To identify a column used in aggregate functions

20. What functionality does the @Temporal annotation provide in Hibernate?

a) It specifies whether a date should include time or not.
b) It converts a property to a temporal type (Date, Time, Timestamp).
c) It allows the entity to be time-zone aware.
d) It provides time-based versioning of entities.

21. Which interface must a class implement to be considered a Hibernate UserType?

a) Serializable
b) Cloneable
c) UserType
d) Externalizable

22. What is an orphanRemoval in Hibernate?

a) It prevents the removal of child entities when the parent entity is deleted.
b) It ensures that child entities are automatically deleted when they are removed from their parent.
c) It locks the database records for orphan records.
d) It flags entities that are no longer referred to by any other entity.

23. How does the @SecondaryTable annotation function in Hibernate?

a) It allows an entity to be mapped to multiple tables.
b) It specifies a backup table used during recovery.
c) It creates a temporary table for transaction-specific operations.
d) It specifies a table used only for read operations.

24. What is the purpose of the @NaturalId annotation in Hibernate?

a) It marks an id as being generated by the database.
b) It is used to specify a natural key for the entity.
c) It indicates that the id should be manually assigned.
d) It specifies a UUID as the id.

25. What does enabling the dynamic-insert property do in Hibernate?

a) It causes Hibernate to generate SQL that includes only the columns whose values are not null.
b) It forces Hibernate to include all columns in the insert statement, regardless of whether they are null.
c) It updates all columns upon every insert to ensure consistency.
d) It disables batch processing for insert statements.

Comments