Tuesday, 27 August 2019

Notes on Hibernate : Trying to understand with codes

 -----------
| Hibernate |
 -----------

Hibernate is a persistence framework which provides many elegant and innovative ways to simplifies the relational database(RDBMS) handling task in java.

Hibernate Framework was implemented by REDHAT. Architech of Hibernate is Gavin King.

Hibernate is the best and dominated open source ORM (Object Relational Mapping) tool and is best among all other persistence framework like Ibatis, TopLink, JDO etc.

   0
   |
  java objects

   R
   |
  Relational Entities

   | 
   M (Mapping between these two O & R)

Hibernate can be implemented either with xml approach (Hibernate Core) or Annotation approach(Hibernate Annotation) on the Top of JDBC technology.(not an alternative of JDBC).


------------------------------------------------------------------------

===================MIXED===============================


package com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Type4Mysql
{
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myconnection","root","root");
System.out.println("Connection establish");

String sql = "insert into type4 values(80,'sanjay','gaya')";  // create the sql statement
stmt = conn.createStatement();   // create the jdbc statement

// for insert,update,delete -- you have to use --  public int executeUpdate(String sql)

int res = stmt.executeUpdate(sql);    // submit the query  (for modification type)
System.out.println("upadte = "+res);
// process the result
if(res == 1)
{
System.out.println("Recorded Inserted");

}
else
{
System.out.println("Record Not inserted");
}
}
catch(ClassNotFoundException e){
System.out.println("Class not found");
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

}



When you write the JDBC code for implementing database operations, you need to write the following.

1.) try {
2.) get the Connection
3.) Prepare the SQL (*)
4.) Create the JDBC Statement
5.) Submit the SQL to DB
6.) Process the Result (*)
    }catch(Exception e){
      }finally{

  // every method of java.sql API throwing a compile time exception i.e SQLException
  // that's why we used try and catch block for normal execution.

7.) Clean the Resources }


---------------------------Hibernate Lab1----------------------------------

1. JdbcUtil.java
----------------

package com.jdbc.Pstatement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcUtil
{

static Connection conn;

public static Connection getOracleConnection() throws SQLException
{
try
  {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "SYSTEM", "root");
System.out.println("Connection Established");
  }
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}

public static Connection getMysqlConnection() throws SQLException
{
try
  {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myconnection","root","root");
System.out.println("Connection establish");
  }
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}

public static void cleanUp(PreparedStatement pstmt,Connection conn)
   {
   try
{
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
   }

public static void cleanUp(ResultSet rs,PreparedStatement pstmt,Connection conn)
{
try{
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}

}


2. Lab1.java
------------

package com.jdbc.Pstatement;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;

public class Lab4
{
public static void main(String[] args)
{
if(args.length!=8)
{
System.out.println("Enter  values as CLA");
System.exit(0);
}
Connection conn = null;
PreparedStatement pstmt = null;
try
{
int id = Integer.parseInt(args[0]);
String name = args[1];
String email = args[2];
long phone = Long.parseLong(args[3]);
float fee = Float.parseFloat(args[4]);
int d = Integer.parseInt(args[5]);
int m = Integer.parseInt(args[6]);
int y = Integer.parseInt(args[7]);

java.sql.Date dob = new Date(y-1900, m-1, d);
conn = JdbcUtil.getMysqlConnection();

pstmt = conn.prepareStatement("insert into cmstudents values(?,?,?,?,?,?)");

pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, email);
pstmt.setLong(4, phone);
pstmt.setFloat(5, fee);
pstmt.setDate(6, dob);

int res = pstmt.executeUpdate();
if(res==1)
{
System.out.println("Record Inserted Successfully");
}
else{
System.out.println("Error during inserting");
}

}catch(Exception e)
{
e.printStackTrace();
}
finally{
JdbcUtil.cleanUp(pstmt, conn);
}


}

}





In the above code, more statements (1, 2, 4, 5, 7) are common across multiple JDBC Program. This gives you the code duplication problem.

only 3 and 6 changes here, depends on which type of curd operation you perform.


You are responsible for generating Primary Key.
You are responsible for writing SQL Statements.
You are responsible for cleaning the resources.
You are responsible for processing resultset.
You are responsible for implementing batch updates.
(that means .. some bundle of records you can store at a time in DB).



//  --- Basic code for hibernate core and annotation ---


---------------------------Hibernate Lab2-----------------------------------



1. Customer.java
----------------

package com.cmania.hibernate;

public class Customer
{
private int cid;
private String cname;
private String email;
private long phone;
private String city;

    //  getter and setter

public Customer(String cname, String email, long phone, String city)
{
super();
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
public Customer()
{  }
}


2. Customer.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 package="com.cmania.hibernate">

   <class name="Customer" table="customers">
       <id name="cid" column="cid" type="int">
          <generator class="increment"></generator>
       </id>
     
       <property name="cname"></property>
       <property name="email"></property>
       <property name="phone" type="long"></property>
       <property name="city" column="city" type="string"></property>
     
   </class>

</hibernate-mapping>


3. 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.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost:3306/cmdb</property>
          <property name="connection.username">root</property>
          <property name="connection.password">root</property>
       
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="show_sql">true</property>
       
          <mapping resource="Customer.hbm.xml"  />
       
