Hibernate Q&A

[vc_row][vc_column css_animation=”fadeInLeft”][vc_column_text css=”.vc_custom_1562932048995{background-color: #1e73be !important;}”]

Hibernate Interview Questions & Answers

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column css_animation=”fadeInLeft” width=”1/2″][vc_tta_accordion color=”peacoc” active_section=”1″][vc_tta_section title=”What’s Hibernate?” tab_id=”1562932119424-276670a3-5386″][vc_column_text]Hibernate is a popular framework of Java which allows an efficient Object Relational mapping using configuration files in XML format. After java objects mapping to database tables, database is used and handled using Java objects without writing complex database queries.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is ORM?” tab_id=”1562932119442-4748681c-bb36″][vc_column_text]ORM (Object Relational Mapping) is the fundamental concept of Hibernate framework which maps database tables with Java Objects and then provides various API’s to perform different types of operations on the data tables.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How properties of a class are mapped to the columns of a database table in Hibernate?” tab_id=”1562932135339-4b2bc098-d815″][vc_column_text]Mappings between class properties and table columns are specified in XML file as in the below example:[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the usage of Configuration Interface in hibernate?” tab_id=”1562932135891-08ff7668-3671″][vc_column_text]Configuration interface of hibernate framework is used to configure hibernate. It’s also used to bootstrap hibernate. Mapping documents of hibernate are located using this interface.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we use new custom interfaces to enhance functionality of built-in interfaces of hibernate?” tab_id=”1562932136460-cc83c1cc-5e26″][vc_column_text]We can use extension interfaces in order to add any required functionality which isn’t supported by built-in interfaces.[/vc_column_text][/vc_tta_section][vc_tta_section title=” Should all the mapping files of hibernate have .hbm.xml extension to work properly?” tab_id=”1562932137080-57d21931-1218″][vc_column_text]No, having .hbm.xml extension is a convention and not a requirement for hibernate mapping file names. We can have any extension for these mapping files.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How do we create session factory in hibernate?” tab_id=”1562932137651-3d84adbc-10d8″][vc_column_text]To create a session factory in hibernate, an object of configuration is created first which refers to the path of configuration file and then for that configuration, session factory is created as given in the example below:

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are POJOs and what’s their significance?” tab_id=”1562932138224-533876ed-5f72″][vc_column_text]POJOs( Plain Old Java Objects) are java beans with proper getter and setter methods for each and every properties.
Use of POJOs instead of simple java classes results in an efficient and well constructed code.[/vc_column_text][/vc_tta_section][vc_tta_section title=” What’s HQL?” tab_id=”1562932138883-4ff19ff3-4d14″][vc_column_text]HQL is the query language used in Hibernate which is an extension of SQL. HQL is very efficient, simple and flexible query language to do various type of operations on relational database without writing complex database queries.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we invoke stored procedures in hibernate?” tab_id=”1562932140232-ae7983e7-cf29″][vc_column_text]In hibernate we can execute stored procedures using code as below:

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is criteria API?” tab_id=”1562932140987-37eec860-5ba3″][vc_column_text]Criteria is a simple yet powerful API of hibernate which is used to retrieve entities through criteria object composition.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the benefits of using Hibernate template?” tab_id=”1562932141480-3555c1ad-fb5c”][vc_column_text]Following are some key benefits of using Hibernate template:
a. Session closing is automated.
b. Interaction with hibernate session is simplified.
c. Exception handling is automated.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we see hibernate generated SQL on console?” tab_id=”1562932142064-dd9f6a1c-5829″][vc_column_text]We need to add following in hibernate configuration file to enable viewing SQL on the console for debugging purposes:

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the two types of collections in hibernate?” tab_id=”1562932142739-88cf3208-5699″][vc_column_text]Following are the two types of collections in hibernate:
a. Sorted Collection
b. Order Collection[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the difference between session.save() and session.saveOrUpdate() methods in hibernate?” tab_id=”1562932143275-51f42a1c-0474″][vc_column_text]Sessionsave() method saves a record only if it’s unique with respect to its primary key and will fail to insert if primary key already exists in the table.
saveOrUpdate() method inserts a new record if primary key is unique and will update an existing record if primary key exists in the table already.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What the benefits are of hibernate over JDBC?” tab_id=”1562932143852-b63982a6-3601″][vc_column_text]a. Hibernate can be used seamlessly with any type of database as its database independent while in case of JDBC, developer has to write database specific queries.
b. Using hibernate, developer doesn’t need to be an expert of writing complex queries as HQL simplifies query writing process while in case of JDBC, its job of developer to write and tune queries.
c. In case of hibernate, there is no need to create connection pools as hibernate does all connection handling automatically while in case of JDBC, connection pools need to be created.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we get hibernate statistics?” tab_id=”1562932145144-7f388c33-94fa”][vc_column_text]We can get hibernate statistics using getStatistics() method of SessionFactory class as shown below:
SessionFactory.getStatistics()[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is transient instance state in Hibernate?” tab_id=”1562932145708-685d0072-1f51″][vc_column_text]If an instance is not associated with any persistent context and also, it has never been associated with any persistent context, then it’s said to be in transient state.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we reduce database write action times in Hibernate?” tab_id=”1562932146252-023a42a4-8f3b”][vc_column_text]Hibernate provides dirty checking feature which can be used to reduce database write times. Dirty checking feature of hibernate updates only those fields which require a change while keeps others unchanged.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the usage of callback interfaces in hibernate?” tab_id=”1562932146780-b762bca9-f454″][vc_column_text]Callback interfaces of hibernate are useful in receiving event notifications from objects. For example, when an object is loaded or deleted, an event is generated and notification is sent using callback interfaces.[/vc_column_text][/vc_tta_section][vc_tta_section title=”When an instance goes in detached state in hibernate?” tab_id=”1562932147324-1e6f1af5-c66c”][vc_column_text]When an instance was earlier associated with some persistent context (e.g. a table) and is no longer associated, it’s called to be in detached state.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What the four ORM levels are in hibernate?” tab_id=”1562932147900-85e933c8-59dd”][vc_column_text]Following are the four ORM levels in hibernate:
a. Pure Relational
b. Light Object Mapping
c. Medium Object Mapping
d. Full Object Mapping[/vc_column_text][/vc_tta_section][vc_tta_section title=” What’s transaction management in hibernate? How it works?” tab_id=”1562932149228-76d72824-c12e”][vc_column_text]Transaction management is the process of managing a set of statements or commands. In hibernate; transaction management is done by transaction interface as shown in below code:

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What the two methods are of hibernate configuration?” tab_id=”1562932149813-652552bb-562e”][vc_column_text]We can use any of the following two methods of hibernate configuration:
a. XML based configuration ( using hibernate.cfg.xml file)
b. Programmatic configuration ( Using code logic)[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is the default cache service of hibernate?” tab_id=”1562932150401-807ae144-f24e”][vc_column_text]Hibernate supports multiple cache services like EHCache, OSCache, SWARMCache and TreeCache and default cache service of hibernate is EHCache.[/vc_column_text][/vc_tta_section][/vc_tta_accordion][/vc_column][vc_column css_animation=”fadeInRight” width=”1/2″][vc_tta_accordion color=”peacoc” active_section=”1″][vc_tta_section title=”What are the two mapping associations used in hibernate?” tab_id=”1562932174259-5548a645-903a”][vc_column_text]In hibernate; we have following two types of mapping associations between entities:
a. One-to-One Association
b. Many-to-Many Association[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the usage of Hibernate QBC API?” tab_id=”1562932174282-20dd900b-bfda”][vc_column_text]Hibernate Query By Criteria (QBC) API is used to create queries by manipulation of criteria objects at runtime.[/vc_column_text][/vc_tta_section][vc_tta_section title=”In how many ways, objects can be fetched from database in hibernate?” tab_id=”1562932186400-4a971f48-431e”][vc_column_text]Hibernate provides following four ways to fetch objects from database:
a. Using HQL
b. Using identifier
c. Using Criteria API
d. Using Standard SQL[/vc_column_text][/vc_tta_section][vc_tta_section title=”How primary key is created by using hibernate?” tab_id=”1562932186972-e18aaf89-abae”][vc_column_text]Database primary key is specified in the configuration file hbm.xml. Generator can also be used to specify how primary key is being created in the database.
In the below example, deptId acts as primary key:

