1
我在動作類中寫入代碼以便在數據庫中插入一條記錄,但不會在同一時間提交它,因爲我希望稍後再提交它記錄已使用不同的操作類插入。我是休眠的新手,不知道該怎麼做。請引導我。我正在使用struts 2.提前感謝。 我使用下面的代碼在一個動作中插入數據庫中的記錄,而不commiting在一個動作類中啓動事務,並在另一個動作類中提交它在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();
}
}
在此之後,在另一個動作類我書面方式將以下代碼插入其他記錄,並在同一時間commiting但我我不能犯以前插入
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();
}
}
}
請幫我插入的記錄。
你根本不應該那樣做。如果第二個動作從未執行會怎麼樣?如果你有某種類型的嚮導,並且最後必須保存整個事件,那麼將中間狀態保存在內存中,並將所有內容保存到最後,或者將中間狀態保存在特定的表中或「草稿」狀態中,並且在最後讀回這些表中的狀態,以便在單個事務中將整個事物保存在最終表中。 –
謝謝,我想像一個巫師那樣做。在第一次行動中,我將插入考試名稱,並在另一次考試中移動詳細信息。如果第二個動作(對於班次細節)沒有得到執行,那麼考試細節也不會被保存。所以我想要這樣做。 –
您是否看過我的評論?您的第一個操作應該簡單地將考試名稱保存在內存中(例如,在HTTP會話中)。您也可以簡單地在第二頁中使用隱藏字段來保存考試名稱,這將使其更加清潔。 –