java - Hibernate缺少提交和回滚?

标签 java hibernate

I'm following this guide并在提交和回滚上给我一个错误。 tx 唯一的方法是 getStatus() getTimeout() 和 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;

    }

Cannot resolve method Rollback()

Cannot resolve method Commit()

最佳答案

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

org.hibernate.Transaction 扩展自 javax.persistence.EntityTransaction 接口(interface),并且 rollbackcommit 方法均来自该接口(interface)。

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

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

关于java - Hibernate缺少提交和回滚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525896/

相关文章:

java - 在 Java 中使用字符串变量作为数学函数

java - 当我尝试使用 WHERE 子句时出现异常

java - 使用 Javamail 从 Gmail 下载 Word 文档

java - HttpURLConnection 收到 405 但服务已完成

hibernate - 使用不同的别名多次加入关联

java - 安全生成每天重置的顺序订单号

java - 曼宁·斯普林 (Manning Spring),Hibernate 封面图片

java - 如何通过hibernate生成namedQuery?

java - 在 System.out.println 中使用 charAt 显示整数?

java - 用减号而不是括号格式化美元的负数(Java)