      </session-factory>
</hibernate-configuration>


4. Lab2Client1.java
-------------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Lab1Client1
{
static SessionFactory factory;

public static void main(String[] args)
{
try
{
Configuration cfg = new Configuration();
cfg = cfg.configure("hibernate.cfg.xml");
factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Customer cust = new Customer("funky", "ns@cm", 76676767, "patna");
session.save(cust);
tx.commit();
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}

}


5. Lab2Client2.java
-------------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Lab1Client2
{
static SessionFactory factory;

public static void main(String[] args)
{
try
{
Configuration cfg = new Configuration();
cfg = cfg.configure("hibernate.cfg.xml");
factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Customer cust = (Customer) session.load(Customer.class, cid);

System.out.println(cust.getCid()+"\t"+cust.getCnam()
                        +"\t"+cust.getEmail()+"\t"+cust.getPhone()+"\t"+cust.getCity());

tx.commit();
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}


}

}


-------------------  Repeatable code Reduction ----------------------------
-------------------  HibernateUtil Based Code -----------------------------



1. Customer.java
----------------

package com.cmania.hibernate;

public class Customer
{
private int cid;
private String cname;
private String email;
private long phone;
private String city;

    //  getter and setter

public Customer(String cname, String email, long phone, String city)
{
super();
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
public Customer()
{  }
}


2. Customer.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 package="com.cmania.hibernate">

   <class name="Customer" table="customers">
       <id name="cid" column="cid" type="int">
          <generator class="increment"></generator>
       </id>
     
       <property name="cname"></property>
       <property name="email"></property>
       <property name="phone" type="long"></property>
       <property name="city" column="city" type="string"></property>
     
   </class>

</hibernate-mapping>


3. 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.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost:3306/cmdb</property>
          <property name="connection.username">root</property>
          <property name="connection.password">root</property>
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="show_sql">true</property>
       
          <mapping resource="Customer.hbm.xml"/>
       
      </session-factory>
</hibernate-configuration>



4. CHibernateUtil.java
----------------------

package com.cmania.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CHibernateUtil
{
static SessionFactory factory;
static
{
Configuration cfg = new Configuration();
cfg = cfg.configure("hibernate.cfg.xml");
factory = cfg.buildSessionFactory();

}
public static SessionFactory geFactory()
{
return factory;
}
}



5. Lab2Client1.java
-------------------


package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class Lab1Client1
{
public static void main(String[] args)
{
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer cust = new Customer("ankit", "am@cm", 76000000, "blore");
session.save(cust);
tx.commit();
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}

}


6. Lab2Client2.java
-------------------


package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class Lab1Client2
{
public static void main(String[] args)
{
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer cust = (Customer) session.load(Customer.class, 1);

System.out.println(cust.getCid()+"\t"+cust.getCnam()
                        +"\t"+cust.getEmail()+"\t"+cust.getPhone()+"\t"+cust.getCity());

tx.commit();
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}


}

}


---------------------------Hibernate Lab3-----------------------------------



1. Customer.java
----------------

package com.cmania.hibernate;

public class Customer
{
private int cid;
private String cname;
private String email;
private long phone;
private String city;

    //  getter and setter

public Customer(String cname, String email, long phone, String city)
{
super();
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
public Customer()
{  }
}



2. 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.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost/cmdb</property>
          <property name="connection.username">root</property>
          <property name="connection.password">root</property>
       
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
       
          <mapping class="com.codingmania.hibernate.Customer"  />
       
      </session-factory>
 
</hibernate-configuration>



3. AHibernateUtil.java
----------------------

package com.cmania.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class AHibernateUtil
{
static SessionFactory factory;
static
{
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg = (AnnotationConfiguration) cfg.configure("hibernate.cfg.xml");
factory = cfg.buildSessionFactory();

}
public static SessionFactory geFactory()
{
return factory;
}
}






4. Lab3Client1.java
-------------------


package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab2Client1
{
public static void main(String[] args)
{
try
{
SessionFactory sf = AHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer cust1 = new Customer("ankit", "cs@cm", 767676, "cheni");
session.save(cust1);
tx.commit();
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}

}



5. Lab3Client2.java
-------------------


package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab2Client2
{
public static void main(String[] args)
{
try
{
SessionFactory sf = AHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer cust1 = (Customer) session.load(Customer.class,cid);

             System.out.println(cust1.getCid)+"\t"+cust1.getCname)+"\t"+cust1.getEmail                         ()+"\t"+cust1.getPhone()+"\t"+cust1.getCity());

tx.commit();
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}


}

}



----------------------------------------------------------------------------


Hibernate Features: 
-------------------

  1.  Hibernate system is responsible for taking the Connection, Creating statements and               releasing the resources.

         Customer cust = new Customer("nish","nish@cm",11);
         session.save(cust);

  2.  Hibernate System is responsible for generating SQL queries required.

  3.  Hibernate system is responsible for generating the value required for Primary Key                columns. Hibernate provides many built-In Primary Key generation algorithms and                  supports to implement your own Custom Primary key generation algorithms.

  4.  Hibernate supports various mapping types
       (i)   Simple Mapping
       (ii)  Collection Mapping

       (iii) Inheritance Mapping
              a) Table per sub Class Mapping
              b) Table per Class Mapping
              c) Table per Concrete Class Mapping

       (iv)  Association Mapping
              a) One-to-One Mapping
        b) One-to-Many Mapping
              c) Many-to-Many Mapping

  5.  Hibernate supports two ways to manage connections
         a) DriverManager Connections
         b) DataSource Connections(*more preferable by performance wise)

  6.  Hibernate supports two ways to Manage Transitions
         a) JDBC Transitions (Outside the EJB or Spring Container)
         b) JTA Transitions (Inside the EJB or Spring Container)
          *it supports mainly Distributed Transaction Management*

  7.  Hibernate has in-built support for Batch Updates.

  8.  Hibernate provides various caching mechanism.

  9.  Hibernate provides various Object Oriented Query Language (OOQL)
       a) HQL (*Hibernate Query Language)
       b) QBC (Query By Criteria)
       c) QBE (Query By Example)
       d) Native SQL
       e) Named SQL

  10.  Hibernate System uses many persistent best practices and forces the developer to use             them for better performance.


         ---- DIAGRAM 1 ----


-----------------------------------------------------------------------------


Hibernate Mapping Types:
------------------------

a.) Simple Mapping:

         When you map your persistence class field with simple data types like String,                    primitives, wrappers date etc with the corresponding table columns then it is
         called as Simple Mapping.

                   ----   Refer previous Labs with core and annotation  ----

b.) Collection Mapping:

         When you map your persistence class with Collection data types like array, set, map              with the corresponding table then it is called as Simple Mapping Collection Mapping.

     --   Refer Labs with core and annotation  (JPA does not support Collection Mapping)  --

     ---------------------------Hibernate  Lab4-----------------------------------

     
1.Student.java
--------------

package com.cmania.hibernate;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student
{
   // select these simple type data and make a master table with the help of these.
private int sid;   // for primary key
private String sname;
private String dob;
private String qualification;

   // and related to that master table design --  A slave table for Each collection type data.
   // because collection may contain multiple values with itself which you can't insert in    // a particular fields.so you must have to create a table for each collection type of data.

   // collection type of mapping data type use below

private String[] courses;

   // only String array type of collection is loaded aggressively or eagerly.

   // & rest of all collection type is loaded lazily.

private List<String> emails;
private List<Integer> marks;
private Set<Long> phones;
private Map<String, Long> refs;

//  import setters and getters

public Student(String sname, String dob, String qualification,
String[] courses, List<String> emails, List<Integer> marks,
Set<Long> phones, Map<String, Long> refs) {
super();
this.sname = sname;
this.dob = dob;
this.qualification = qualification;
this.courses = courses;
this.emails = emails;
this.marks = marks;
this.phones = phones;
this.refs = refs;
}

public Student()
{
super();
}

}


2.  Student.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 package="com.cmania.hibernate">

   <class name="Student" table="students">
       <id name="sid" column="sid" type="int">
          <generator class="increment"></generator>
       </id>
     
       <property name="sname"></property>
       <property name="dob"></property>
       <property name="qualification" column="quali"></property>
     
       <array name="courses" table="courses">
         <key column="sid"></key>
         <index column="idx"></index>
         <element column="cname" type="string"></element>
       </array>
     
       <list name="emails" table="emails">
          <key column="sid"></key>
          <index column="idx"></index>
          <element column="emailId" type="string"></element>
       </list>
     
       <bag name="marks" table="marks">
         <key column="sid"></key>
          <element column="marks" type="int"></element>
       </bag>
     
       <set name="phones" table="phones">
         <key column="sid"></key>
          <element column="phoneNo" type="long"></element>
       </set>
     
       <map name="refs" table="refs">
         <key column="sid"></key>
          <index column="rname" type="string"></index>
          <element column="rphone" type="long"></element>
       </map>
   </class>

</hibernate-mapping>



