Hibernate insert problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bernouli
    New Member
    • Oct 2012
    • 26

    Hibernate insert problem

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

    Code:
    
    <%!
    int empno;double salary;String name; Session session1 = null;
    %>
    <body>
    <%
    String num1=request.getParameter("t1");
    if(num1 != null)
    {
    empno=Integer.parseInt(num1);
    name=request.getParameter("t2");
    String sal=request.getParameter("t3");
    salary=Integer.parseInt(sal);
    try
    {
    Configuration cf=new Configuration();
    cf.configure();
    SessionFactory sessionFactory = cf.buildSessionFactory();
    session1 =sessionFactory.openSession();
    Transaction tr = session1.beginTransaction();
    Emp e=new Emp(empno,name,salary);
    tr.commit();
    session1.save(e);
    
    session1.close();
    out.println("<h1>Data Inserted Successfully</h1>");
    }
    catch(Exception e)
    {
    System.out.println("e="+e.getMessage());
    }
    }
    %>
    
    <form>
      <table width="352" border="1">
        <tr>
          <th>Emp Number</th>
          <td><input name="t1" type="text"></td>
        </tr>
        <tr>
          <th> Name </th>
          <td><input name="t2" type="text"></td>
        </tr>
        <tr>
          <th>Salary </th>
          <td><input name="t3" type="text"></td>
        </tr>
        <tr>
          <th colspan="2"><input type="submit"value="Submit" >
          </th>
        </tr>
      </table>
    </form>
    </body>
    </html>
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    Working...