You are here
Home > Hibernate >

MCQ on JPA and Hibernate

MCQ on JPA and HibernateAs a Java developer, you must have enhanced your skills in service layer technologies, such as Core Java, Spring, Spring Boot, Microservices etc.  Undoubtedly, they are the most important for you. On the other hand, in order to work in an industry level project, you must have a good knowledge on data layer technologies, such as JPA or Hibernate or any ORM based technology. It is advisable that developers need much coding practice on ORM concepts rather than going through theoretical concepts again & again.

In this article, we will be doing practice on ‘JPA & Hibernate’ in the form of MCQs that are frequently asked in the interviews. Certainly, you must find this article beneficial whether it is an interview or a written test from these MCQ on JPA and Hibernate.

Who can get benefit from these questions?

1) Job seekers who are going to appear in a Java interview or written/online test.

2) Students and learners who want to practice their concepts and test the skills.

3) Experienced developers who want to revise their concepts before appearing in any interview.

4) All who want to appear in any online test or in an interview.

In summary, MCQ on JPA and Hibernate can benefit anyone who wants to improve their understanding of JPA and Hibernate concepts, whether they are job seekers, students, experienced developers, or any others.

MCQ on JPA and Hibernate

Q#1. What does ORM stand for?

(a) Object Relational Model
(b) Object Resource Mapping
(c) Object Relational Mapping
(d) Object Reference Mapping

Answer: c) Object Relational Mapping

Explanation: ORM stands for Object Relational Mapping. It is a technique that offers developers to map objects from the application code to relational database tables.

Q#2. Which of the following technologies is not an ORM framework?

(a) Hibernate
(b) TopLink
(c) MyBatis
(d) Gradle

Answer: d) Gradle

Explanation: Hibernate, TopLink, and MyBatis (formally known as iBatis) are the ORM frameworks. Gradle is a build tool like Maven.

Q#3. What is the purpose of Hibernate in Java applications?

(a) To achieve database connections
(b) To simplify database querying using ORM technique
(c) To manage user authentication
(d) To increase application performance

Answer: b) To simplify database querying using ORM technique

Explanation: Hibernate simplifies database querying by providing an object-oriented approach to interact with the database. It reduces the need for writing complicated SQL queries and controls the database operations behind the scenes.

Q#4. Bob is working in a hibernate based project. He has two entities Employee and Address with the relation Employee  ‘HAS-A’  Address. He has a requirement where in he wants to delete address as well on deletion of Employee. Which of the following Cascade Type should he use?

(a) CascadeType.REMOVE
(b) CascadeType.MERGE
(c) CascadeType.DELETE
(d) CascadeType.REFRESH

Answer: c) CascadeType.DELETE

Explanation: CascadeType.REMOVE and CascadeType.DELETE both propagate the delete operation from parent to child entity. CascadeType.REMOVE is specific to JPA, whereas CascadeType.DELETE is specific to Hibernate.

Q#5. Which of the following is a feature of Hibernate?

(a) Automatic database schema generation
(b) Automatic memory management
(c) Dynamic class loading
(d) Garbage collection

Answer: a) Automatic database schema generation

Explanation: Based on the provided mapping metadata, Hibernate can automatically produce the database schema. It eliminates the need to create database tables manually and helps in preserving the database structure.

Q#6. What does JPA stand for?

(a) Java Persistence API
(b) Java Programming Architecture
(c) Java Persistence Algorithm
(d) Java Persistence Architecture

Answer: a) Java Persistence API

Explanation: JPA stands for Java Persistence API. It is a Java specification that offers a standardized way to interact with databases using ORM technology.

Q#7. Which of the following is not a key component of JPA?

(a) Entity Manager
(b) Persistence Unit
(c) Query Language
(d) Session Factory

Answer: d) Session Factory

Explanation: In JPA, the EntityManager does the job of Session Factory. Session Factory is a component specific to Hibernate, and not a part of the JPA specification.

Q#8. What is the main role of the EntityManager in JPA?

(a) To achieve the database connections
(b) To handle entity persistence and retrieval
(c) To offer SQL query execution
(d) To manage the user session

Answer: b) To handle entity persistence and retrieval

Explanation: The EntityManager is responsible for managing entity persistence and retrieval operations in JPA. It gives methods to create, read, update, and delete entities from the database.

