Ok. Lets assume there are 100s 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:
and so on till 100 functions******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(); }
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 dont understand the code reduction in Hibernate because I dont see it.