Ans: 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.
Ans: 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.
Ans: Mappings between class properties and table columns are specified in XML file as in the below example:
Ans: 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.
Ans: We can use extension interfaces in order to add any required functionality which isn’t supported by built-in interfaces.
Ans: 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.
Ans: 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:
1
2
3
4
|
Configuration config = new Configuration ( ) ;
config . addResource ( & amp ; amp ; quot ; myinstance / configuration . hbm . xml & amp ; amp ; quot ; ) ;
config . setProperties ( System . getProperties ( ) ) ;
SessionFactory sessions = config . buildSessionFactory ( ) ;
|
Ans: 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.
Ans: 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.
Ans: In hibernate we can execute stored procedures using code as below:
[xml]
<sql-query name=”getStudents” callable=”true”>
<return alias=”st” class=”Student”>
<return-property name=”std_id” column=”STD_ID”/>
<return-property name=”s_name” column=”STD_NAME”/>
<return-property name=”s_dept” column=”STD_DEPARTMENT”/>
{ ? = call selectStudents() }
</return>
</sql-query>
[/xml]
Ans: Criteria is a simple yet powerful API of hibernate which is used to retrieve entities through criteria object composition.
Ans: Following are some key benefits of using Hibernate template:
[xml]
<property name=”show_sql”>true</property>
[/xml]
Ans: Following are the two types of collections in hibernate:
Ans: 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.
Ans:
Ans: We can get hibernate statistics using getStatistics() method of SessionFactory class as shown below:
SessionFactory.getStatistics()
Ans: 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.
Ans: 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.
Q20. What’s the usage of callback interfaces in hibernate?
Ans: 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.
Ans: 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.
Ans: Following are the four ORM levels in hibernate:
Ans: 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:
[java]
Session s = null;
Transaction tr = null;
try {
s = sessionFactory.openSession();
tr = s.beginTransaction();
doTheAction(s);
tr.commit();
} catch (RuntimeException exc) {
tr.rollback();
} finally {
s.close();
}
[/java]
Ans: We can use any of the following two methods of hibernate configuration:
Ans: Hibernate supports multiple cache services like EHCache, OSCache, SWARMCache and TreeCache and default cache service of hibernate is EHCache.
Ans: In hibernate; we have following two types of mapping associations between entities:
Ans: Hibernate Query By Criteria (QBC) API is used to create queries by manipulation of criteria objects at runtime.
Ans: Hibernate provides following four ways to fetch objects from database:
Ans: 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:
[xml]
<id name=”deptId” type=”string” >
<column name=”columnId” length=”30″/>
<generator/>
</id>
[/xml]
Ans: 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.
Ans: Hibernate second level cache can be disabled using any of the following ways:
Ans: All the mapping between classes and tables, properties and columns, Java types and SQL types etc is defined in ORM metadata.
Ans: With hibernate 3.2, default transaction factory is JDBCTransactionFactory.
Ans: 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.
Ans: Hibernate session factory can be bound to JNDI by making configuration changes in hibernate.cfg file.
Ans: Object identification can be done in hibernate in following three ways:
Ans: Following fetching strategies are available in hibernate:
Ans: 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:
[xml]
<hibernate-mapping>
<class name=”Student” table=”tbl_student”>
<property column=”studentname” length=”255″
name=”studentName” not-null=”true” type=”java.lang.String”/>
<property column=”studentDisciplne” length=”255″
name=”studentDiscipline” not-null=”true” type=”java.lang.String”/>
</class>
</hibernate-mapping>
[/xml]
Ans: 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.
Ans: 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:
[xml]
<sql-query name = “studentdetails”>
<return alias=”std”/>
SELECT std.STUDENT_ID AS {std.STUDENT_ID},
std.STUDENT_DISCIPLINE AS {std.discipline},
FROM Student std WHERE std.NAME LIKE :name
</sql-query>
[/xml]
Then this query can be called as follows:
[java]
List students = session.getNamedQuery(&quot;studentdetails&quot;)
.setString(&quot;TomBrady&quot;, name)
.setMaxResults(50)
.list();
[/java]
Ans: 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.
Ans: Version property is used in hibernate to know whether an object is in transient state or in detached state.
Ans: 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.
Ans: 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.
Ans: Yes, hibernate fully supports polymorphism. Polymorphism queries and polymorphism associations are supported in all mapping strategies of hibernate.
Ans: Hibernate has following three inheritance models:
Ans: 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
Ans: General hibernate flow involving RDBMS is as follows:
Ans: Light Object Mapping is one of the levels of ORM quality in which all entities are represented as classes and they are mapped manually.
Ans: Managed associations relate to container management persistence and are bi-directional while hibernate associations are unidirectional.