Q#9. Which of the following is incorrect about @Table annotation in JPA?

(a) It is used to create a user defined table in JPA.
(b) It is used in conjunction with @Entity annotation.
(c) It creates user defined columns against each table.
(d) It offers a name attribute to specify a table name.

Answer: c) It creates user defined columns against each table.

Explanation: The @Table annotation is used to create a user defined table in JPA with the help of ‘name’ attribute and used in combination with the @Entity annotation.

Q#10. What is a persistence unit in JPA?

(a) A set of all database tables
(b) A logical group of related entities
(c) A container-managed entity manager
(d) A configuration file for all database tables

Answer: b) A logical group of related entities

Explanation: A persistence unit in JPA is a logical group of related entities. It defines the set of entities that can be managed as a group.

Q#11. Which of the following is not a valid statement about JPQL (Jakarta Persistence Query Language)?

(a) It can be used with any type of database such as MySQL, Oracle (database independent).
(b) It can perform join operations.
(c) It can't perform an aggregate function with sorting and grouping clauses.
(d) It can provide single and multiple value result types.

Answer: c) It can’t perform an aggregate function with sorting and grouping clauses.

Explanation: The JPQL (Jakarta Persistence Query Language: Formerly Java Persistence Query Language) is an object-oriented query language. It is used to perform database operations on persistent objects. JPQL uses entity object model to operate the SQL queries rather than database tables. JPA converts JPQL into SQL. It can also perform an aggregate function with sorting and grouping clauses.

Q#12. What is incorrect about a many-to-many relationship?

(a) A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table.
(b) A many-to-many relationship exists between customers and products if customers can purchase various products, and products can be purchased by many customers.
(c) Many-to-Many relationships are lazy by default.
(d) A many-to-many relationship can't produce the data redundancy problem.

Answer: d) A many-to-many relationship can’t produce the data redundancy problem.

Explanation: Many-to-many relationships are not favorably recommended in a relational database because it can create many problems, such as Data redundancy, data insertion, deletion, and updating difficulties.

Q#13. Which of the following is supported by Hibernate?

(a) HQL
(b) SQL
(c) JPQL
(d) All of the above

Answer: d) All of the above

Explanation: Hibernate supports several query languages. HQL (Hibernate Query Language) is specific to Hibernate. SQL (Structured Query Language) is the standard database query language. JPQL (Jakarta Persistence Query Language) is the query language defined in the JPA.

Q#14. Which is incorrect about the owning side in the context of ORM technique?

(a) It is one of the two sides of a bidirectional relationship.
(b) The owning side initiates the creation of the relationship to the database.
(c) Generally, this is the side where the foreign key resides.
(d) Generally, this is the side where the primary key resides.

Answer: d) Generally, this is the side where the primary key resides.

Explanation: In fact, owning side and inverse side are the technical terminologies of the ORM technology. They are not the concepts of participating entities. In fact, they are the two sides of a bidirectional relationship. The owning side initiates the creation of the relationship to the database. Generally, this is the side where the foreign key resides. For more details on this, kindly go through the topic ‘owning side & inverse side‘.

Q#15. What is the intent of the ‘mappedBy’ attribute in a bidirectional association mapping?

(a) It specifies the mapped column name
(b) It defines the target entity class
(c) It determines the primary key relationship
(d) It contains the name of the association-field on the owning side

Answer: d) It represents the owning side of the relationship

Explanation: In a bidirectional association mapping, the mappedBy attribute is used to indicate the name of the association-field on the owning side. It specifies the property in the target entity that maps back to the owning entity.

Q#16. Which operator will you use for named parameter binding in JPQL (Jakarta Persistence Query Language) ?

(a) =
(b) :=
(c) ?
(d) :

Answer: d) :

Explanation: The colon (:) operator is used for named parameter binding in JPQL. When constructing a JPQL query, we can use named parameters prefixed with a colon to represent values that will be provided at runtime.

Q#17. What is the purpose of the Hibernate SessionFactory?

(a) Performs CRUD-based operations.
(b) Provides methods such as save, delete and update, retrieve.
(c) Responsible for the creation of Session objects.
(d) Manages the transactions.

Answer: c) Responsible for the creation of Session objects.

