How can we get access to O/R mapping information such as table and column names at runtime?
This information is available via the Configuration object. For example, entity mappings may be obtained using Configuration.getClassMapping(). It is even possible to manipulate this metamodel at runtime and then build a new SessionFactory.Reply
Does Hibernate implement its functionality using a minimal number of database queries?
Hibernate can make certain optimizations implement its functionality using a minimal number of database queries:
* Caching objects
* Executing SQL statement when needed.
* Never updating unmodified objects.
* Efficient Collection Handling.
* Updating only the modified columns.
* Outer join fetching.
* Lazy collection initialization.
* Lazy object initialization. Reply
How does Hibernate distinguish between transient (i.e. newly instantiated) and detached objects?
The EmptyInterceptor class provides the isTransient() method to check whether the object is transient or deteched. Method signature:
java.lang.Boolean isTransient(java.lang.Object obj) Reply
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. Reply
Explain the various object states in Hibernate?
Hibernate defines and supports the following object states:
* Transient :- an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned.
* Persistent :- a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session.
* Detached :- a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it persistent again.Reply
What are the types of inheritence models in Hibernate?
There are three types of inheritance mapping in hibernate:
1. Table per concrete class with unions.
2. Table per class hierarchy.
3. Table per subclass.Reply
How will you obtain a session factory?
After all mappings have been parsed by the org.hibernate.cfg.Configuration, the application must obtain a factory for org.hibernate.Session instances. This factory is intended to be shared by all application threads:
SessionFactory sessions = cfg.buildSessionFactory();
Hibernate does allow your application to instantiate more than one org.hibernate.SessionFactory. This is useful if you are using more than one database.
The org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.Reply
What is the general flow of Hibernate communication with RDBMS?
The general flow of Hibernate communication with RDBMS is:
* Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files.
* Create session factory from configuration object.
* Get one session from this session factory
* Create HQL Query.
* Execute query to get list containing Java objects.
How do you map Java Objects with Database tables?
First we need to write Java domain objects (beans with setter and getter). The variables should be same as database columns. Write hbm.xml, where we map java class to table and database columns to Java class variables.
<class name="com.User" table="user">
<property column="username" length="255"
name="userName" not-null="true" type="java.lang.String"/>
<property column="userpassword" length="255"
name="userPassword" not-null="true" type="java.lang.String"/>