如何使用hibernate框架从下拉列表中获取所选值的id

时间:2015-03-07 17:27:37

标签: java hibernate

这是我的代码:

    Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction t=null;
    String department=request.getParameter("department");    
    String name=request.getParameter("name");
    try
    {
        t=s.getTransaction();
        t.begin();
        int bid=0;
        Criteria criteria=s.createCriteria(BuildingMaster.class);
        criteria.add(Restrictions.eq("Build_Name",department));
        BuildingMaster bm=(BuildingMaster)criteria.uniqueResult();
        out.println("<script type=\"text/javascript\">");  
        out.println("alert('Inserted Successfully');");  
        out.println("</script>");

        if(bm!=null)
        {
            bid=bm.getId();
            branch_master bm1=new branch_master(name,bid);
            s.save(bm1);
            out.println("<script type=\"text/javascript\">");  
            out.println("alert('Inserted Successfully');");  
            out.println("</script>");
            s.flush();
            t.commit();
        }
        else
        {
            out.println("<script type=\"text/javascript\">");  
            out.println("alert('fail');");  
            out.println("</script>");

        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    finally
    {
        s.close();
    }
  1. 我必须获取下拉选择值的ID并插入此ID 另一张桌子。怎么可能?
  2. 我有两张表building_masterbranch_master
  3. 我也尝试使用具有唯一结果的sql查询,但它总是返回0.所以 我使用了标准,但它也没有用。

0 个答案:

没有答案