0

我正在操作类中编写代码以在数据库中插入一条记录,但不会同时提交它,因为我想稍后在使用不同的操作类插入一些其他记录后提交它。我是休眠新手,不知道该怎么做。请指导我。我正在使用struts 2。在此先感谢。我在操作中使用以下代码在数据库中插入记录而不提交

    public void addExamDetails( ExamDetails examDetails){
       try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            session.save(examDetails);
       } catch (Exception e) {
            System.out.println("ExamDetailsDAO.addExamDetails()");
            e.printStackTrace();
       }
    }

在此之后,在另一个动作类中,我正在编写以下代码来插入其他记录并同时提交,但我无法提交之前的插入

public void addExamShift( ExamShift examShift){
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.getTransaction();
            transaction.begin();
            session.save(examShift);
            transaction.commit();
        } catch (Exception e) {
            System.out.println("ExamShiftDAO.addExamShift()");
            e.printStackTrace();
        }
    }

好的,我正在发布这两个动作。请指导我。这是插入考试详细信息的第一个操作,但未进行提交。

package com.zealsofts.schoolmanagement;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zealsofts.schoolmanagement.hibernate.ExamDetails;
import com.zealsofts.schoolmanagement.hibernate.ExamDetailsDAO;

public class AddExamDetailsAction extends ActionSupport implements ModelDriven<ExamDetails> {

    private ExamDetails examDetails = new ExamDetails();
    private ExamDetailsDAO examDetailsDAO = new ExamDetailsDAO();

    private int examCode;

    public ExamDetails getModel(){
        return examDetails;
    }

    public String execute(){
        String result = ERROR;
        try{
            examDetailsDAO.addExamDetails(examDetails);
            result = SUCCESS;
        }
        catch (Exception e) {
            System.out.println("AddExamDetailsAction.execute()");
            e.printStackTrace();
        }
        return result;
    }

    public int getExamCode(){
        return examCode;
    }

    public void validate(){
        if(examDetails.getExamName().trim().equals("") ){
            addFieldError("examName", "Exam Name is required");
        }
        if(examDetails.getAcademicSession().equals("-1")){
            addFieldError("academicSession", "Session is Required");
        }

    }

}

在此之后,调用以下操作以插入考试的班次详细信息。它是两个事务的提交发生的地方。

package com.zealsofts.schoolmanagement;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zealsofts.schoolmanagement.hibernate.ExamShift;
import com.zealsofts.schoolmanagement.hibernate.ExamShiftDAO;

public class AddShiftDetailsAction extends ActionSupport implements ModelDriven<ExamShift> {

    private ExamShift examShift = new ExamShift();

    private ExamShiftDAO examShiftDAO = new ExamShiftDAO();

    private SessionFactory sessionFactory;
    private Session session;

    private boolean shiftError;
    private boolean startHourError;
    private boolean startMinuteError;
    private boolean startTimeSuffixError;
    private boolean endHourError;
    private boolean endMinuteError;
    private boolean endTimeSuffixError;

    private boolean validationFailed;


    public ExamShift getModel(){
        return examShift;
    }

    public String execute(){
        String result = ERROR;
        try{

            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.getCurrentSession();
            examShiftDAO.addExamShift(examShift);
            result = SUCCESS;
        }
        catch (Exception e) {
            System.out.println("AddShiftDetailsAction.execute()");
            e.printStackTrace();
        }
        return result;
    }

    public boolean getShiftError(){
        return shiftError;
    }
    public boolean getStartHourError(){
        return startHourError;
    }
    public boolean getStartMinuteError(){
        return startMinuteError;
    }
    public boolean getStartTimeSuffixError(){
        return startTimeSuffixError;
    }
    public boolean getEndHourError(){
        return endHourError;
    }
    public boolean getEndMinuteError(){
        return endMinuteError;
    }
    public boolean getEndTimeSuffixError(){
        return endTimeSuffixError;
    }
    public boolean getValidationFailed(){
        return validationFailed;
    }

