代码如下:
  public static void main(string[]args)
{
   SessionFactory sf=new configration().configure().buildSessionFactory();
   Session session = sf.OpenSession();
   Medal medal = new medal();
   medal.setOwner("totong");
   medal.setType("gold medal");
   Session.save(user);
   Session.close();
}

解决方案 »

  1.   

    你没有开启事务
     public static void main(string[]args)
    {
      SessionFactory sf=new configration().configure().buildSessionFactory();
      Session session = sf.OpenSession();
      Transaction  transaction = session.beginTransaction();
      Medal medal = new medal();
      medal.setOwner("totong");
      medal.setType("gold medal");
      Session.save(user);
      transaction.commit();
      Session.close();
    }
      

  2.   

    1.没开启食物;
    2.Session.save(user);user哪里来的啊?
      

  3.   

    可以看出lz用的hibernate在这里事务必须的开启的,或者你用spring的依赖注入给每个方法注入事务也行
      

  4.   

    hibernate自己的close方法不会调用flush,直接关闭会导致之前的操作同步不到数据库中.
    session.flush();
    session.close();