这是我的代码:
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();
}
building_master
和branch_master