3. 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.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost:3306/cmdb</property>
          <property name="connection.username">root</property>
          <property name="connection.password">root</property>
       
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="show_sql">true</property>
          <property name="hbm2ddl.auto">update</property>
       
          <mapping resource="Student.hbm.xml"  />
       
      </session-factory>
</hibernate-configuration>



4. CHibernateUtil.java
----------------------

5. Lab4Client1.java
-------------------


package com.cmania.hibernate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab4Client1
{
public static void main(String[] args)
{
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

String cous[] = {"JAVA","JDBC","JSP","SERVLET"};

List<String> ems = new ArrayList<String>();
ems.add("ns@cm.com");
ems.add("n@cm.com");
ems.add("s@cm.com");

List<Integer> marks = new ArrayList<Integer>();
marks.add(new Integer(11));
marks.add(new Integer(22));
marks.add(new Integer(33));

Set<Long> phs = new HashSet<Long>();
phs.add(new Long(1111));
phs.add(new Long(2222));
phs.add(new Long(3333));

Map<String, Long> refs = new HashMap<String, Long>();
refs.put("aaa", new Long(1111));
refs.put("bbb", new Long(2222));
refs.put("ccc", new Long(3333));

Student st = new Student("Nishant", "02-june", "BTECH", cous, ems, marks, phs, refs);

session.save(st);
tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
}

}

}


6. Lab4Client2.java
-------------------


package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CLab3Client2
{
public static void main(String[] args)
{
try
{
    SessionFactory sf = CHibernateUtil.geFactory();
    Session session = sf.openSession();
    Transaction tx = session.beginTransaction();
     
     Student stud = (Student) session.load(Student.class, 1);
     System.out.println(stud.getSid()+"\t"+stud.getSname()+"\t"+stud.getDob());
   
     for (String cn : stud.getCourses())
     {
  System.out.println(cn);
}
     System.out.println(stud.getEmails());
     System.out.println(stud.getMarks());
     System.out.println(stud.getPhones());
     System.out.println(stud.getRefs());
 
     tx.commit();
     session.close();
   
}
catch (Exception e)
{
e.printStackTrace();
}

}

}

------------------------------------------------------------------------------

   

c.) Inheritance Mapping:
When multiple persistence classes are in inheritance relationship then use Inheritance           Mapping. You can implement Inheritance Mapping in three ways.
   
      1) Table per sub class Mapping (*)
                  --> In this mapping, you need to take one table for one sub class.


---------------------------Hibernate Lab5----------------------------------- 


1. Student.java
---------------

package com.cmania.hibernate;

public class Student
{
private int sid;   // for primary key

private String sname;
private String city;
private String status;
private double totalfee;

// getters and setters

public Student(String sname, String city, String status, double totalfee) {
super();
this.sname = sname;
this.city = city;
this.status = status;
this.totalfee = totalfee;
}

public Student()
{
super();
}
}


2. CurrentStudent.java
----------------------

package com.cmania.hibernate;

public class CurrentStudent extends Student
{
private double feebal;
private String timings;
private String branch;
public CurrentStudent(String sname, String city, String status,
double totalfee, double feebal, String timings, String branch) {
super(sname, city, status, totalfee);
this.feebal = feebal;
this.timings = timings;
this.branch = branch;
}
// applying setters and getters
}


3. OldStudent.java
------------------

package com.cmania.hibernate;

public class OldStudent extends Student
{
private String ocompany;
private String ocemail;
private double octc;
public OldStudent(String sname, String city, String status,
double totalfee, String ocompany, String ocemail, double octc) {
super(sname, city, status, totalfee);
this.ocompany = ocompany;
this.ocemail = ocemail;
this.octc = octc;
}
  // setters and getters
}


4. WeekdayStudent.java
----------------------

package com.cmania.hibernate;

public class WeekdayStudent extends CurrentStudent
{
private String qualification;
private String percentage;
private int yoe;
public WeekdayStudent(String sname, String city, String status,
double totalfee, double feebal, String timings, String branch,
String qualification, String percentage, int yoe) {
super(sname, city, status, totalfee, feebal, timings, branch);
this.qualification = qualification;
this.percentage = percentage;
this.yoe = yoe;
}
// setters and getters

}

5. WeekendStudent.java
----------------------

package com.cmania.hibernate;

public class WeekendStudent extends CurrentStudent
{
private String wcompany;
private String wcemail;
private double wctc;
public WeekendStudent(String sname, String city, String status,
double totalfee, double feebal, String timings, String branch,
String wcompany, String wcemail, double wctc) {
super(sname, city, status, totalfee, feebal, timings, branch);
this.wcompany = wcompany;
this.wcemail = wcemail;
this.wctc = wctc;
}
// setters and getters
}



6. CHibernateUtil.java
----------------------

7. Lab5.java
------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CLab5
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

  // step 1 -- Adding the Student

Student stud = new Student("funky", "patna", "green", 15000.00);
Integer it = (Integer) session.save(stud);
System.out.println(it.intValue());

// step 2 -- Adding the CurrentStudent

CurrentStudent cstud = new CurrentStudent("nishu", "blore", "enable",                                                             11000.0, 5000, "3:00PM", "patna");
it = (Integer) session.save(cstud);
System.out.println(it.intValue());

// step 3 -- Adding the OldStudent

OldStudent ostud = new OldStudent("pink", "bhopal", "red", 2200.00,                                                             "CM", "pink@cm", 120);
it = (Integer) session.save(ostud);
System.out.println(it.intValue());

// step 4 -- Adding the RegularStudent

WeekdayStudent wdstud = new WeekdayStudent("Praty", "MP", "green",                                         3400.00, 2300.00, "9:00AM", "BPL", "BTECH", "85.0", 3);
it = (Integer) session.save(wdstud);
System.out.println(it.intValue());

// step 5 -- Adding the WeekendStudent

WeekendStudent westud = new WeekendStudent("khusu", "Jbad", "disable",                                        3400.00, 2323.45, "1:00PM", "CSE", "CM", "kk@cm", 9.0);
it = (Integer) session.save(westud);
System.out.println(it.intValue());

//  commitment of transaction

tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
if (tx!= null)
{
    tx.rollback();
}
}}}


8. Student.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 package="com.cmania.hibernate">

   <class name="Student" table="mystudents">
     
       <id name="sid" column="sid" type="int">
            <generator class="increment"></generator>
       </id>
     
       <property name="sname"></property>
       <property name="city"></property>
       <property name="status"></property>
       <property name="totalfee" type="double"></property>
     
        <joined-subclass name="CurrentStudent" table="cstudents">
          <key column="sid"></key>
          <property name="feebal" type="double"></property>
          <property name="timings"></property>
          <property name="branch"></property>
               <joined-subclass name="WeekdayStudent" table="wdstudents">
                      <key column="sid"></key>
                      <property name="qualification"></property>
                       <property name="percentage"></property>
                       <property name="yoe" type="int"></property>                 
                </joined-subclass>
               <joined-subclass name="WeekendStudent" table="westudents">
                      <key column="sid"></key>
                      <property name="wcompany"></property>
                       <property name="wcemail"></property>
                       <property name="wctc" type="double"></property>                 
                </joined-subclass>
        </joined-subclass>
     
        <joined-subclass name="OldStudent" table="ostudents">
          <key column="sid"></key>
          <property name="ocompany"></property>
          <property name="ocemail"></property>
          <property name="octc" type="double"></property>
        </joined-subclass>     
     
    </class>

