org.hibernate.transactionexception 不支持嵌套事务我做错了什么?

org.hibernate.transactionexception nested transactions not supported What am I doing wrong?

我不知道为什么会出现此错误:

javax.faces.FacesException: #{loginBean.login}: org.hibernate.TransactionException: nested transactions not supported

public class UsuarioDaoImpl implements UsuarioDao {

@Override
public FwUsuarios findByUsuario(FwUsuarios usuario) {
  FwUsuarios model = null;

 System.out.println("El usuario es : "+usuario.getClaveUsuario());
 String sql = "FROM FwUsuarios WHERE claveUsuario='"+usuario.getClaveUsuario()+"'";
 Session sesion = HibernateUtil.getSessionFactory().getCurrentSession();
  try{

      sesion.beginTransaction();
      model=(FwUsuarios) sesion.createQuery(sql).uniqueResult();
      sesion.beginTransaction().commit();
  }
  catch(Exception e){
  sesion.beginTransaction().rollback();
  }

  return model;
}

@Override
public FwUsuarios login(FwUsuarios usuario) {
    FwUsuarios model = this.findByUsuario(usuario);
    if(model!= null){
    if(usuario.getPasswordUsuario().equals(model.getPasswordUsuario()))
        model = null;
    }
    return model;
}

}



public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (HibernateException ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

多次不必要地调用 beginTransaction 是问题所在。相反,应该调用它一次,并在以后使用存储的引用:

org.hibernate.Transaction tx;
//...
tx = session.beginTransaction();
//.. some database work here
tx.commit();
//when something went wrong
tx.rollback();