MCQs on Hibernate Framework | Java ORM (Object-Relational Mapping)

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.


MCQs

1. What is the primary purpose of the Hibernate framework in Java?

  • A. To perform database operations using JDBC
  • B. To provide Object-Relational Mapping (ORM) to map Java objects to database tables
  • C. To execute SQL queries in Java
  • D. To manage database transactions only

2. Which of the following is a key feature of Hibernate?

  • A. Provides a lightweight container for Java Beans
  • B. Supports Object-Relational Mapping (ORM)
  • C. Works only with MySQL databases
  • D. Requires manual SQL query execution

3. What does Hibernate use to automatically generate SQL queries for database operations?

  • A. Hibernate Query Language (HQL)
  • B. Java Persistence API (JPA)
  • C. JDBC API
  • D. Annotations only

4. Which file is typically used for Hibernate configuration?

  • A. hibernate.cfg.xml
  • B. hibernate.properties
  • C. persistence.xml
  • D. application.properties

5. Which of the following annotations is used to mark a Java class as an entity in Hibernate?

  • A. @Entity
  • B. @Table
  • C. @Column
  • D. @Id

6. What is the purpose of the sessionFactory object in Hibernate?

  • A. To manage database connections
  • B. To create and manage Hibernate sessions
  • C. To execute SQL queries
  • D. To map Java objects to database tables

7. In Hibernate, which interface is used for interacting with the database?

  • A. Session
  • B. SessionFactory
  • C. Transaction
  • D. EntityManager

8. Which of the following annotations is used to specify the primary key of an entity in Hibernate?

  • A. @PrimaryKey
  • B. @Id
  • C. @Column
  • D. @GeneratedValue

9. What is the default database dialect used by Hibernate for SQL generation?

  • A. MySQLDialect
  • B. PostgreSQLDialect
  • C. H2Dialect
  • D. HibernateDialect

10. Which of the following is the correct way to configure Hibernate with XML mapping?

  • A. Use the hibernate.cfg.xml file
  • B. Use persistence.xml file
  • C. Use application.properties file
  • D. Use hibernate.properties file

11. What is the Hibernate Session used for?

  • A. Managing database connections
  • B. Creating and managing transactions
  • C. Performing CRUD operations
  • D. All of the above

12. In Hibernate, how do you define a one-to-many relationship between two entities?

  • A. Using the @OneToMany annotation
  • B. Using the @ManyToOne annotation
  • C. Using the @ManyToMany annotation
  • D. Using @JoinColumn

13. Which Hibernate annotation is used to define a many-to-many relationship between two entities?

  • A. @OneToMany
  • B. @ManyToOne
  • C. @ManyToMany
  • D. @JoinTable

14. What is the purpose of the @GeneratedValue annotation in Hibernate?

  • A. To automatically generate values for the primary key
  • B. To map fields to database columns
  • C. To specify the database table name
  • D. To define a custom query

15. Which method of the Session interface is used to save a new object in Hibernate?

  • A. save()
  • B. create()
  • C. persist()
  • D. insert()

16. Which Hibernate configuration property is used to specify the database connection URL?

  • A. hibernate.connection.url
  • B. hibernate.db.url
  • C. hibernate.jdbc.url
  • D. hibernate.database.url

17. What is the role of the Transaction interface in Hibernate?

  • A. To map entities to tables
  • B. To manage database connections
  • C. To provide database transaction support
  • D. To define database queries

18. In Hibernate, what is the significance of the session.beginTransaction() method?

  • A. It begins a database transaction
  • B. It starts the Hibernate session
  • C. It commits the database transaction
  • D. It opens a new database connection

19. Which of the following annotations in Hibernate maps a Java class to a database table?

  • A. @Entity
  • B. @Table
  • C. @Column
  • D. @Id

20. What does the Hibernate Query Language (HQL) allow you to do?

  • A. Execute SQL queries directly
  • B. Perform operations on database tables using object-oriented syntax
  • C. Generate SQL queries dynamically
  • D. Map Java objects to database tables

21. How do you perform a basic SELECT query in HQL?

  • A. SELECT * FROM entity
  • B. FROM Entity
  • C. SELECT entity FROM Entity
  • D. SELECT entity

22. In HQL, which keyword is used to fetch all objects of a particular entity?

  • A. ALL
  • B. FROM
  • C. SELECT
  • D. FETCH

23. How would you write a WHERE clause in an HQL query?

  • A. WHERE column = value
  • B. WHERE entity.property = value
  • C. WHERE entity = value
  • D. WHERE value IN entity

24. Which of the following methods is used to execute an HQL query in Hibernate?

  • A. executeQuery()
  • B. createQuery()
  • C. select()
  • D. getQuery()

25. What is the use of Query.setParameter() method in Hibernate?

  • A. To set query parameters dynamically
  • B. To limit the number of records returned
  • C. To define the query structure
  • D. To execute the query

26. In Hibernate, what does the distinct keyword do in an HQL query?

  • A. It fetches unique records only
  • B. It limits the number of records returned
  • C. It sorts the result
  • D. It creates an alias for the result

27. Which Hibernate query method is used for batch processing?

  • A. executeUpdate()
  • B. setParameter()
  • C. createNativeQuery()
  • D. executeQuery()

28. What is the advantage of using HQL over SQL in Hibernate?

  • A. HQL allows object-oriented queries
  • B. HQL works only with MySQL databases
  • C. HQL is easier to learn than SQL
  • D. HQL eliminates the need for ORM mapping

29. How can you perform a JOIN operation in HQL?

  • A. Using the JOIN keyword
  • B. Using the INNER JOIN keyword
  • C. Using FETCH keyword
  • D. Using the FROM keyword

30. What does the @Transient annotation indicate in Hibernate?

  • A. A field should not be mapped to a database column
  • B. A field is a primary key
  • C. A field should be included in the query
  • D. A field should be automatically generated

Answer Key

QNoAnswer
1B. To provide Object-Relational Mapping (ORM) to map Java objects to database tables
2B. Supports Object-Relational Mapping (ORM)
3A. Hibernate Query Language (HQL)
4A. hibernate.cfg.xml
5A. @Entity
6B. To create and manage Hibernate sessions
7A. Session
8B. @Id
9C. H2Dialect
10A. Use the hibernate.cfg.xml file
11D. All of the above
12A. Using the @OneToMany annotation
13C. @ManyToMany
14A. To automatically generate values for the primary key
15A. save()
16A. hibernate.connection.url
17C. To provide database transaction support
18A. It begins a database transaction
19B. @Table
20B. Perform operations on database tables using object-oriented syntax
21C. SELECT entity FROM Entity
22B. FROM
23B. WHERE entity.property = value
24B. createQuery()
25A. To set query parameters dynamically
26A. It fetches unique records only
27A. executeUpdate()
28A. HQL allows object-oriented queries
29A. Using the JOIN keyword
30A. A field should not be mapped to a database column

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top