</hibernate-mapping>


9. 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.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost:3306/cmdb1</property>
          <property name="connection.username">root</property>
          <property name="connection.password">root</property>
       
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="show_sql">true</property>
          <property name="hbm2ddl.auto">update</property>
       
          <mapping resource="Student.hbm.xml"  />
  <mapping resource="WeekdayStudent.hbm.xml"  />
  <mapping resource="WeekendStudent.hbm.xml"  />
  <mapping resource="CurrentStudent.hbm.xml"  />
  <mapping resource="OldStudent.hbm.xml"  />
       
      </session-factory>
</hibernate-configuration>


-------------------------------------------------------------------------------


      2) Table per class Mapping
          --> In this mapping, you need to take only one table for all the super and sub                       class.It is also called as Single Table Mapping.
         :NOTE:
         ------
           In the Above table, there is one special column called discriminator column-stutype              but there is no variable for this column in any persistence class. You have to                   specify the discriminator column value in mapping doc.
         <subclass name=”CurrnetStudent” discriminator-value=”CSTU”>


---------------------------Hibernate Lab6----------------------------------- 


1. Student.java
---------------


2. CurrentStudent.java
----------------------


3. OldStudent.java
------------------


4. WeekdayStudent.java
----------------------


5. WeekendStudent.java
----------------------


6. CHibernateUtil.java
----------------------

7. Lab6.java
------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab6
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

  // step 1 -- Adding the Student

Student stud = new Student("funky", "patna", "green", 15000.00);
Integer it = (Integer) session.save(stud);
System.out.println(it.intValue());

// step 2 -- Adding the CurrentStudent

CurrentStudent cstud = new CurrentStudent("nishu", "blore", "enable",    11000.0, 5000, "3:00PM", "patna");
it = (Integer) session.save(cstud);
System.out.println(it.intValue());

// step 3 -- Adding the OldStudent

OldStudent ostud = new OldStudent("pink", "bhopal", "red", 2200.00, "CM", "pink@cm", 120);
it = (Integer) session.save(ostud);
System.out.println(it.intValue());

// step 4 -- Adding the RegularStudent

WeekdayStudent wdstud = new WeekdayStudent("Praty", "MP", "green", 3400.00, 2300.00, "9:00AM", "BPL", "BTECH", "85.0", 3);
it = (Integer) session.save(wdstud);
System.out.println(it.intValue());

// step 5 -- Adding the WeekendStudent

WeekendStudent westud = new WeekendStudent("khusu", "Jbad", "disable", 3400.00, 2323.45, "1:00PM", "CSE", "CM", "kk@cm", 9.0);
it = (Integer) session.save(westud);
System.out.println(it.intValue());

//  commitment of transaction

tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
if (tx!= null)
{
    tx.rollback();
}
}}}


8. Student.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 package="com.codingmania.hibernate">

   <class name="Student" table="mystudents" discriminator-value="STUD">
     
       <id name="sid" column="sid" type="int">
            <generator class="increment"></generator>
       </id>
<!-- use of descriminator to identify the type of student,& with the column name StudType-->
<!-- we use here 1 more attribute that value of discriminator-value attribute should not exceed than 6 -->
       <discriminator column="StudType" length="6"></discriminator>
       <property name="sname"></property>
       <property name="city"></property>
       <property name="status"></property>
       <property name="totalfee" type="double"></property>
     
        <subclass name="CurrentStudent" discriminator-value="CSTUD">
          <property name="feebal" type="double"></property>
          <property name="timings"></property>
          <property name="branch"></property>
                  <subclass name="WeekdayStudent" discriminator-value="RSTUD">
                           <property name="qualification"></property>
                           <property name="percentage"></property>
                           <property name="yoe" type="int"></property>                 
                  </subclass>
                  <subclass name="WeekendStudent" discriminator-value="WSTUD">
                             <property name="wcompany"></property>
                             <property name="wcemail"></property>
                             <property name="wctc" type="double"></property>                 
                    </subclass>
            </subclass> 
           <subclass name="OldStudent" discriminator-value="OSTUD">
                 <property name="ocompany"></property>
                 <property name="ocemail"></property>
                 <property name="octc" type="double"></property>                 
           </subclass>     
    </class>

</hibernate-mapping>


9. hibernate.cfg.xml
--------------------




      3) Table per Concrete  class Mapping
                  -->  In this mapping, you need to take one table for one Concrete class.




---------------------------Hibernate Lab7----------------------------------- 


1. Student.java
---------------


2. CurrentStudent.java
----------------------


3. OldStudent.java
------------------


4. WeekdayStudent.java
----------------------


5. WeekendStudent.java
----------------------


6. CHibernateUtil.java
----------------------

7. Lab7.java
------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab7
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

  // step 1 -- Adding the Student

Student stud = new Student("funky", "patna", "green", 15000.00);
Integer it = (Integer) session.save(stud);
System.out.println(it.intValue());

// step 2 -- Adding the CurrentStudent

CurrentStudent cstud = new CurrentStudent("nishu", "blore", "enable", 11000.0, 5000, "3:00PM", "patna");
it = (Integer) session.save(cstud);
System.out.println(it.intValue());

// step 3 -- Adding the OldStudent

OldStudent ostud = new OldStudent("pink", "bhopal", "red", 2200.00, "CM", "pink@cm", 120);
it = (Integer) session.save(ostud);
System.out.println(it.intValue());

// step 4 -- Adding the RegularStudent

WeekdayStudent wdstud = new WeekdayStudent("Praty", "MP", "green", 3400.00, 2300.00, "9:00AM", "BPL", "BTECH", "85.0", 3);
it = (Integer) session.save(wdstud);
System.out.println(it.intValue());

// step 5 -- Adding the WeekendStudent

WeekendStudent westud = new WeekendStudent("khusu", "Jbad", "disable", 3400.00, 2323.45, "1:00PM", "CSE", "CM", "kk@cm", 9.0);
it = (Integer) session.save(westud);
System.out.println(it.intValue());

//  commitment of transaction

tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
if (tx!= null)
{
    tx.rollback();
}
}}}

