xaa xaa - 1 year ago 154
Java Question

Hibernate missing Commit and Rollback?

I'm following this guide and gives me an error on

Commit and Rollback
. The only methods
tx
has is
getStatus() getTimeout() and setTimeOut()
and I have no idea how to fix it.

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()

Answer Source

It seems that you do not have javax.persistence API library in your classpath.

org.hibernate.Transaction extends from javax.persistence.EntityTransaction interface and the rollback and commit methods are from there.

Try adding this dependency to your pom.xml

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download