June 4, 2019

Srikaanth

Gartner Advanced Java Interview Questions Answers

Gartner Most Frequently Asked Latest Advanced Java Interview Questions Answers

What Is Lazy Initialization In Hibernate?

The delaying the object creation or calculating a value or some process until the first time it is needed. The retrieval of particular information only at the time when the object is accessed, is lazy initialization in hibernate. A scenario for lazy initialization is

When the field creation is expensive, a field may or may not be invoked.

In this scenario the creation of a field can be deferred until the actual moment is arise to use it. The performance is increased using this technique, by avoiding unnecessary creation of objects which is expensive and consumes the memory space.

What Is Lazy Fetching In Hibernate?

Lazy setting decides whether to load child objects while loading the Parent Object.
This can be done by a setting in hibernate mapping file of the parent class.Lazy = true.
By default the lazy loading of the child objects is true.

What Is The Difference Between Sorted And Ordered Collection In Hibernate?

Sorted Collection
The sorted collection is a collection that is sorted using the Java collections framework. The sorting is done in the memory of JVM that is running hibernate, soon after reading the data from the database using Java Comparator.

The less the collection the more the efficient of sorting.

Ordered Collection
The order collections will also sorts a collection by using the order by clause for the results fetched.

The more the collection, the more efficient of sorting.

Explain The Advantages And Disadvantages Of Detached Objects.

Advantages
Detached objects passing can be done across layers upto the presentation layer without using Data Transfer Objects.
At the time of using long transactions by the user which needs long think-time, it is suggested to split these transactions into some transactions. The detached objects get modified apart from the transaction scope which then can be re-attached to a new transaction.

Disadvantages
The usage of detached objects are cumbersome and cryptic. It is suggested not to be cluttered with the session, if possible.
It is recommended to use DataTransferObjects and DomainObjects that is used to maintain separation between the user interfaces and the Service.

What Is Hibernate Query Language (hql)?

Hibernate Query Language is designed for data management using Hibernate technology. It is completely object oriented and hence has notions like inheritance, polymorphism and abstraction. The queries are case-sensitive. This has an exception for Java classes and properties. The query operations are through objects. HQL acts as a bridge between Objects and RDBMS.

Explain The General Flow Of Hibernate Communication With Rdbms.

The general flow of Hibernate communication with RDBMS is

The Hibernate configuration is to be loaded and creation of configuration object is done. The mapping of all hbm files will be performed automatically.
Creation of session factory from the configuration object.
Obtain a session from the session factory.
Creation of HQL Query .
Execution of the query in order to get the list of containing java objects.

Explain The Role Of Session Interface In Hibernate.

In hibernate, the Session interface wraps a JDBC connection, holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier and is a factory for Transaction.
Session session = sessionFactory.openSession();
The Session interface is the primary interface used by Hibernate applications.
It is a single-threaded, short-lived object representing a conversation between the application and the persistent store.
It allows you to create query objects to retrieve persistent objects.
Gartner Most Frequently Asked Latest Advanced Java Interview Questions Answers
Gartner Most Frequently Asked Latest Advanced Java Interview Questions Answers

State The Role Of Sessionfactory Interface Plays In Hibernate.

An application obtains Session instances from a SessionFactory which is typically single for the whole application created during its initialization.
The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime.
It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
SessionFactory sessionFactory = configuration.buildSessionFactory();

What Is The Difference Between Merge And Update?

update () : When the session does not contain an persistent instance with the same identifier, and if it is sure use update for the data persistence in hibernate.

merge (): Irrespective of the state of a session, if there is a need to save the modifications at any given time, use merge().

What Is The Advantage Of Hibernate Over Jdbc?

The advantages of Hibernate over JDBC are
Hibernate code will work well for all databases, for ex: Oracle,MySQL, etc. where as JDBC is database specific.
No knowledge of SQL is needed because Hibernate is a set of objects and a table is treated as an object, where as to work with JDBC, one need to know SQL.
Query tuning is not required in Hibernate. The query tuning is automatic in hibernate by using criteria queries, and the result of performance is at its best. Where as in JDBC the query tuning is to be done by the database authors.
With the support of cache of hibernate, the data can be placed in the cache for better performance. Where as in JDBC the java cache is to be implemented.

Why Hibernate Is Advantageous Over Entity Beans & Jdbc?

An entity bean always works under the EJB container, which allows reusing of the object external to the container. An object can not be detached in entity beans and in hibernate detached objects are supported.

Hibernate is not database dependent where as JDBC is database dependent. Query tuning is not needed for hibernate as JDBC is needed. Data can be placed in multiple cache which is supported by hibernate, whereas in JDBC the cache is to be implemented.

