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 No SQL to write
Improved performance
o Sophisticated caching
o Lazy loading
o Eager loading
Improved maintainability
Improved portability
Programmatic configuration
The five core interfaces are used in just about every Hibernate application. Using these interfaces, you
can store and retrieve persistent objects and control transactions.
Session interface
SessionFactory interface
Configuration interface
Transaction interface
Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph
or looking up objects by identifier
Load the Hibernate configuration file and create configuration object. It will automatically load all
hbm mapping files
First we need to write Java domain objects (beans with setter and getter).
Write hbm.xml, where we map java class to table and database columns to Java class variables.
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>
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>
A component can be saved directly without needing to declare interfaces or identifier properties
Example:
29.What is the difference between sorted and ordered collection in hibernate?
sorted collection vs. order collection :-
sorted collection order collection
A sorted collection is sorting a collection by
utilizing the sorting features provided by the Java
Order collection is sorting a collection by
collections framework. The sorting occurs in the
specifying the order-by clause for sorting this
memory of JVM which running Hibernate, after the
collection when retrieval.
data being read from database using java
comparator.
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 .
31.What is the advantage of Hibernate over jdbc?
Hibernate Vs. JDBC :-
JDBC Hibernate
Bag
Set
List
Array
Map
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.
dynamic-update (defaults to false): Specifies that UPDATE SQL should be generated at runtime
and contain only those columns whose values have changed
dynamic-insert (defaults to false): Specifies that INSERT SQL should be generated at runtime and
contain only the columns whose values are not null.
A fetching strategy is the strategy Hibernate will use for retrieving associated objects if the application
needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or
over-ridden by a particular HQL or Criteria query.
Detached -The instance was associated with a persistence context which has been closed –
currently not associated
XDoclet Annotations used to support Java 5.0 Annotations used to support Attribute
Attribute Oriented Programming Oriented Programming
Supports Entity Relationships through Support Entity Relationships through Java 5.0
mapping files and annotations in JavaDoc annotations
Provides callback support through lifecycle, Provides callback support through Entity Listener
interceptor, and validatable interfaces and Callback methods
Answer:
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.
Answer:
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.
Q. What is a Session? Can you share a session object between different theads?
Answer:
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();
It is also vital that you close your session after your unit of work completes. Note: Keep your Hibernate
Session API handy.
Answer:
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.
Answer:
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.
Q. How does Hibernate distinguish between transient (i.e. newly instantiated) and detached
objects?
Answer