Hibernate not saving Object in the Database?

后端 未结 1 1887
南笙
南笙 2021-01-20 16:34

I have the following problem, when trying to insert an object in the database Hibernate gets stuck, no error returned, the object is not saved correctly.

Debugging i

1条回答
  •  被撕碎了的回忆
    2021-01-20 17:16

    I did'nt see you that flushing your session

    Session session = sessionFactory.openSession();
    session.save(mc);
    session.flush();
    session.close();
    

    But most preferable is

    Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(mc);
            tx.commit(); // Flush happens automatically
        }
        catch (RuntimeException e) {
            tx.rollback();
            throw e; // or display error message
        }
        finally {
            session.close();
        }
    

    0 讨论(0)
提交回复
热议问题