What is ORM ?
ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.
What does ORM consists of ?
An ORM solution consists of the followig four pieces:
API for performing basic CRUD operations
API to express queries refering to classes
Facilities to specify metadata
Optimization facilities : dirty checking,lazy associations fetching
What are the ORM levels ?
The ORM levels are:
Pure relational (stored procedure.)
Light objects mapping (JDBC)
Medium object mapping
Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)
What is Hibernate ?
Hibernate is Java-based middleware designed to complete the Object Relational (O/R) mapping model. In fact, one of the most complex parts of many O/R mapping mechanisms—writing SQL code—can be simplified significantly with Hibernate. With
Hibernate, you can handle the data in the object and not worry about the details of extracting, storing, and loading individual data components. Hibernate sits between traditional Java objects and handles all the work in persisting those objects based on the appropriate O/R mechanisms and patterns. Hibernate can persist even the most complex classes, including those using composition, inheritance, and collections.
Why do you need ORM tools like hibernate ?
The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:
High-level object-oriented API
Less Java code to write
No SQL to write
A lot less code to write
ORM framework generates database-specific SQL for you
What Does Hibernate Simplify ?
Saving and retrieving your domain objects
Making database column and table name changes
Centralizing pre save and post retrieve logic
Complex joins for retrieving related items
Schema creation from object model
How to configure Hibernate ?
Use below dependencies in the pom.xml file to use hibernate
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.1.Final</version> <classifier>tests</classifier> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.0.1.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.0.CR2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <!-- use mysql connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> </dependency>
Use below configuration file to use MySQL database
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cdcol?zeroDateTimeBehavior=convertToNull</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> </session-factory> </hibernate-configuration>
What are the most common methods of Hibernate configuration ?
The most common methods of Hibernate configuration are:
XML configuration (hibernate.cfg.xml)
What are the Core interfaces are of Hibernate framework ?
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.
Query and Criteria interfaces
What is SessionFactory in Hibernate ?
SessionFactory sessionFactory = configuration.buildSessionFactory();
SessionFactory is a singleton class, that means, only one instance will be created per application per JVM. The SessionFactory configures Hibernate for the application using the supplied configuration file(hibernate.cfg.xml) and allows for a Session object to be instantiated.
What is Session in Hibernate ?
Session session = sessionFactory.openSession();
The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. When the Session object is created, an object or two is persisted or loaded from the database, then the Session object is closed. When a Session object is instantiated, a connection is made to the database; thus the interactions should be fairly quick to ensure the application doesn’t keep database connections open needlessly.
Generally, a Session object isn’t created when the application is started and closed when the application exits; instead, it’s created when needed and closed when a persistence operation has completed.
It plays the following role
Wraps a JDBC connection
Factory for Transaction
Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier
What is transaction interface in Hibernate ?
The Transaction interface lets you make sure that all the operations on persisted objects occur in a transaction supported by either JDBC or JTA. When you’re using a transaction, any error that occurs between the start and end of the transaction will cause all the operations to fail; this is important when the persisted objects rely on each other to maintain integrity in the database.
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
What is query interface in Hibernate ?
The Query interface lets you query the database for objects instead of having to rely solely on an object’s identifier.
What are Hibernate Callbacks ?
When Hibernate is in the process of persisting or loading an object, it provides support for you to be warned of certain events so you can respond appropriately. The process is implemented through callbacks. The available events are as follows:
public Boolean onSave(Session s) – called before the object is saved to the database
public Boolean onUpdate(Session s) – called before the object is updated
public boolean onDelete(Session s) – called before the object is deleted
public Boolean onLoad(Session s) – called after the object is loaded from the database
What is Validatable interface in Hibernate ?
Hibernate also implements a Validatable interface that you can use to verify the state of an object before it is persisted to the database. The goal of the interface is not to change the state of the object but to make sure the object is in a state appropriate to be stored; otherwise an exception is thrown.
What is XML mapping document in Hibernate ?
The mapping document is an XML document, and it includes the necessary tags to indicate its status. The root element is <hibernate-mapping>; it contains all the <class> elements used to define specific mappings from a Java class to the database. If you don’t include a package attribute in the <hibernate-mapping> element, you must provide a fully qualified class name in the <class> element.
What is class element in Hibernate mapping document ?
The <class> element includes two attributes:
name: Identifies the fully qualified class name for the class or interface being mapped; if the package prefix is used in the <hibernate-mapping> element, the class name can be the unqualified name.
table: Specifies the name of the table in the database where the mapped class will be persisted.