Explanation: The Hibernate SessionFactory is responsible for the creation of Session objects. It is used to create Hibernate Session objects that can perform CRUD-based operations.

Q#18. Which statement is correct about Hibernate and JPA?

(a) Hibernate is a particular implementation of JPA.
(b) Hibernate is used for database connection management, while JPA is used for entity mapping.
(c) Hibernate supports Java language features beyond the JPA.
(d) Hibernate and JPA are two different ORM frameworks for different programming languages.

Answer: a) Hibernate is a particular implementation of JPA.

Explanation: Hibernate is a particular implementation of the JPA specification. JPA provides a standard set of APIs and annotations for object-relational mapping, while Hibernate is one of the implementations that follow to this specification.

Q#19. Which of the following annotations is used to mark a field as the primary key in JPA?

(a) @Id
(b) @PrimaryKey
(c) @Key
(d) @Primary

Answer: a) @Id

Explanation: The @Id annotation is used to mark a field as the primary key in JPA. It indicates that the corresponding attribute is the unique identifier for the entity.

Q#20. Which of the following type can’t be a primary key field in JPA?

(a) java.sql.Date
(b) Any primitive wrapper
(c) java.math.BigInteger
(d) java.util.Collection

Answer: d) java.util.Collection

Explanation: A standard primary key consists of a single Java field which maps to a single table column. Collection type primary key is not possible.

Q#21. Which of the following is incorrect about FetchType in JPA?

(a) FetchType is an enumerated type in the JPA Specification.
(b) It specifies whether the field or property should be lazily loaded or eagerly loaded.
(c) By default, @OneToMany associations use the FetchType.EAGER strategy.
(d) It can be specified for associations at the time of mapping the association.

Answer: c) By default, @OneToMany associations use the FetchType.EAGER strategy.

Explanation: By default, @OneToMany and @ManyToMany associations use the FetchType.LAZY strategy while the @OneToOne and @ManyToOne use the FetchType.EAGER strategy.

Q#22. Which of the following annotations is used to specify the name of the table associated with an entity in Hibernate?

(a) @TableName
(b) @Table
(c) @EntityTable
(d) @Entity

Answer: b) @Table

Explanation: The @Table annotation is used to specify the name of the table associated with an entity in Hibernate. It allows us to customize the table name and other properties of the table.

Q#23. What is the purpose of the @GeneratedValue annotation in Hibernate?

(a) To specify the strategy for primary key generation
(b) To define the mapping between entities and database tables
(c) To indicate the relationship between two entities
(d) To create the caching configuration for entities

Answer: a) To specify the generation strategy for primary keys

Explanation: The @GeneratedValue annotation in Hibernate is used to specify the strategy for primary key generation. It allows you to define how the primary key values for entities should be automatically generated.

Q#24. What is the purpose of the Hibernate dialect?

(a) To define the mapping metadata for entities
(b) To provide the connection pooling mechanism
(c) To specify the database-specific SQL syntax
(d) To configure the second-level cache

Answer: c) To specify the database-specific SQL syntax

Explanation: The Hibernate dialect is used to specify the database-specific SQL syntax and behavior. It provides the necessary translations and adaptations to generate the appropriate SQL statements for a particular database system.

Q#25. Which of the following is a disadvantage of using Hibernate?

(a) Increase in productivity of developers
(b) Database Independency
(c) Performance overhead due to object-relational mapping
(d) Improved maintainability and code readability

Answer: c) Performance overhead due to object-relational mapping

Explanation: One of the disadvantages of using Hibernate is the possible performance overhead due to the object-relational mapping. The additional processing required for mapping objects to database tables can introduce some performance overhead.

Q#26. How can you create a native SQL query in Hibernate?

(a) Using the createNativeQuery() method
(b) Using the createSQLQuery() method
(c) Using the executeQuery() method
(d) Using the executeNativeQuery() method

Answer: b) Using the createSQLQuery() method

Explanation: To create a native SQL query in Hibernate, we can use the createSQLQuery() method. This method allows you to create SQL queries directly without any Hibernate-specific query language.

Q#27. What is the purpose of the @EmbeddedId annotation in Hibernate?

(a) It specifies the primary key for an entity using an embedded object.
(b) It indicates that the entity is Embeddable.
(c) It defines the relationship between two entities.
(d) It configures the caching strategy for an entity.

