1.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
2.What does ORM consists of ?
ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database
2.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
3 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)
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.
5.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:
- Improved productivity
- High-level object-oriented API
- Less Java code to write
- No SQL to write
- Improved performance
- Sophisticated caching
- Lazy loading
- Eager loading
- Improved maintainability
- A lot less code to write
- Improved portability
- ORM framework generates database-specific SQL for you
6.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.
- Session interface
- SessionFactory interface
- Configuration interface
- Transaction interface
- Query and Criteria interfaces
7 What role does the SessionFactory interface play in Hibernate?
The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application—created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
SessionFactory sessionFactory = configuration.buildSessionFactory();
SessionFactory sessionFactory = configuration.buildSessionFactory();
8 How do you map Java Objects with Database tables?
- 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.
17) What are different environments to configure hibernate?
There are mainly two types of environments in which the configuration of hibernate application differs.
- 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.
- 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.
Example :-
one-to-many class mapping
This article explains how to map two classes in collection mapping. In this example one Author object has the list of Book objects. Both the classes are mapped and will be inserted together into the database. Here this example uses Bag as the collection type. Other collection types available are Set,List,Array. Look into the example programs for more detail.
Author.java
package javabeat.net.hibernate;
import java.util.List;
public class Author {
private long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private List<Book> books;
}
Book.java
package javabeat.net.hibernate;
public class Book {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JavaBeatHibernateExample
package javabeat.net.hibernate;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class JavaBeatHibernateExample {
public static void main(String args[]) {
Configuration configuration = new Configuration();
// configuring hibernate
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
Book book1 = new Book();
book1.setName("Java Book");
List<Book> list = new ArrayList<Book>();
list.add(book1);
Author author = new Author();
author.setName("First Author");
author.setBooks(list);
session.save(author);
tx.commit();;
session.flush();
session.close();
}
}
Author.hbm.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="javabeat.net.hibernate.Author" table="Author">
<id name="id" column="id" type="java.lang.Long" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<bag name="books" cascade="all" >
<key column="id"/>
<one-to-many class="javabeat.net.hibernate.Book"/>
</bag>
</class>
<class name="javabeat.net.hibernate.Book" table="Book">
<id name="id" column="id" type="java.lang.Long" unsaved-value="0" >
<generator class="increment"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?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="connection.url">jdbc:derby://localhost:1527/SampleDB
</property>
<property name="connection.username">root
</property>
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver
</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect
</property>
<property name="connection.password">root
</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="current_session_context_class">thread
</property>
<property name="hibernate.show_sql">true
</property>
<mapping resource="javabeat/net/hibernate/Author.hbm.xml"/>
</session-factory>
</hibernate-configuration>
For Improve Performance:
1. On production server keep hibernate.show_sql to false.
2. set batch size hibernate.jdbc.batch_size
3. if possible also set fetch size.
Session management through Thread Local for improve performance.
How to do this,
public class SerialNum {
1. On production server keep hibernate.show_sql to false.
2. set batch size hibernate.jdbc.batch_size
3. if possible also set fetch size.
Session management through Thread Local for improve performance.
How to do this,
public class SerialNum {
// The next serial number to be assigned
private static int nextSerialNum = 0;
private static ThreadLocal serialNum = new ThreadLocal() {
protected synchronized Object initialValue() {
return new Integer(nextSerialNum++);
}
};
public static int get() {
return ((Integer) (serialNum.get())).intValue();
}
}
4. Try to avoid joining on primary key values.
-/ Dinesh