By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,783 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,783 IT Pros & Developers. It's quick & easy.

Hibernate insert problem

P: 26
can someone tell me what is wrong with this code.
it prints "Data Inserted Successfully" but does not insert the values to database

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <%!
  4. int empno;double salary;String name; Session session1 = null;
  5. %>
  6. <body>
  7. <%
  8. String num1=request.getParameter("t1");
  9. if(num1 != null)
  10. {
  11. empno=Integer.parseInt(num1);
  12. name=request.getParameter("t2");
  13. String sal=request.getParameter("t3");
  14. salary=Integer.parseInt(sal);
  15. try
  16. {
  17. Configuration cf=new Configuration();
  18. cf.configure();
  19. SessionFactory sessionFactory = cf.buildSessionFactory();
  20. session1 =sessionFactory.openSession();
  21. Transaction tr = session1.beginTransaction();
  22. Emp e=new Emp(empno,name,salary);
  23. tr.commit();
  24. session1.save(e);
  25.  
  26. session1.close();
  27. out.println("<h1>Data Inserted Successfully</h1>");
  28. }
  29. catch(Exception e)
  30. {
  31. System.out.println("e="+e.getMessage());
  32. }
  33. }
  34. %>
  35.  
  36. <form>
  37.   <table width="352" border="1">
  38.     <tr>
  39.       <th>Emp Number</th>
  40.       <td><input name="t1" type="text"></td>
  41.     </tr>
  42.     <tr>
  43.       <th> Name </th>
  44.       <td><input name="t2" type="text"></td>
  45.     </tr>
  46.     <tr>
  47.       <th>Salary </th>
  48.       <td><input name="t3" type="text"></td>
  49.     </tr>
  50.     <tr>
  51.       <th colspan="2"><input type="submit"value="Submit" >
  52.       </th>
  53.     </tr>
  54.   </table>
  55. </form>
  56. </body>
  57. </html>
  58.  
Mar 1 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
1.) Don't mix java with HTML. Use plain java classes for the hibernate parts.
2.) You are committing the transaction before saving the data.
Mar 6 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.