    public void validate(){
        try{
            if(examShift.getShift() == 0 ){
                shiftError = true;
                validationFailed = true;
                addFieldError("shift", "Exam Shift is required");
                System.out.println("Action : shift");
            }
            if(examShift.getStartHour() == -1){
                startHourError = true;
                validationFailed = true;
                addFieldError("startHour", "Start Time is Required");
                System.out.println("Action : startHour");
            }
            if(examShift.getStartMinute() == -1){
                startMinuteError = true;
                validationFailed = true;
                addFieldError("startMinute", "Start Time is Required");
                System.out.println("Action : startMinute");
            }
            if(examShift.getStartTimeSuffix().equals("-1")){
                startTimeSuffixError = true;
                validationFailed = true;
                addFieldError("startTimeSuffix", "Start Time is Required");
                System.out.println("Action : startTimeSuffix");
            }
            if(examShift.getEndHour() == -1){
                endHourError = true;
                validationFailed = true;
                addFieldError("endHour", "End Time is required");
                System.out.println("Action : endHour");
            }
            if(examShift.getEndMinute() == -1){
                endMinuteError = true;
                validationFailed = true;
                addFieldError("endMinute", "End Time is required");
                System.out.println("Action : endMinute");
            }
            if(examShift.getEndTimeSuffix().equals("-1")){
                endTimeSuffixError = true;
                validationFailed = true;
                addFieldError("endTimeSuffix", "End Time is required");
                System.out.println("Action : endTimeSuffix");
            }
        }
        catch (Exception e) {
            System.out.println("AddShiftDetailsAction.validate()");
            e.printStackTrace();
        }
    }

}

后面的插入(Shift Details)已成功提交,但前一个(Exam Details)未提交。以下是我在数据库中插入这些记录的两个 DAO

package com.zealsofts.schoolmanagement.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ExamDetailsDAO {

    SessionFactory sessionFactory;
    Session session;
    Transaction transaction;

    public List<ExamDetails> getExamDetails(){
        List<ExamDetails> examDetails = new ArrayList<ExamDetails>();
        try{
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            examDetails = session.createQuery("FROM ExamDetails").list();
        }
        catch (Exception e) {
            System.out.println("ExamDetailsDAO.getExamDetails()");
            e.printStackTrace();
        }
        return examDetails;
    }

    public void addExamDetails( ExamDetails examDetails){
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            session.save(examDetails);
            transaction.commit();
        } catch (Exception e) {
            System.out.println("ExamDetailsDAO.addExamDetails()");
            e.printStackTrace();
        }
    }
    public void editExamDetails( ExamDetails examDetails ){
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            transaction.commit();
        } catch (Exception e) {
            System.out.println("ExamDetailsDAO.editExamDetails()");
            e.printStackTrace();
        }
    }

}


package com.zealsofts.schoolmanagement.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ExamShiftDAO {

    SessionFactory sessionFactory;
    Session session;
    Transaction transaction;

    public List<ExamShift> getExamShifts(){
        List<ExamShift> examShifts = new ArrayList<ExamShift>();
        try{
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            examShifts = session.createQuery("FROM ExamShift").list();
        }
        catch (Exception e) {
            System.out.println("ExamDetailsDAO.getExamDetails()");
            e.printStackTrace();
        }
        return examShifts;
    }

    public void addExamShift( ExamShift examShift){
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.getTransaction();
            transaction.begin();
            session.save(examShift);
            transaction.commit();
        } catch (Exception e) {
            System.out.println("ExamShiftDAO.addExamShift()");
            e.printStackTrace();
        }
    }
    public void editExamShift( ExamShift examShift ){
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            transaction.commit();
        } catch (Exception e) {
            System.out.println("ExamShiftDAO.editExamShift()");
            e.printStackTrace();
        }
    }
}

请帮我。

4

1 回答 1

0

最后我解决了这个问题,问题是我在每个类中都获得了新的 SessionFactory 对象,因此每次都会获得新的事务。感谢您抽出时间

于 2013-09-01T07:53:42.373 回答