Hibernate Framework MCQs: Master Java ORM (Object-Relational Mapping)
Enhance your understanding of Hibernate with these 30 MCQs covering key topics like Hibernate introduction, configuration, mapping Java classes to database tables, and Hibernate Query Language (HQL). Prepare for success in Java ORM.
1. What is the primary purpose of the Hibernate framework in Java?
2. Which of the following is a key feature of Hibernate?
3. What does Hibernate use to automatically generate SQL queries for database operations?
4. Which file is typically used for Hibernate configuration?
hibernate.cfg.xmlhibernate.propertiespersistence.xmlapplication.properties5. Which of the following annotations is used to mark a Java class as an entity in Hibernate?
@Entity@Table@Column@Id6. What is the purpose of the sessionFactory object in Hibernate?
7. In Hibernate, which interface is used for interacting with the database?
SessionSessionFactoryTransactionEntityManager8. Which of the following annotations is used to specify the primary key of an entity in Hibernate?
@PrimaryKey@Id@Column@GeneratedValue9. What is the default database dialect used by Hibernate for SQL generation?
10. Which of the following is the correct way to configure Hibernate with XML mapping?
hibernate.cfg.xml filepersistence.xml fileapplication.properties filehibernate.properties file11. What is the Hibernate Session used for?
12. In Hibernate, how do you define a one-to-many relationship between two entities?
@OneToMany annotation@ManyToOne annotation@ManyToMany annotation@JoinColumn13. Which Hibernate annotation is used to define a many-to-many relationship between two entities?
@OneToMany@ManyToOne@ManyToMany@JoinTable14. What is the purpose of the @GeneratedValue annotation in Hibernate?
15. Which method of the Session interface is used to save a new object in Hibernate?
save()create()persist()insert()16. Which Hibernate configuration property is used to specify the database connection URL?
hibernate.connection.urlhibernate.db.urlhibernate.jdbc.urlhibernate.database.url17. What is the role of the Transaction interface in Hibernate?
18. In Hibernate, what is the significance of the session.beginTransaction() method?
19. Which of the following annotations in Hibernate maps a Java class to a database table?
@Entity@Table@Column@Id20. What does the Hibernate Query Language (HQL) allow you to do?
21. How do you perform a basic SELECT query in HQL?
SELECT * FROM entityFROM EntitySELECT entity FROM EntitySELECT entity22. In HQL, which keyword is used to fetch all objects of a particular entity?
ALLFROMSELECTFETCH23. How would you write a WHERE clause in an HQL query?
WHERE column = valueWHERE entity.property = valueWHERE entity = valueWHERE value IN entity24. Which of the following methods is used to execute an HQL query in Hibernate?
executeQuery()createQuery()select()getQuery()25. What is the use of Query.setParameter() method in Hibernate?
26. In Hibernate, what does the distinct keyword do in an HQL query?
27. Which Hibernate query method is used for batch processing?
executeUpdate()setParameter()createNativeQuery()executeQuery()28. What is the advantage of using HQL over SQL in Hibernate?
29. How can you perform a JOIN operation in HQL?
JOIN keywordINNER JOIN keywordFETCH keywordFROM keyword30. What does the @Transient annotation indicate in Hibernate?
| QNo | Answer |
|---|---|
| 1 | B. To provide Object-Relational Mapping (ORM) to map Java objects to database tables |
| 2 | B. Supports Object-Relational Mapping (ORM) |
| 3 | A. Hibernate Query Language (HQL) |
| 4 | A. hibernate.cfg.xml |
| 5 | A. @Entity |
| 6 | B. To create and manage Hibernate sessions |
| 7 | A. Session |
| 8 | B. @Id |
| 9 | C. H2Dialect |
| 10 | A. Use the hibernate.cfg.xml file |
| 11 | D. All of the above |
| 12 | A. Using the @OneToMany annotation |
| 13 | C. @ManyToMany |
| 14 | A. To automatically generate values for the primary key |
| 15 | A. save() |
| 16 | A. hibernate.connection.url |
| 17 | C. To provide database transaction support |
| 18 | A. It begins a database transaction |
| 19 | B. @Table |
| 20 | B. Perform operations on database tables using object-oriented syntax |
| 21 | C. SELECT entity FROM Entity |
| 22 | B. FROM |
| 23 | B. WHERE entity.property = value |
| 24 | B. createQuery() |
| 25 | A. To set query parameters dynamically |
| 26 | A. It fetches unique records only |
| 27 | A. executeUpdate() |
| 28 | A. HQL allows object-oriented queries |
| 29 | A. Using the JOIN keyword |
| 30 | A. A field should not be mapped to a database column |