Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Advantage of Hibernate over JDBC

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Advantage of Hibernate over JDBC

    Ok. Lets assume there are 100’s of tables say Table1, Table2 ….Table100. The tables contain details of employees.

    And say Table1 contains ‘a1’,’b1’,’c1’…’z1’
    Table2 contains ‘a2’,’b2’,’c2’………’z2’
    and so on till Table100

    Now let see the implementation in JDBC and Hibernate. Lets see where the advantage is, in Hibernate(Unfortunately I don't seem to find any reduction in code). The question is below the code.

    JDBC Code:
         sql1= "INSERT INTO Table1 VALUES (‘a1’,…………….)”;
          sql1 += "INSERT INTO Table1 VALUES (‘b1’,……………)”;
          sql1+ = "INSERT INTO Table1 VALUES(‘c1’,……………..)”;
          stmt.executeUpdate(sql1);
     
          sql2= "INSERT INTO Table2 VALUES (‘a2’,…………….)”;
          sql2 += "INSERT INTO Table2 VALUES (‘b2’,……………)”;
          sql2+ = "INSERT INTO Table2 VALUES(‘c2’,……………..)”;
          stmt.executeUpdate(sql2);
     
         and so on till 100 tables

    Hibernate Code:
     
    ******Integer empID1Table1 = ME. addNameToTable1(“a1”, ………….);
    ******Integer empID2Table1 = ME. addNameToTable1(“b1”,………….. );
    ******Integer empID3Table1 = ME. addNameToTable1(“c1”,…………….);
     
    *     Integer empID1Table2 = ME. addNameToTable2(“a2”, ………….);
    ******Integer empID2Table2 = ME. addNameToTable2(“b2”,………….. );
    ******Integer empID3Table2 = ME. addNameToTable2(“c2”,…………….);
     
        and so on
     
    public Integer addNameToTable1(String name, …….){
          Session session = factory.openSession();
          Transaction tx = null;
          Integer employeeID = null;
          try{
             tx = session.beginTransaction();
             Table1 table1 = new Table1(name, ……………);
             employeeID = (Integer) session.save(table1); 
             tx.commit();
          }catch (HibernateException e) {
             if (tx!=null) tx.rollback();
             e.printStackTrace(); 
          }finally {
             session.close(); 
          }
          return employeeID;
       }
     
    public Integer addNameToTable2(String name, …….){
          Session session = factory.openSession();
          Transaction tx = null;
          Integer employeeID = null;
          try{
             tx = session.beginTransaction();
             Table2 table2 = new Table2(name, ……………);
             employeeID = (Integer) session.save(table2); 
             tx.commit();
          }catch (HibernateException e) {
             if (tx!=null) tx.rollback();
             e.printStackTrace(); 
          }finally {
             session.close(); 
          }
    and so on till 100 functions

    Now, my question is where is the advantage of code reduction in Hibernate when compared to JDBC? I am aware of other advantages of Hibernate like lazy loading, transaction handling, caching but just don’t understand the code reduction in Hibernate because I don’t see it.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: Advantage of Hibernate over JDBC

    This thread has been cross posted here:

    http://www.java-forums.org/hibernate/88089-advantage-hibernate-over-jdbc.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3

    Default Re: Advantage of Hibernate over JDBC

    Developer has to write code in JDBC to map an object model's data representation to a relational data model and its corresponding database schema.

    The mapping of Java objects with database tables has to be taken care of in JDBC. Hibernate provides transparent persistence and therefore there is no need to map database tables tuples to application objects during interaction with RDBMS.

Similar Threads

  1. hibernate
    By usharani13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2013, 03:28 PM
  2. Hibernate Advantages over JDBC
    By noorul11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2013, 11:25 PM
  3. Replies: 2
    Last Post: January 12th, 2013, 11:16 AM
  4. Hibernate
    By kchandan38 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2012, 06:48 AM
  5. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Java Servlet
    Replies: 2
    Last Post: November 21st, 2011, 10:54 PM