8. Student.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 package="com.codingmania.hibernate">

   <class name="Student" table="mystudents">
     
       <id name="sid" column="sid" type="int">
            <generator class="increment"></generator>
       </id>
       <property name="sname"></property>
       <property name="city"></property>
       <property name="status"></property>
       <property name="totalfee" type="double"></property>
    </class>
     
    <class name="CurrentStudent" table="cstudents">
         
           <id name="sid" column="sid" type="int">
                 <generator class="increment"></generator>
           </id>
           <property name="sname"></property>
          <property name="city"></property>
          <property name="status"></property>
          <property name="totalfee" type="double"></property>
          <property name="feebal" type="double"></property>
          <property name="timings"></property>
          <property name="branch"></property>
       
        </class>
       
        <class name="WeekdayStudent" table="wdstudents">
     
             <id name="sid" column="sid" type="int">
                    <generator class="increment"></generator>
            </id>
                <property name="sname"></property>
               <property name="city"></property>
              <property name="status"></property>
              <property name="totalfee" type="double"></property>
              <property name="feebal" type="double"></property>
              <property name="timings"></property>
              <property name="branch"></property>         
                           <property name="qualification"></property>
                           <property name="percentage"></property>
                           <property name="yoe" type="int"></property>                 
        </class>
               
       <class name="WeekendStudent" table="westudents">
                <id name="sid" column="sid" type="int">
                    <generator class="increment"></generator>
                </id>
                  <property name="sname"></property>
                  <property name="city"></property>
                  <property name="status"></property>
                  <property name="totalfee" type="double"></property>
                  <property name="feebal" type="double"></property>
                  <property name="timings"></property>
                  <property name="branch"></property>     
                   <property name="wcompany"></property>
                   <property name="wcemail"></property>
                   <property name="wctc" type="double"></property>                 
       </class>
   
           <class name="OldStudent" table="ostudents">
                 <id name="sid" column="sid" type="int">
                        <generator class="increment"></generator>
                 </id>
                  <property name="sname"></property>
                  <property name="city"></property>
                  <property name="status"></property>
                   <property name="totalfee" type="double"></property>
                  <property name="ocompany"></property>
                  <property name="ocemail"></property>
                  <property name="octc" type="double"></property>                 
           </class>     
 

</hibernate-mapping>


9. hibernate.cfg.xml
--------------------



         ---- DIAGRAM 2 ----

------------------------------------------------------------------------------------


d.) Association Mapping:
            If you want to establish the relationship among different persistence classes, then              you have use Association Mapping.Depending on Directionality, there are two types of             Association Mapping
       
           1.) Uni-Directional Relationship
           2.) Bi-Direcctional Relationship

             Directionality represents whether we can access the Data in One direction Only
             or both the directions.
     
        Example :-

      1)One-one Bi-Directional
          One Customer can have one Address and one Address belongs to One Customer.

      2)One-Many Bi-Directional
          One Customer can place Many Orders and one Order placed by one Customer.

      3)Many-Many Bi-Directional
          One Customer can have Many Accounts and one Account belongs to Many Customer.




          1.) One to One Uni- Directional Mapping :-
                   Consider the relationship between Student and Address.
                   One Student contains One Address & One Address belongs to One Student.


----------------------------Hibernate Lab8---------------------------------

1. Student.java
---------------

package com.cmania.hibernate;

public class Student
{
int sid;   // for primary key
String sname;
String email;
String phone;
Address address;
public Student(String sname, String email, String phone)
{
super();
this.sname = sname;
this.email = email;
this.phone = phone;
}

public Student()
{

}
  // setters and getters
}


2. Address.java
---------------

package com.cmania.hibernate;

public class Address
{
int adid;     // for primary key.
String street;
String city;
String state;
public Address(String street, String city, String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}

public Address()
{

}
// setters and getters
}


3. CHibernateUtil.java
----------------------

4. Student.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 package="com.cmania.hibernate">

   <class name="Student" table="students">
     
       <id name="sid" column="sid" type="int">
            <generator class="increment" />
       </id>
       <property name="sname"></property>
       <property name="email"></property>
       <property name="phone"></property>
       <many-to-one name="address" class="Address" column="adid" />
    </class>
   
</hibernate-mapping>


5. Address.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 package="com.cmania.hibernate">

   <class name="Address" table="addresses">
     
       <id name="adid" column="adid" type="int">
            <generator class="increment" />
       </id>
       <property name="street"></property>
       <property name="city"></property>
       <property name="state"></property>
     
    </class>     
 
</hibernate-mapping>



6. 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.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost/cmdb15</property>
          <property name="connection.username">root</property>
          <property name="connection.password">root</property>
       
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="show_sql">true</property>
          <property name="hbm2ddl.auto">update</property>
       
          <mapping resource="Student.hbm.xml"  />
          <mapping resource="Address.hbm.xml"  />
       
      </session-factory>
</hibernate-configuration>


7. Lab8Client1.java
-------------------


package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab8Client1
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Student st = new Student("Nishant", "n@gmail.com", "2323");
session.save(st);


Address ad = new Address("cm", "patna", "Bihar");
session.save(ad);

st.setAddress(ad);

tx.commit();
session.close();

} catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}
}}}


8. Lab8Client2.java
-------------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab8Client2
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Student st = (Student) session.load(Student.class, 1);
System.out.println(st.getSid()+"\t"+st.getSname()+"\t"+st.getEmail()+"\t"+st.getPhone());

Address ad = st.getAddress();
System.out.println(ad.getAdid()+"\t"+ad.getStreet()+"\t"+ad.getCity()+"\t"+ad.getState());

Address add1 = (Address) session.load(Address.class, 1);
System.out.println(add1.getAdid()+"\t"+add1.getStreet()+"\t"+add1.getCity()+"\t"+add1.getState());

tx.commit();
session.close();


}
catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}
}}}
------------------------------------------------------------------------------


          2.)   One to One Bi-Directional Mapping :-
                   Consider the relationship between Student and Address


---------------------------Hibernate Lab9 ---------------------------


1. Student.java
---------------

package com.cmania.hibernate;

public class Student
{
int sid;   // for primary key
String sname;
String email;
String phone;
Address address;
public Student(String sname, String email, String phone)
{
super();
this.sname = sname;
this.email = email;
this.phone = phone;
}

public Student()
{

}
// setters and getters
}


2. Address.java
---------------

package com.cmania.hibernate;

public class Address
{
int adid;     // for primary key.
String street;
String city;
String state;
Student student;

public Address(String street, String city, String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}

public Address()
{

}
 // setters and getters
}


3. CHibernateUtil.java
----------------------

4. Student.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 package="com.cmania.hibernate">

   <class name="Student" table="students">
     
       <id name="sid" column="sid" type="int">
            <generator class="increment" />
       </id>
       <property name="sname"></property>
       <property name="email"></property>
       <property name="phone"></property>
       <many-to-one name="address" class="Address" column="adid" />
    </class>
   
</hibernate-mapping>


5. Address.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 package="com.cmania.hibernate">

   <class name="Address" table="addresses">
     
       <id name="adid" column="adid" type="int">
            <generator class="increment" />
       </id>
       <property name="street"></property>
       <property name="city"></property>
       <property name="state"></property>
       <one-to-one name="student" class="Student"></one-to-one>
    </class>     
 
</hibernate-mapping>


6. hibernate.cfg.xml
--------------------

7. Lab9Client1.java
-------------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab9Client1
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Student st = new Student("Nishant", "n@gmail.com", "2323");
session.save(st);


Address ad = new Address("cm", "patna", "Bihar");
session.save(ad);

st.setAddress(ad);
ad.setStudent(st);

tx.commit();
session.close();

} catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}

   }}}


8. Lab9Client2.java
-------------------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab9Client2
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Student st = (Student) session.load(Student.class, 1);
System.out.println(st.getSid()+"\t"+st.getSname()+"\t"+st.getEmail()+"\t"+st.getPhone());

