Hibernate transaction not successfully started

后端 未结 7 2045
梦谈多话
梦谈多话 2020-12-31 00:15

Consider this simple Hibernate scenario:

session = getHibernateSession();
tx = session.beginTransaction();
SomeObject o = (SomeObject) session.get(SomeObject         


        
7条回答
  •  感情败类
    2020-12-31 01:06

    Above solutions were not helpful for me and that is why I want to share my solution.

    In my case, I was not using @Column annotation properly in one of my entity. I changed my code from

    @Column(columnDefinition = "false") 
    private boolean isAvailable;
    

    to

    @Column(columnDefinition = "boolean default false") 
    private boolean isAvailable;
    

    And it worked.

    My create method in dao

    public int create(Item item) {
        Session session = sessionFactory.getCurrentSession();
        try {
    
              int savedId = (int) session.save(item);
              return savedId;
            } catch (Exception e) {
              e.printStackTrace();
              session.getTransaction().rollback();
              return 0; //==> handle in custom exception
            }
    }
    

提交回复
热议问题