Answer: a) It specifies the primary key for an entity using an embedded object.

Explanation: The @EmbeddedId annotation is used in Hibernate to specify the primary key for an entity using an embedded object. It allows you to define a composite primary key by embedding multiple attributes within a separate class.

Q#28. In order to make an entity eligible for second-level caching, which annotation will you use in Hibernate?

(a) @Cacheable 
(b) @Cache 
(c) @SecondLevelCache
(d) @CacheableEntity 

Answer: b) @Cache

Explanation: To configure a second-level cache for an entity in Hibernate, we can use the @org.hibernate.annotations.Cache annotation. This annotation allows us to specify the caching settings for the entity, such as the cache region and cache concurrency strategy etc.

Q#29. When should you use @Temporal annotation?

(a) If you have fields with type 'java.util.Collection'.
(b) If you want to create a temporary field.
(c) If you have fields with type 'java.util.Date' or 'java.util.Calendar'.
(d) If you want a field to be used in the persistence context.

Answer: c) If you have fields with type ‘java.util.Date’ or ‘java.util.Calendar’.

Explanation: If you are still using java.util.Date or java.util.Calendar as your field types, you need to annotate the field with @Temporal. Using this annotation, you can define if the attribute shall be mapped as an SQL DATE, TIME, or TIMESTAMP.

Q#30. How can you get an entity by its primary key in JPA?

(a) By calling find() method of the EntityManager
(b) By calling get() method of the EntityManager
(c) By calling load() method of the EntityManager
(d) By calling key() method of the EntityManager

Answer: a) By calling find() method of the EntityManager

Explanation: The find() method of the EntityManager in JPA is used to retrieve an entity by its primary key. It takes the entity class as the first argument and the primary key value as the second arguments and returns the corresponding entity if found.

Q#31. What is the purpose of the CascadeType.PERSIST option in JPA?

(a) It defines that entity deletion should be cascaded to related entities.
(b) It defines that entity updates should be cascaded to related entities.
(c) It defines that entity insertion should be cascaded to related entities.
(d) All of the above

Answer: c) It defines that entity insertion should be cascaded to related entities.

Explanation: The CascadeType.PERSIST option in JPA specifies that entity insertion should be cascaded to related entities. It means that when an entity is persisted, any associated entities will also be persisted.

Q#32. What is the main usage of the @JoinColumn annotation in JPA?

(a) To specify the database table and column name
(b) To specify a column for joining an entity association or element collection
(c) To define the primary key of a table in the database
(d) To configure the database join column 

Answer: b) To specify a column for joining an entity association or element collection

Explanation: The @JoinColumn annotation in JPA is used to specify the column name for joining an entity association or element collection. It allows us to customize the name of the column that holds the foreign key reference.

Q#33. What does the following property mean?

             <property name=”hbm2ddl.auto”>create</property>
(a) Update the database schema without re-creating it on startup
(b) Drop and re-create the database schema on startup
(c) Update the database schema on startup
(d) All of the above

Answer: b) Drop and re-create the database schema on startup

Explanation: To enable automatic table creation for entities in Hibernate, we can set the hibernate.hbm2ddl.auto property to “create” in the configuration. The ‘create’ option instructs Hibernate to drop the database schema and recreate it on startup.

Q#34. Which one is incorrect about Criteria Queries?

(a) Criteria queries are more flexible as compared to HQL and JPQL.
(b) Like HQL and JPQL, Criteria queries provide native query support.
(c) Criteria queries provide better support for writing dynamic queries.
(d) It is easier to detect errors in the Criteria API during the compile time.

Answer: b) Like HQL and JPQL, Criteria queries provide native query support.

Explanation: HQL and JPQL provide native query support that isn’t possible with the Criteria queries.

Q#35. Which of the following is incorrect about the Hibernate HQL (Hibernate Query Language)?

(a) HQL queries are translated by Hibernate into traditional SQL queries.
(b) It supports object-oriented queries.
(c) It works with persistent objects and their properties.
(d) It works on tables and columns.

Answer: d) It works on tables and columns.

Explanation: Hibernate Query Language (HQL) is an object-oriented query language. Unlike SQL, which operates on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into traditional SQL queries.

♥ You may also attempt MCQs on other topics in Java by visiting our Quizzes section.

 

 

Leave a Reply


Top