Module 1 - Introduction To Hibernate
Module 1 - Introduction To Hibernate
4
Study Guide
Author
Som Prakash
Implementation
Java
Program
JDBC
JDBC
Uses API DB
JDBC API
Hibernate is implemented on the top of JDBC Technology i.e. you can use Hibernate
API which internally uses JDBC to contact Database.
Java
Implementation
Hibernate
Program Runtime
System JDBC
JDBC
Uses
Uses API DB
Hibernate
JDBC API
API
In the above code, more statements (1, 2, 4, 5, 7) are common across multiple JDBC
programs. This gives you the code duplication problem.
When you write JDBC Code , you as Java Developer are responsible for:
o Getting Connection
o Writing SQL Statements.
o Creating the JDBC Statements.
o Generating Primary Keys.
o Submitting SQL Statements to DB.
o Process the Results.
o Clean the resources.
etc
Hibernate as Persistence Framework automates these tasks and simplifies you life.
b) Customer cust=session.load(Customer.class,99)
2) Hibernate system is responsible for generating SQL queries which are well-tuned in
terms of performance.
3) Hibernate system is responsible for generating the values required for Primary key
columns.
unique id automcatically
4) Hibernate provides many built-In Primary key generation algorithms and also supports
to implement Custom Primary key generation algorithms.
5) Hibernate supports various mapping styles:
1) Simple Mapping
2) Collection Mapping
3) Inheritance Mapping
a. Table per sub class mapping
b. Table per class mapping
c. Table per concrete class mapping
4) Association Mapping
a. One - to - One Mapping
b. One - to - Many Mapping
c. Many- to - Many Mapping
5) Other mappings.
6) Hibernate provides various Query Languages: //not dependet on complete sql it provide other lang
a. HQL(*)
b. QBC(*)
c. Native SQL
d. Named SQL
7) Hibernate supports two ways to manage connections
a. DriverManager connections
b. DataSource connections
11) Write Mapping Annotations to Map the Customer Entity Class with mycustomers Table
1) Lab1A.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
public class Lab1A {
public static void main(String[] args) {
Transaction tx=null;
try {
//Step 1: get the SessionFactory Object.
SessionFactorysessionFactory= XHibernateUtil.getSessionFactory();
//Step 5: End Tx
tx.commit();
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
public class Lab1B {
public static void main(String[] args) {
Transaction tx=null;
try {
SessionFactorysessionFactory= XHibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
session.close();
}
catch(Exception ex)
{ tx.rollback();
ex.printStackTrace();
}
}
}
3)Lab1C.java
package com.jtcindia.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
public class Lab1C {
public static void main(String[] args) {
tx.commit();
session.close();
}
catch(Exception ex)
{ tx.rollback();
ex.printStackTrace();
}
}
}
4)Lab1D.java
package com.jtcindia.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
public class Lab1D {
public static void main(String[] args) {
Transaction tx=null;
try {
SessionFactorysessionFactory= XHibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
5)Customer.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
@Entity
@Table(name="mycustomers")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="cid")
private int cid;
@Column(name="cname")
private String cname;
@Column(name="myemail")
private String email;
@Column(name="myphone")
private int phone;
@Column(name="mycity")
private String city;
public Customer() {}
public Customer(String cname, String email, int phone, String city)
{ this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
@Override
public String toString() {
return " [" + cid + ", " + cname + ", " + email + ", " + phone + ", " + city + "]";
}
}
6)XHibernateUtil.java
package com.jtcindia.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
public class XHibernateUtil {
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myjtcdb</property>
<property name="connection.username">root</property>
<property name="connection.password">somprakash</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.jtcindia.hibernate.Customer"/>
</session-factory>
</hibernate-configuration>
import java.util.Properties;
import org.hibernate.*;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
/*
* @Author : Som Prakash Rai
* @Company: JtcIndia
* */
public class HibernateUtil {
static SessionFactory sessionFactory = null;
static {
try {
Configuration cfg = new Configuration();
Properties myprops = new Properties();
myprops.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
myprops.put(Environment.URL, "jdbc:mysql://localhost:3306/myjtcdb");
myprops.put(Environment.USER, "root");
myprops.put(Environment.PASS, "somprakash");
myprops.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
myprops.put(Environment.SHOW_SQL, "true");
myprops.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
myprops.put(Environment.HBM2DDL_AUTO, "update");
cfg.setProperties(myprops);
cfg.addAnnotatedClass(Customer.class);
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
} catch (Exception e)
{ e.printStackTrace();
}
}
Q3) What is the Annotation for mapping the Entity Class with the Table?
Ans:
Q4) What is the Annotation for mapping the Entity Class field with with column of Table?
Ans:
Q5) What is the Annotation for mapping the Simple Primary Key?
Ans:
Q6) What happens when I specify the Primary Key Strategy as AUTO?
Ans:
Q7) What happens when I specify the Primary Key Strategy as IDENTITY?
Ans:
Q8) What is the difference between Primary Key Strategies – AUTO and IDENTITY?
Ans:
Q9) How many Session Factory Objects will be required for Hibernate Application?
Ans: