Hibernate's primary feature is mapping from Java classes to database tables (and from Java data types to SQL data types). Hibernate also provides data query and retrieval facilities. Hibernate generates the SQL calls and relieves the developer from manual result set handling and object conversion, keeping the application portable to all SQL databases, with database portability delivered at very little performance overhead.
Hibernate provides transparent persistence for Plain Old Java Objects (POJOs). The only strict requirement for a persistent class is a no-argument constructor, not compulsorily public. (Proper behavior in some applications also requires special attention to the equals() and hashCode() methods.)
Hibernate provides a dirty checking feature that avoids unnecessary database write actions by performing SQL updates only on the modified fields of persistent objects.
Hibernate can be used both in standalone Java applications and in Java EE applications using servlets or EJB session beans.
1. Hibernate 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. Transient instances will be destroyed by the garbage collector if the application doesn't hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).
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. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers don't execute manual UPDATE statements, or DELETE statements when an object should be made transient.
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 (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e. a unit of work from the point of view of the user.
2. How does Hibrnate maintain relations of RDBMS
Those are .1 to many, many to many, many to one.. relations.
These relational mappings are done by using the hibernates utilities such as list, set, bags, object...
3. How to Execute Stored procedure in Hibernate
1. Get java.sql.Connection from Hibernate Session and then play with that connection by call callablestatement.
Connection con = null;
try {
con = session.connection();
CallableStatement st = con
.prepareCall("{call your_sp(?,?)}");
st.registerOutParameter(2, Types.INTEGER);
st.setString(1, "some_Seq");
st.executeUpdate();
3. By this process you can write hibernate procedure:
< sql-query name="selectAllEmployees_SP" callable="true">
{ ? = call selectAllEmployees() }
4. You can declare it as Named queries in one of your mapping files and give unique name.
Call it in your code. Sample code is below.
SQLQuery sq = (SQLQuery) session.getNamedQuery("findSearchResultsListSP");
sq.addEntity("documentTypeCode", documentTypeCode);
List results = sq.list();
4. What are Callback interfaces?
These interfaces are used in the application to receive a notification when some object events occur. Like when an object is loaded, saved or deleted. There is no need to implement callbacks in hibernate applications, but they?re useful for implementing certain kinds of generic functionality.
5. What are different environments to configure hibernate?
There are mainly two types of environments in which the configuration of hibernate application differs. i. Managed environment ? In this kind of environment everything from database connections, transaction boundaries, security levels and all are defined. An example of this kind of environment is environment provided by application servers such as JBoss, Weblogic and WebSphere. ii. Non-managed environment ? This kind of environment provides a basic configuration template. Tomcat is one of the best examples that provide this kind of environment.
6. What are Extension interfaces?
When the built-in functionalities provided by hibernate is not sufficient enough, it provides a way so that user can include other interfaces and implement those interfaces for user desire functionality. These interfaces are called as Extension interfaces.
7. What are managed associations and hibernate associations?
Associations that are related to container management persistence are called managed associations. These are bi-directional associations. Coming to hibernate associations, these are unidirectional.
8. What are POJOs?
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 then simple java classes.
9. What are the benefits of ORM and Hibernate?
There are many benefits from these. Out of which the following are the most important one. i. Productivity ? Hibernate reduces the burden of developer by providing much of the functionality and let the developer to concentrate on business logic. ii. Maintainability ? As hibernate provides most of the functionality, the LOC for the application will be reduced and it is easy to maintain. By automated object/relational persistence it even reduces the LOC. iii. Performance ? Hand-coded persistence provided greater performance than automated one. But this is not true all the times. But in hibernate, it provides more optimization that works all the time there by increasing the performance. If it is automated persistence then it still increases the performance. iv. Vendor independence ? Irrespective of the different types of databases that are there, hibernate provides a much easier way to develop a cross platform application.
10. What are the different approaches to represent an inheritance hierarchy?
i. Table per concrete class. ii. Table per class hierarchy. iii. Table per subclass.