[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we reattach any detached objects in Hibernate?” tab_id=”1562932187481-10aec302-e479″][vc_column_text]Objects which have been detached and are no longer associated with any persistent entities can be reattached by calling session.merge() method of session class.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are different ways to disable hibernate second level cache?” tab_id=”1562932188011-83385f64-a2c1″][vc_column_text]Hibernate second level cache can be disabled using any of the following ways:
a. By setting use_second_level_cache as false.
b. By using CACHEMODE.IGNORE
c. Using cache provider as org.hibernate.cache.NoCacheProvider[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is ORM metadata?” tab_id=”1562932189176-d51ba9be-aef0″][vc_column_text]All the mapping between classes and tables, properties and columns, Java types and SQL types etc is defined in ORM metadata.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Which one is the default transaction factory in hibernate?” tab_id=”1562932189733-c1766038-e4b3″][vc_column_text]With hibernate 3.2, default transaction factory is JDBCTransactionFactory.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the role of JMX in hibernate?” tab_id=”1562932190236-b6863c82-6b05″][vc_column_text]Java Applications and components are managed in hibernate by a standard API called JMX API. JMX provides tools for development of efficient and robust distributed, web based solutions.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How can we bind hibernate session factory to JNDI ?” tab_id=”1562932190941-fe6364fb-4726″][vc_column_text]Hibernate session factory can be bound to JNDI by making configuration changes in hibernate.cfg file.[/vc_column_text][/vc_tta_section][vc_tta_section title=”In how many ways objects can be identified in Hibernate?” tab_id=”1562932191589-c5cb57d7-f489″][vc_column_text]Object identification can be done in hibernate in following three ways:
a. Using Object Identity: Using == operator.
b. Using Object Equality: Using equals() method.
c. Using database identity: Relational database objects can be identified if they represent same row.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What different fetching strategies are of hibernate?” tab_id=”1562932192084-6b831f4a-3ae5″][vc_column_text]Following fetching strategies are available in hibernate:
1. Join Fetching
2. Batch Fetching
3. Select Fetching
4. Sub-select Fetching[/vc_column_text][/vc_tta_section][vc_tta_section title=”How mapping of java objects is done with database tables?” tab_id=”1562932192598-324fa175-6bd6″][vc_column_text]To map java objects with database tables, we need to have Java beans properties names same as column names of a database table. Then mapping is provided in hbm.xml file as given below:

[/vc_column_text][/vc_tta_section][vc_tta_section title=” What are derived properties in hibernate?” tab_id=”1562932193108-0c3b3467-539a”][vc_column_text]Derived properties are those properties which are not mapped to any columns of a database table. Such properties are calculated at runtime by evaluation of any expressions.[/vc_column_text][/vc_tta_section][vc_tta_section title=” What is meant by a Named SQL Query in hibernate and how it’s used?” tab_id=”1562932194577-f6722449-c1ba”][vc_column_text]Named SQL queries are those queries which are defined in mapping file and are called as required anywhere.
For example, we can write a SQL query in our XML mapping file as follows:

Then this query can be called as follows:

[/vc_column_text][/vc_tta_section][vc_tta_section title=” What’s the difference between load() and get() method in hibernate?” tab_id=”1562932195092-010eecd4-5b14″][vc_column_text]Load() methods results in an exception if the required records isn’t found in the database while get() method returns null when records against the id isn’t found in the database.
So, ideally we should use Load() method only when we are sure about existence of records against an id.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the use of version property in hibernate?” tab_id=”1562932195685-967cd654-8f57″][vc_column_text]Version property is used in hibernate to know whether an object is in transient state or in detached state.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is attribute oriented programming?” tab_id=”1562932196233-c9e6804c-65c5″][vc_column_text]In Attribute oriented programming, a developer can add Meta data (attributes) in the java source code to add more significance in the code. For Java (hibernate), attribute oriented programming is enabled by an engine called XDoclet.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s the use of session.lock() in hibernate?” tab_id=”1562932196805-2cce7631-31f8″][vc_column_text]session.lock() method of session class is used to reattach an object which has been detached earlier. This method of reattaching doesn’t check for any data synchronization in database while reattaching the object and hence may lead to lack of synchronization in data.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Does hibernate support polymorphism?” tab_id=”1562932197454-5ce74163-3338″][vc_column_text]Yes, hibernate fully supports polymorphism. Polymorphism queries and polymorphism associations are supported in all mapping strategies of hibernate.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What the three inheritance models are of hibernate?” tab_id=”1562932198617-24e03eb4-fe43″][vc_column_text]Hibernate has following three inheritance models:
a. Tables Per Concrete Class
b. Table per class hierarchy
c. Table per sub-class[/vc_column_text][/vc_tta_section][vc_tta_section title=” How can we map the classes as immutable?” tab_id=”1562932199177-ab9df092-2c95″][vc_column_text]If we don’t want an application to update or delete objects of a class in hibernate, we can make the class as immutable by setting mutable=false[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s general hibernate flow using RDBMS?” tab_id=”1562932200014-c093b581-691c”][vc_column_text]General hibernate flow involving RDBMS is as follows:
a. Load configuration file and create object of configuration class.
b. Using configuration object, create sessionFactory object.
c. From sessionFactory, get one session.
d. Create HQL query.
e. Execute HQL query and get the results. Results will be in the form of a list.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is Light Object Mapping?” tab_id=”1562932200537-de10c7c3-df12″][vc_column_text]Light Object Mapping is one of the levels of ORM quality in which all entities are represented as classes and they are mapped manually.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What’s difference between managed associations and hibernate associations?” tab_id=”1562932201101-38ff1b4e-a1cc”][vc_column_text]Managed associations relate to container management persistence and are bi-directional while hibernate associations are unidirectional.

[/vc_column_text][/vc_tta_section][/vc_tta_accordion][/vc_column][/vc_row]

WhatsApp us