Friday 10 February 2012

Getting Started with Hibernate

    My class name is 'mycontact'.It is a public class and I didn't use package to place this class. It is a value bean


  In that class I declared three fields(
Id,name and phone). Each of them is  String type.


 This class has a no-argument consructor and a parameterized  constructor with three parameters to initialize  Id, name and phone. And setter / getter methods for each field.
----------------------------------------------


  There is a hibenate.properties file In which we have to mention the dialect, driver  and the url of the mycontactstable.


hibernate.dialect  =   
        net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class   =  
        com.mysql.jdbc.Driver
hibernate.connection.url     =      
        jdbc:mysql://localhost/mycontacts
hibernate.connection.username  =   root
hibernate.connection.password
----------------------------------------------


  and the third important file is mapping file  (mycontact.hbm.xml). I have shown the essential elements.


<hibernate-mapping>     
    <class name="mycontact"                table="mycontactstable">
     <id    name="id"               column="ID"  unsaved-value="0">
          
               <generator class="assigned"/>


          </id>


          <property name="name"                      type="string"  />
          <property name="phone"                     type="string" />
</class>

</hibernate-mapping>
=======================================
( I have not shown the DTDeclaration).


The most important part is setting the classpath.
It should have the following jar files.


    1. cglib-full-2.0.2.jar;


    2. commons-collections-2.1.1.jar;


    3. commons-logging-1.0.4.jar;

    4. dom4j-1.4.jar;


    5. ehcache-0.9.jar;


    6. jdbc2_0-stdext.jar;


    7. jta.jar;


    8. log4j-1.2.8.jar;


    9. xerces-2.4.0.jar;


    10. xml-apis.jar;


    11. hibernate2.jar;


    12. commons-pool-1.2.jar;


    13. commons-dpcp-1.2.1.jar;


    14. mysqldriver.jar;   


All these files are available in hibernate\lib.
So we set the classpath accordingly.
=======
     I then created a bean for using hibernate.
As in my previous blog, I think , best thing is to   mention the operation also in the bean's method.
    The method name is 'edit'.
It has 4 arguments. ( namely name, phone, id and operation).
------------------------------------------
   There is a common portion for all the operations. like:


The function is:
  public String  edit
     ( String  a, String b, String c, String d)
----
   a   is  name
   b   is  phone
   c   is  Id
   d   is  operation.
---------------------------------------- 


SessionFactory  factory  =  null;
System.out.println("Creating session factory");




String   r="";
   try
   {
 Configuration     cfg =     new Configuration();
 cfg.addClass(mycontact.class);


 factory = cfg.buildSessionFactory();
 System.out.println("sessionfactory ready");


 Transaction tx = null;


 Session session = factory.openSession();
                                 
 tx   =  session.beginTransaction();


***************************************************************************************************
And  for a add record,




mycontact  mycontact1 = new mycontact(a,b,c);
   session.save(mycontact1);
------------------------------------------


for removing,


mycontact  mycontact1    =
     (mycontact)session.get(mycontact.class,c);
session.delete(mycontact1);


------------------------------------------
to find a record,




mycontact  mycontact1 =
         (mycontact)session.get(mycontact.class,c);
String n=mycontact1.getName();
String m=mycontact1.getPhone();
System.out.println(n+"\t"+m);
 r=n+"......."+m;


------------------------------------------
to show all the records,


java.util.List list1  =  session.find("from mycontact");
Iterator i=list1.iterator();


                 while(i.hasNext())
                 {
      mycontact mycontact1 = (mycontact)i.next();
      r=r+mycontact1.getId()+"\t";
      r=r+mycontact1.getName()+"\t";
      r=r+mycontact1.getPhone()+"\t";
                 }


------------------------------------------
and for modifying,                


 mycontact  mycontact1 =
     (mycontact)session.get(mycontact.class,c);
               
    mycontact1.setName(a);
    mycontact1.setPhone(b);
************************************************
After carrying out the operation,
 the session and transaction are to be properly handled as shown.


               tx.commit();


               session.flush();
               session.close();
==============================================
  After creating the bean and compiling, I created a simple console program as shown below.


 besthiberbean   bean  = new besthiberbean();
 String           r    = bean.edit(a,b,c,d);




System.out.println(r);
===
   I got correct results.
( >java besthiberbean    gopal 34251 200   add
etc).
***
   Now, our component is ready! After trying with a package, I can use it anywhere, be it frame or servlet or jsp or struts!


That is for integration of hibernate with Struts.
*************************************************