Address ad = st.getAddress();
System.out.println(ad.getAdid()+"\t"+ad.getStreet()+"\t"+ad.getCity()+"\t"+ad.getState());

Address add1 = (Address) session.load(Address.class, 1);
System.out.println(add1.getAdid()+"\t"+add1.getStreet()+"\t"+add1.getCity()+"\t"+add1.getState());

Student st1 = add1.getStudent();
System.out.println(st1.getSid()+"\t"+st1.getSname()+"\t"+st1.getEmail()+"\t"+st1.getPhone());

tx.commit();
session.close();


}
catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}
}

}

}

---------------------------------------------------------------------------


          3.)   One to Many Bi-Directional Mapping :-
             Consider the relationship between Customer and order. One Customer can place                     Many  Order.One order belongs one Customer.


------------------------------- Hibernate Lab10 ---------------------------


1. Customer.java
----------------


package com.cmania.hibernate;

import java.util.Date;
import java.util.Set;

public class Customer
{
int cid;   // for primary key
String cname;
String email;
Date dob;
Long phone;

Set<Order> orders;

  // setters and getters

public Customer(String cname, String email, Date dob, Long phone) {
super();
this.cname = cname;
this.email = email;
this.dob = dob;
this.phone = phone;
}

public Customer() { }

}


2. Order.java
-------------

package com.cmania.hibernate;

import java.util.Date;

public class Order
{
int orderId;     // for primary key.
int totalQty;
Double totalCost;
Date orderDate;
String status;

Customer customers;

public Customer getCustomers() {
return customers;
}

public void setCustomers(Customer customers) {
this.customers = customers;
}

public Order(int totalQty, Double totalCost, Date orderDate, String status) {
super();
this.totalQty = totalQty;
this.totalCost = totalCost;
this.orderDate = orderDate;
this.status = status;
}

public Order()
{
super();

}
// setters and getters

}


3. CHibernateUtil.java
----------------------


4. Customer.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 package="com.cmania.hibernate">

   <class name="Customer" table="customers">
     
       <id name="cid" column="cid" type="int">
            <generator class="increment" />
       </id>
       <property name="cname"></property>
       <property name="email"></property>
       <property name="dob" type="date"></property>
       <property name="phone" type="long"></property>
     
       <set name="orders">
          <key column="cid" />
             <one-to-many class="Order" />
       </set>
     
    </class>
   
</hibernate-mapping>


5. Order.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 package="com.cmania.hibernate">

   <class name="Order" table="orders">
     
       <id name="orderId" column="orderId" type="int">
            <generator class="increment" />
       </id>
     
       <property name="totalQty" type="int"></property>
       <property name="totalCost" type="double"></property>
       <property name="orderDate" type="date"></property>
       <property name="status" />
     
       <many-to-one name="customers" class="Customer" column="cid" />
   
    </class>     
 
</hibernate-mapping>


6. hibernate.cfg.xml
--------------------


7. Lab10Client1.java
--------------------

package com.cmania.hibernate;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab10Client1
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Customer cust = new Customer("funky", "n@gma.com", new Date(), new Long(7676));
session.save(cust);

Set<Order> od = new HashSet<Order>();
Order od1 = new Order(2, 200.23, new Date(), "New");
od.add(od1);
session.save(od1);
Order od2 = new Order(3, 400.23, new Date(), "Old");
od.add(od2);
session.save(od2);
Order od3 = new Order(4, 500.23, new Date(), "New");
od.add(od3);
session.save(od3);


od1.setCustomers(cust);
od2.setCustomers(cust);
od3.setCustomers(cust);

} catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}

}}}


8. Lab10Client2.java
--------------------


package com.cmania.hibernate;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab10Client2
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Customer cust = (Customer) session.load(Customer.class, 1);
System.out.println(cust.getCid()+"\t"+cust.getCname()+"\t"+cust.getEmail()+"\t"+cust.getPhone());

Set<Order> orders = cust.getOrders();
System.out.println(orders.getClass().getName());

for (Order od : orders)
{
System.out.println(od.getOrderId()+"\t"+od.getTotalQty()+"\t"+od.getTotalCost());
}

Order ord = (Order) session.load(Order.class, 2);

System.out.println(ord.getOrderId()+"\t"+ord.getTotalQty()+"\t"+ord.getTotalQty());

Customer cust1 = ord.getCustomers();
System.out.println(cust1.getCid()+"\t"+cust1.getCname()+"\t"+cust1.getEmail()+"\t"+cust1.getPhone());

tx.commit();
session.close();


}
catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}
}}}

--------------------------------------------------------------------------------

          4.)   Many to Many Bi-Directional Mapping :-


------------------------------- Hibernate Lab11 --------------------------------

1. Customer.java
----------------


package com.cmania.hibernate;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Customer
{
int cid;   // for primary key
String cname;
String email;
Date dob;
Long phone;

Set<Account> accounts;

public Customer() {
super();
// TODO Auto-generated constructor stub
}

public Customer(String cname, String email, Date dob, Long phone) {
super();
this.cname = cname;
this.email = email;
this.dob = dob;
this.phone = phone;
}
// setters and getters
}



2. Account.java
---------------

 package com.codingmania.hibernate;

import java.util.HashSet;
import java.util.Set;

public class Account
{
int accno;     // for primary key.
String atype;
Double bal;

Set<Customer> customers = new HashSet<Customer>();

// setters and getters


public Account(String atype, Double bal) {
super();
this.atype = atype;
this.bal = bal;
}

public Account() {
super();
// TODO Auto-generated constructor stub
}

}


3. CHibernateUtil.java
----------------------

4. Customer.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 package="com.cmania.hibernate">

   <class name="Customer" table="customers">
     
       <id name="cid" column="cid" type="int">
            <generator class="increment" />
       </id>
       <property name="cname" />
       <property name="email" />
       <property name="dob" type="date" />
       <property name="phone" type="long" />
     
       <set name="accounts" table="cust_acc">
          <key column="cid" />
             <many-to-many class="Account" column="accno" />
       </set>
     
    </class>
   
</hibernate-mapping>



5. Account.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 package="com.cmania.hibernate">

   <class name="Account" table="accounts">
     
       <id name="accno" column="accno" type="int">
            <generator class="increment" />
       </id>
       <property name="atype" />
       <property name="bal" type="double" />
     
       <set name="customers" table="cust_acc">
           <key column="accno" />
           <many-to-many class="Customer" column="cid" />
        </set>
     
    </class>     
 
</hibernate-mapping>



6. hibernate.cfg.xml
--------------------




7. Lab11Client1.java
--------------------


package com.cmania.hibernate;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab11Client1
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Account ac1 = new Account("SA",500.34);
session.save(ac1);
Account ac2 = new Account("SA",200.23);
session.save(ac2);
Account ac3 = new Account("CA",44.45);
session.save(ac3);

Customer cust1 = new Customer("funky", "a@cm", new Date(), new Long(7676));
session.save(cust1);

Customer cust2 = new Customer("banwaria", "b@cm", new Date(), new Long(7655));
session.save(cust2);



