1.what Is ORM ?: Improved Productivity
1.what Is ORM ?: Improved Productivity
ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java
application to the tables in a relational database.
4.What is Hibernate?
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to
map plain old Java objects to relational database tables using (XML) configuration files.Its purpose is to
relieve the developer from a significant amount of relational data persistence-related programming
tasks.
Improved productivity
o High-level object-oriented API
o Less Java code to write
o No SQL to write
Improved performance
o Sophisticated caching
o Lazy loading
o Eager loading
Improved maintainability
o A lot less code to write
Improved portability
o ORM framework generates database-specific SQL for you
Programmatic configuration
XML configuration (hibernate.cfg.xml)
Session interface
SessionFactory interface
Configuration interface
Transaction interface
Query and Criteria interfaces
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
Example :
<hibernate-mapping>
<class name="com.test.User" table="user">
<property column="USER_NAME" length="255"
name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255"
name="userPassword" not-null="true" type="java.lang.String"/>
</class>
</hibernate-mapping>
load() get()
Only use the load() method if you are sure that If you are not sure that the object exists,
the object exists. then use one of the get() methods.
load() method will throw an exception if the get() method will return null if the unique
unique id is not found in the database. id is not found in the database.
23.Define HibernateTemplate?
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.
26.If you want to see the Hibernate generated SQL statements on console, what should we do?
In Hibernate configuration file set as follows:
<property name="show_sql">true</property>
Example:
If your collection is not large, it will be more If your collection is very large, it will be more
efficient way to sort it. efficient way to sort it .
JDBC Hibernate
With JDBC, developer has to write code to map Hibernate is flexible and powerful ORM solution
an object model's data representation to a to map Java classes to database tables.
Hibernate itself takes care of this mapping
relational data model and its corresponding
using XML files so developer does not need to
database schema.
write code for this.
In JDBC there is no check that always every user Hibernate enables developer to define version
has updated data. This check has to be added type field to application, due to this defined
by the developer. field Hibernate updates version field of
database table every time relational tuple is
updated in form of Java class object to that
table. So if two users retrieve same tuple and
then modify it and one user save this modified
tuple to database, version is automatically
updated for this tuple by Hibernate. When
other user tries to save updated tuple to
database then it does not allow saving it
because this user does not have updated data.
Bag
Set
List
Array
Map
36.How can Hibernate be configured to access an instance variable directly and not through a
setter method ?
By mapping the property with access="field" in Hibernate metadata. This forces hibernate to bypass the
setter method and access the instance variable directly while initializing a newly loaded object.
Mark the class as mutable="false" (Default is true),. This specifies that instances of the class are (not)
mutable. Immutable classes, may not be updated or deleted by the application.
XDoclet Annotations used to support Attribute Java 5.0 Annotations used to support
Oriented Programming Attribute Oriented Programming
Supports Entity Relationships through mapping Support Entity Relationships through Java 5.0
files and annotations in JavaDoc annotations
Provides callback support through lifecycle, Provides callback support through Entity
interceptor, and validatable interfaces Listener and Callback methods
What will happened if level 2 cache is enable and query cache is diabled in Hibernate?
Alternatively:
What will happened if level 2 cache is disabled and query cache is enabled in
Hibernate?
Answer: Hibernate query can return both value types and entity types.
Query cache saves the id of entity types and complete value types.
When the same query is executed again value types and entity type ids are
fetched from query cache.
(OR)
Get java.sql.Connection from Hibernate Session and then play with that connection by call
callablestatement.
CallableStatement st = con
.prepareCall("{call your_sp(? ?)}");
st.registerOutParameter(2 Types.INTEGER);
st.setString(1 "some_Seq");
st.executeUpdate();
1. Join Fetching : retrieval of assosiated obects or collections are done by OUTER JOIN in the same
SELECT.
2. Select Fetching : A second select is used to retrieve the associated instance or collection.
4. Batch Fetching : Hibernate retrieves a batch of entity instance or collections ina single SELECT.
1. Immediate fetching.
2. Lazy collection fetching.
3. "Extra-lazy collection fetching.
4. Proxy fetching.
5."No-proxy" fetching.
6. Lazy attribute fetching.
There are two types of Loading in application. Eager loading and Lazy
loading. In eager loading we will fetch all the values from the Persistent storage
and cache it. It will make serious performance issues. There we use lazy
loading to avoid that scenario.
Which persistent technology can be used instead of Hibernate which is as compatible as Hibernate
(not JDBC nor EJB) any parallel technology?
User java persistance API it is the best and can work on any ORM model.
Hibernate is an ORM tool. There are various other tools available in industry to achieve the
functionalities provided by the Hibernate. JPA (Java Persistent API) is one of them.
A component is a contained object that is persisted as a value type not an entity reference.eg)
.....
{chat initial;
String first;
String last;
.......//first last
in hbm:
The person table would have the columns pid birthday initial first and last.