Explain The Main Difference Between Entity Beans And Hibernate.

Entity beans are to be implemented by containers, classes, descriptors. Hibernate is just a tool that quickly persist the object tree to a class hierarchy in a database and without using a single SQL statement. The inheritance and polymorphism is quite simply implemented in hibernate which is out of the box of EJB and a big drawback.

Explain The Difference Between Hibernate And Spring.

Hibernate is an ORM tool for data persistency. Spring is a framework for enterprise applications. Spring supports hibernate and provides the different classes which are templates that contains the common code.

How Will You Configure Hibernate?

The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the SessionFactory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service.

" hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to configure the hibernate sevice (connection driver class, connection URL, connection username, connection password, dialect etc). If both files are present in the classpath then hibernate.cfg.xml file overrides the settings found in the hibernate.properties file.

" Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational database. It is the best practice to store each object in an individual mapping file (i.e mapping file per class) because storing large number of persistent classes into one mapping file can be difficult to manage and maintain. The naming convention is to use the same name as the persistent (POJO) class name. For example Account.class will have a mapping file named Account.hbm.xml. Alternatively hibernate annotations can be used as part of your persistent class code instead of the *.hbm.xml files.

What Is A Sessionfactory? Is It A Thread-safe Object?

SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.
SessionFactory sessionFactory = new Configuration().configure().buildSessionfactory();

What Is A Session? Can You Share A Session Object Between Different Theads?

Session is a light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required). To avoid creating too many sessions ThreadLocal class can be used as shown below to get the current session no matter how many times you make call to the currentSession() method.
&
public class HibernateUtil {
&
public static final ThreadLocal local = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session session = (Session) local.get();
//open a new session if this thread has no session
if(session == null) {
session = sessionFactory.openSession();
local.set(session);
}
return session;
}
}
It is also vital that you close your session after your unit of work completes.

What Are The Benefits Of Detached Objects?

Detached objects can be passed across layers all the way up to the presentation layer without having to use any DTOs (Data Transfer Objects). You can later on re-attach the detached objects to another session.

What Are The Pros And Cons Of Detached Objects?

Pros

" When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and later on re-attached to a new transaction via another session.

Cons

" In general, working with detached objects is quite cumbersome, and better to not clutter up the session with them if possible. It is better to discard them and re-fetch them on subsequent requests. This approach is not only more portable but also more efficient because - the objects hang around in Hibernate's cache anyway.
" Also from pure rich domain driven design perspective it is recommended to use DTOs (DataTransferObjects) and DOs (DomainObjects) to maintain the separation between Service and UI tiers.

How Does Hibernate Distinguish Between Transient (i.e. Newly Instantiated) And Detached Objects?

Hibernate uses the version property, if there is one.

If not uses the identifier value. No identifier value means a new object. This does work only for Hibernate managed surrogate keys. Does not work for natural keys and assigned (i.e. not managed by Hibernate) surrogate keys.

Write your own strategy with Interceptor.isUnsaved().

What Is The Difference Between The Session.get() Method And The Session.load() Method?

Both the session.get(..) and session.load() methods create a persistent object by loading the required object from the database. But if there was not such object in the database then the method session.load(..) throws an exception whereas session.get(&) returns null.

What Is The Difference Between The Session.update() Method And The Session.lock() Method?

Both of these methods and saveOrUpdate() method are intended for reattaching a detached object. The session.lock() method simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object. It is the best practice to use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure that the detached object is in sync with your detached object or if it does not matter because you will be overwriting all the columns that would have changed later on within the same transaction. Note: When you reattach detached objects you need to make sure that the dependent objects are reatched as well.

How Would You Reatach Detached Objects To A Session When The Same Object Has Already Been Loaded Into The Session?

You can use the session.merge() method call.

What Are The General Considerations Or Best Practices For Defining Your Hibernate Persistent Classes?

1.You must have a default no-argument constructor for your persistent classes and there should be getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance variables.
2.You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object.
3. It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster.
4.The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects.
5.Use XDoclet tags for generating your *.hbm.xml files or Annotations (JDK 1.5 onwards), which are less verbose than *.hbm.xml files.

How Will You Communicate Between Two Applets?

The simplest method is to use the static variables of a shared class since there's only one instance of the class and hence only one copy of its static variables. A slightly more reliable method relies on the fact that all the applets on a given page share the same AppletContext. We obtain this applet context as follows

AppletContext ac = getAppletContext();

AppletContext provides applets with methods such as getApplet(name), getApplets(),getAudioClip, getImage, showDocument and showStatus().

https://mytecbooks.blogspot.com/2019/06/gartner-advanced-java-interview.html
Subscribe to get more Posts :