Set<Account> accs1 = new HashSet<Account>();
accs1.add(ac1);
accs1.add(ac2);
accs1.add(ac3);
cust1.setAccounts(accs1);

Set<Account> accs2 = new HashSet<Account>();
accs2.add(ac2);
accs2.add(ac3);
cust2.setAccounts(accs2);

tx.commit();
session.close();

} catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}}}}


8. Lab11Client2.java
--------------------


package com.cmania.hibernate;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab11Client2
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
tx = session.beginTransaction();

Customer cust = (Customer) session.load(Customer.class, 1);
System.out.println(cust.getCid()+"\t"+cust.getCname()+"\t"+cust.getEmail()+"\t"+cust.getPhone());

Set<Account> acs = cust.getAccounts();
for (Account acc : acs)
{
System.out.println(acc.getAccno()+"\t"+acc.getAtype()+"\t"+acc.getBal());
}

Account ac = (Account) session.load(Account.class, 2);
System.out.println(ac.getAccno()+"\t"+ac.getAtype()+"\t"+ac.getBal());

Set<Customer> cust1 = ac.getCustomers();
for (Customer custo : cust1)
{
System.out.println(custo.getCid()+"\t"+custo.getCname()+"\t"+custo.getEmail()+"\t"+custo.getPhone());
}


tx.commit();
session.close();


}
catch (Exception e)
{
if (tx!=null)
{
tx.rollback();

}}}}



         Important Note:
        -----------------
             1.) In the case of One-One, F.K. must be with Master Table.

             2.) In the case of One-Many, F.K. must be with Secondary Table.

             3.) In the case of Many-Many, F.K. must be with Third Table.


=================================================================================



Working with Primary Key:
-------------------------

Hibernate supports to configure both the types of Primary Keys

          1) Simple Primary Key
          2) Composite Primary Key

    -->    When you use Single column as Primary key then it is called as Simple Primary Key.
             • use<id> tag to configure Simple Primary Key.

    -->    When you use combination of two or more columns as Primary Key then it is called as              Composite Primary Key.
             • use<composite-id> tag to configure Composite Primary Key.
-------------------------------------------------------------------------------
     
    1.) Simple Primary Key:
          Hibernate core Provides various Built-In Simple Primary Key Generator follows
           1) increment(****)
           2) sequence(****)
           3) hilo
           4) seqhilo
           5) uuid

         Above names are called Short-cut names for Generator classes.
       


For every built-In generator, one generator class in implemented and provided in the          package called org.hibernate.id and all the generator classes are sub classes of          Identifier Generator.


----------------
Using increment:  (for integer type only)
================

<id name= “sid”, column= “sid” type= “int”>
<generator class= “increment”/>
</id>

//   it generates the generator class of corresponding class of increment.

<id name= “sid”, column= “sid” type= “int”>
<generator class= “org.hibernate.id.IncrementGenerator”/>
</id>

Note:
-----
1. starts with 1 and increment by 1.
        2. used only for integer type of columns.





----------------- Hibernate Lab12 ------As u done previous ---------------


-----------
Using uuid:  (for String type only)
===========

<id name= “sid” column= “sid” type= “string”>
<generator class= “uuid”/>
</id>

Note:
=====
  1.  Collects the IP address, timestamp and converts to hexadecimal and converts to string.
  2.  used only for character type columns. 

-------------------------- Hibernate Lab13 --------------------------------

1. Request.java
---------------

package com.cmania.hibernate;
import java.util.Date;

public class Request
{
private String reqId;
private String cid;
private String message;
private Date reqDate;
private int version;

public Request()
{
// TODO Auto-generated constructor stub
}

public Request(String cid, String message, Date reqDate)
{
super();
this.cid = cid;
this.message = message;
this.reqDate = reqDate;
}
// setters and getters
}

2. CHibernateUtil.java
----------------------

3. Request.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 package="com.cmania.hibernate">

   <class name="Request" table="requests">
 
       <id name="reqId" column="reqId" type="string">
          <generator class="uuid">
          </generator>
       </id>
     
       <version name="version" type="integer"></version>
     
       <property name="cid"></property>
       <property name="message"></property>
       <property name="reqDate" type="date"></property>
     
   </class>

</hibernate-mapping>

4. Lab13.java
-------------

package com.codingmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab13
{
public static void main(String[] args)
{
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
/*
Request re = new Request("c101", "funky", new Date());
String st =  (String) session.save(re);
System.out.println(st);
*/

Request req = (Request) session.load(Request.class, "402881883fbd5f28013fbd5f2aa30001");
req.setMessage("Ok Ok");

tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
 }}}

5. hibernate.cfg.xml
--------------------

----------------------------------------------------------------------------------


---------------
Using sequence:  for oracle only not for mysql
===============
<id name= “sid”, column= “sid” type= “int”>
<generator class= “sequence”>
<param name= “sequence”> SID_SEQ</param>
</generator>
</id>

Note : Used only for integer type columns.

----------------------------- Hibernate Lab14 ----------------------------------


-----------
Using hilo: (same as sequence) used only for integer type of column
===========
<id name= “cid”, column= “cid” type= “int”>
<generator class= “hilo”>
<param name= “table”> hi_value</param>
<param name= “column”>next_value</param>
<param name= “max_lo>10</param>
</generator>
</id>

----------------------------- Hibernate Lab15 ----------------------------------

1. Customer.java
----------------

package com.cmania.hibernate;

import java.sql.Timestamp;

public class Customer
{
private int cid;
private String cname;
private String email;
private long phone;
private Timestamp tstamp;

public Customer()
{
// TODO Auto-generated constructor stub
}

public Customer(String cname, String email, long phone) {
super();
this.cname = cname;
this.email = email;
this.phone = phone;
}
// setters and getters

}

2. CHibernateUtil.java
----------------------

3. Customer.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 package="com.cmania.hibernate">

   <class name="Customer" table="customers">
   
       <id name="cid" column="cid" type="int">
          <generator class="hilo">
               <param name="table">hi_value</param>
               <param name="column">next_value</param>
               <param name="max_lo">10</param>
          </generator>
       </id>
     
       <timestamp name="tstamp"></timestamp>
     
       <property name="cname"></property>
       <property name="email"></property>
       <property name="phone" type="long"></property>
     
   </class>

</hibernate-mapping>

4. Lab15
--------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab15
{
public static void main(String[] args)
{
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

Customer cu = new Customer("nidhi", "nd@cm", 7677);
Integer it = (Integer) session.save(cu);

System.out.println(it.intValue());

tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
}}}

5. hibernate.cfg.xml
--------------------


--------------
Using seqhilo:  used only for integer type of column
==============

<id name= “sid”, column= “sid” type= “int”>
<generator class= “seqhilo”>
<param name= “sequence>SID_SEQ</param>
<param name= “max_lo>100</param>
</generator>
</id>

---------------------------- Hibernate Lab16 ----------------------------------

 ======================
| Custom Id Generators |
 ======================


If these Built-In Id Generators are not suitable for your requirements then you can write your own Custom ID Generators.Steps to write Custom ID Generators:

1) Write your generator by implementing IdentifierGenerator interface which is in         org.hibernate.id package.

2) Override the generate() method as follows public Serializable generate                (SessionImplementor si, Object obj) throws HibernateException.

3) Write the required id generation logic in generate() method.

4) <id name= “sid”, column= “sid” type= “string”>
            <generator class= “com.cm.id.SIDGenerator”/>
        </id>


-------------------------- Hibernate Lab17 ----------------------------

1. Student.java
---------------

package com.cmania.hibernate;

public class Student
{
private String sid;   // for primary key

private String sname;
private String email;
private String phone;

  public Student()
  {  }

public Student(String sname, String email, String phone) {
super();
this.sname = sname;
this.email = email;
this.phone = phone;
}
// setters and  getters
}


2. SIDGenerator.java
--------------------

package com.cmania.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

public class SIDGenerator implements IdentifierGenerator
{
public Serializable generate(SessionImplementor si, Object obj) throws HibernateException
{
Transaction tx = null;
String sid = "CMI-001";
try
{
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
tx = s.beginTransaction();

Query q1 = s.createQuery("from Student stu");
int size = q1.list().size();

if (size != 0)
{
Query query = s.createQuery("select max(stu.sid) from Student stu");
List list = query.list();
System.out.println("Total size of list = "+list.size());

Object o1 = list.get(0);
System.out.println("Element = "+o1);

String id= "";
id  = o1.toString();

String p2 = id.substring(4);
int x = Integer.parseInt(p2);

x = x+1;

if (x<=9)
{
sid = "CMI-00"+x;

}
else if (x<=99)
{
sid = "CMI-0"+x;

}
else if (x<=999)
{
sid = "CMI-"+x;
}}
}
catch (Exception e)
{
e.printStackTrace();
}
return sid;
}}

3. CHibernateUtil.java
----------------------

4. Student.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 package="com.cmania.hibernate">

   <class name="Student" table="mystudents">
 
   <id column="sid" name="sid" type="string">
       <generator class="com.cmania.hibernate.SIDGenerator" />
   </id>
 
       <property name="sname"></property>
       <property name="email"></property>
       <property name="phone"></property>
     
   </class>
 
</hibernate-mapping>


5. Lab17
--------

package com.cmania.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Lab17
{
public static void main(String[] args)
{
try
{
SessionFactory sf = CHibernateUtil.geFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

Student stu1 = new Student("kkk", "ck@cm", "5455");
session.save(stu1);

tx.commit();
session.close();

}
catch (Exception e)
{
e.printStackTrace();
 }}}


6. hibernate.cfg.xml
--------------------

-------------------------------------------------------------------------------------


Hibernate Query Language
------------------------

if you want to write simple queries without applying any conditions i.e directly

inserting
deleting
updating
selecting

all values at a time then directly just take help of those methods and write those.

4 methods of session for doing the CURD operation.

------------------------------------------------------------
session.save()          ----------     insert
session.update()        ----------     update
session.load()          ----------     retrieve or select
session.delete()        ----------     delete
-------------------------------------------------------------

but the problems came when some condition is applied by us within query then for that
reason we used HQL.

Hibernate supports various query languages to select the records based on various criteria
or conditions.
------------------------------------------------
1. HQL ( Hibernate Query Language )
2. QBC ( Query by Criteria )
3. Native Queries
------------------------------------------------
consider the table called students which is mapped with persistence class called Student

students -- sid,sname,email,branch1
Student  -- sid,sname,email,branch

-----------Display all the students-------------

SQL
---

select sid,sname,email from students stu;

------------------------------------------------


1. HQL (Based on object oriented)
------
don't directly write here sql
you have to use hql,which is internally converted to sql

-----------------------------------
String hql = from Student stu;
Query q = session.createQuery(hql);
List<Student> list = q.list();
-----------------------------------


2. QBC (Query By Criteria)
------

Criteria ct = session.createCriteria(Student.class);
List<Student> list = ct.list();


3. Native Queries
-----------------

String sql = "select {stu.*} from students stu";
SQLQuery sq = session.createSQLQuery(sql);
sq.addEntity("stu",Student.class);
List<Student> list = sq.list();


---------------------------------------------------------------------------------



Transaction Management (V.V.V.V.V.Imp.):
----------------------------------------

Transaction is the process of performing multiple database operations as one Atomic Unit         with All-Nothing Criteria.i.e, When All the database operations is the unit are         successful then Transaction is successful and should be commited.
     
     When Any one database operation is the unit is failed then Transaction is failed and should      be rolled back.When you implement Transactions properly in your application. It guarantees      ACID Properties.

A ----------- Atomicity
C ----------- Consistency
I ----------- Isolation
D ----------- Durability

       Atomicity
       ---------

    Atomicity = Def of Transaction

          Consider the Salary transfer.
 
             1000 deposits + 1 Withdraw = 1001.
   
       Consistency
       -----------
   When you are running the transaction, you may enforce may business rules as per the            application requirement. Your application should be consistent when any business rule            is failed. Otherwise you may get some inconsistent results.Consider the withdraw                 results.Some business rules are.
           
             1) Min bal is 5K.
             2) if(ATM){
          a) Only 5 Withdraw per day.
                b) Limit: 50K per day.
        }
             3) if(Branch){
                a) Only 10 withdraw per day.
                b) Limit: 5L per day.
                }

       Isolation
       ---------
        You can run multiple transactions concurrently. These concurrently running multiple             transactions should not disturb other transactions. i.e, multiple transactions             should run isolatley.

            Case A:
            -------
              Where multiple transactions running concurrently are using multiple Account rows                 of accounts table.
                Accno: 99
                Accno: 88
                Accno: 77
                At a time, following operations are happening.
               
                   1) Transfer(withdraw) – uses99
                   2) Bank teller(withdraw/deposit) – uses98
                   3) Loan EMI(withdraw) – uses77

             Case B:
             -------
         Where multiple transactions running concurrently are using single Account row                  of accounts table.
                         Accno: 99
                         At a time, following operations are happening.
                   1) Transfer(withdraw) – uses99
                   2) Bank teller(withdraw/deposit) – uses99
                   3) Loan EMI(withdraw) – uses99
                 In case B, 3 transactions are running concurrently and using single                              column/row/table which may cause Transactional Concurrency Problems.
             
        1) Dirty Read Problem
        2) Repeatable Read Problem
        3) Phantom Read Problem

             To avoid these problems, you have to apply one of the following required                         Transactional Isolation Levels.
               1) READ_UNCOMMITED 1
               2) READ_COMMITED 2
               3) REPEATABLE_READ 4
               4) SERIALIZABLE 8

          Durability:
          -----------

          You enterprise data should be available for long time (as long as your                           enterprise is running). You have to protect your enterprise data from crashes,                   failures. You have to implement proper backup and you can make your enterprise                   data durable with recovery mechanism and proper logging mechanism.
        Every one Min Backup System.
    10:31 AM ……..First Backup System
        10:31:53 AM ……..but crashed at this point.
    10:32 AM ……..next backup












No comments:

Post a Comment

JSP interview questions and answers

Q1. What is JSP and why do we need it? JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. J...