10:16 am - Tuesday January 22, 2019

Hibernate Interview Questions and Answers – Part 8

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

1. Types of locks

  • Read lock

  • Write lock

  • Execute lock

  • Fetch lock

  • Save lock

2. What is 2-phase lock?

  • Read only transaction

  • Update transaction

3. What are the design patterns used in hibernate?

  • Composite design

  • DAO Design Pattern

  • Abstract FactoryMVC

  • Data Transfer

  • Proxy

  • facade

4. Whether POJO is java bean or not?

S. POJO is java bean

POJO stands for plain old java objects. These are just basic JavaBeans that have defined setter and getter methods for all the properties that are there in that bean. Besides they can also have some business logic related to that property. Hibernate applications works efficiently with POJOs rather than simple java classes.

5. Please draw the object life cycle in hibernate?

Transient objects do not (yet) have any association with the database. they act like any normal Java object and are not saved to the database. When the last reference to a transient object is lost, the object itself is lost and is (eventually) garbage collected. There is no connection between transactions and such objects: commits and rollbacks have no effects on them. They can be turned into persistent objects via one of the save method calls if the Session object or by adding a reference from a persistent object to this object.

Persistent objects do have an association with the database. They are always associated with a persistence manager, i.e., a Session object and they always participate in a transaction. Actual updates of a database from the persistent object may occur at any time between when the object is updated to the end of the transaction: it does not necessarily happen immediately. However, this feature, which allows important optimizations in database interactions, is essentially invisible to the programmer. For example, one place where one might expect to notice the difference between the in-memory persistent object and the database version is at the point of executing a query. In such a case, Hibernate will, if necessary, synchronize any dirty objects with the database (i.e., save them) in order to ensure that the query returns the correct results.

A persistent object has a primary key value set, whether or not it has been actually saved to the database yet.

Calling the delete method of the Session object on a persistent object will cause its removal from the database and will make it transient.

Detached objects are objects that were persistent but no longer have a connection to a Session object (usually because you have closed the session). Such an object contains data that was synchronized with the database at the time that the session was closed, but, since then, the database may have changed; with the result that this object is now stale.

6. How transaction management is done in hibernate?

Transaction simply means a unit of work, which is atomic. If only one step fails, then the whole unit of work fails. When we consider database, Transaction groups a set of statements/commands which gets committed together. If a single statement fails, whole work will be rolled back. Transactions can be described using ACID criteria.

ACID means:

A – Atomicity: In a Transaction If only one step fails, the whole unit of work must fail. This is known as atomicity

C – Consistency: The Transactions operate on Consistent data. I.e. This data is hidden from other concurrently running transactions. The data is clean and consistent.

I – Isolation: It means when a transaction is being executed, it will not be visible to other concurrently running transactions. This means, any changes done to the existing data during a transaction, before transaction finishes, is not visible to other active transactions. They work on consistent data.

D – Durable: Also the changes done in a transaction are durable. Even if the server/system fails after a transaction, the changes done by a successful transaction are permanent/persistent.

So database transactions involve a set of SQL statements, which either succeeds or fails. If anyone of the statement fails in between, the execution of subsequent statements are aborted and all changes done by the previous SQL statements will be rolled back. In complex applications, which involve different database actions, one should set boundaries for a transaction. This means the beginning and end of the transaction should be decided. This is called Transaction demarcation. If an error occurs (either while executing operations or when committing the transaction), you have to roll back the transaction to leave the data in a consistent state.

This can be done in 2 ways.

In a programmatic manner by explicitly setting boundaries in code or using JTA API.

Setting Transaction boundaries by declarative manner, specifying transaction boundaries to managed containers like EJB container

7. Difference between the association and component mapping?

Association mapping refers to a many-to-one or one-to-one relationship which will be mapped by using another class which you have mapped in Hibernate (also called an “entity”). The associated object has its own lifecycle and is simply related to the first object.

Component mapping refers to mapping a class (or collection of classes) whose lifecycle is bound tightly to the parent. This is also called “composition” in the strict definition of the word in object-oriented programming. Basically, if you delete the parent object the child object should also be deleted; it also cannot exist on its own without a parent.

8. Why hibernate is called as lazy invocation?

Lazy setting decides whether to load child objects while loading the Parent Object. You need to specify parent class. Lazy = true in hibernate mapping file. By default the lazy loading of the child objects is true. This makes sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild() method on parent. In this case, hibernate issues a fresh database call to load the child when getChild() is actually called on the Parent object. But in some cases, you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load the child when parent is loaded from the database. Examples: Address child of User class can be made lazy if it is not required frequently. But you may need to load the Author object for Book parent whenever you deal with the book for online bookshop.

Hibernate does not support lazy initialization for detached objects. Access to a lazy association outside of the context of an open Hibernate session will result in an exception.

9. What is hibernate tuning?

The key to obtaining better performance in any hibernate application is to employ SQL Optimization, session management, and Data caching

10. Define hibernate statistics?

We’ve been doing a lot of Hibernate work at Terracotta recently and that naturally includes a fair amount of performance testing. In Hibernate, you can grab those stats using the Statistics object for SessionFactory via getStatistics(). There are all sorts of tasty morsels inside this class to get factory-wide counts and per-entity/query/cache/etc stats. Cool stuff.

We noticed however while doing some perf testing that the code inside the StatisticsImpl is a bit problematic from a concurrency point of view. The basic gist of the class is just a set of stat counters. I can simplify the pattern to this without much loss of detail:


public class StatisticsImpl implements Statistics, StatisticsImplementor {

private long queryExecutionCount;

public synchronized void queryExecuted(String hql, int rows, long time) {

queryExecutionCount++;

// ... bunch of other stat collection

}

public long getQueryExecutionCount() {

return queryExecutionCount;

}

public synchronized void clear() {

queryExecutionCount = 0;

// ... clear all other stats

}

}


 PARTS : >  1   2   3    4   5   6   7   8   9  10  11  12

Filed in: Z-A Interview Questions

No comments yet.

Leave a Reply