Hibernate缺少提交和回滚?

时间:2017-10-02 12:54:22

标签: java hibernate

I'm following this guide并在Commit and Rollback上给我一个错误。 tx唯一的方法是getStatus() getTimeout() and setTimeOut(),我不知道如何修复它。

import com.atp.servlets.GeneralSetup;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; 

public Integer addGeneralSetup(String name) {
        Session session = factory.openSession();
        Transaction tx = null;
        Integer employeeID = null;
        try{
            tx = session.beginTransaction();
            GeneralSetup employee = new GeneralSetup();
            employeeID = (Integer) session.save(employee);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
        return employeeID;

    }
  

无法解析方法Rollback()

     

无法解析方法Commit()

1 个答案:

答案 0 :(得分:0)

您的类路径中似乎没有javax.persistence API库。

org.hibernate.Transactionjavax.persistence.EntityTransaction界面延伸,rollbackcommit方法来自那里。

尝试将此依赖项添加到您的pom.xml

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>