當前位置: 首頁>>代碼示例>>Java>>正文


Java EntityTransaction.commit方法代碼示例

本文整理匯總了Java中javax.persistence.EntityTransaction.commit方法的典型用法代碼示例。如果您正苦於以下問題:Java EntityTransaction.commit方法的具體用法?Java EntityTransaction.commit怎麽用?Java EntityTransaction.commit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.persistence.EntityTransaction的用法示例。


在下文中一共展示了EntityTransaction.commit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: persist

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static WeeklyStats persist (WeeklyStats stats) {
	if (stats != null) {
           EntityManager em = EMFUtil.getEMFactory().createEntityManager();
           EntityTransaction trans = em.getTransaction();
           try {
               trans.begin();
               em.persist(stats);
               trans.commit();
               return stats;
           } catch (Exception e) {
               e.printStackTrace();
               trans.rollback();
           } finally {
               em.close();
           }
	}
	return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:19,代碼來源:WeeklyStatsDataAccessObject.java

示例2: persist

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static Companies persist(Companies company) {
    if (company != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            em.persist(company);
            trans.commit();
            return company;
        } catch (Exception e) {
            e.printStackTrace();
            trans.rollback();
            return null;
        } finally {
            em.close();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:20,代碼來源:CompanyDataAccessObject.java

示例3: merge

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static ConversationMessages merge (ConversationMessages message) {
    if (message != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            message = em.merge(message);
            trans.commit();
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            trans.rollback();
        } finally {
            em.close();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:19,代碼來源:ConversationMessageDataAccessObject.java

示例4: insertButtonActionPerformed

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
private void insertButtonActionPerformed(ActionEvent evt) 
{
   Addresses address = new Addresses();
   address.setFirstname(firstNameTextField.getText());
   address.setLastname(lastNameTextField.getText());
   address.setPhonenumber(phoneTextField.getText());
   address.setEmail(emailTextField.getText());
   
   // get an EntityTransaction to manage insert operation
   EntityTransaction transaction = entityManager.getTransaction();
   
   try
   {
      transaction.begin(); // start transaction
      entityManager.persist(address); // store new entry
      transaction.commit(); // commit changes to the database
      JOptionPane.showMessageDialog(this, "Person added!",
         "Person added", JOptionPane.PLAIN_MESSAGE);
   }
   catch (Exception e) // if transaction failed
   {
      transaction.rollback(); // undo database operations 
      JOptionPane.showMessageDialog(this, "Person not added!",
         e.getMessage(), JOptionPane.PLAIN_MESSAGE);
   }
}
 
開發者ID:cleitonferreira,項目名稱:LivroJavaComoProgramar10Edicao,代碼行數:27,代碼來源:AddressBook.java

示例5: merge

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static Conversations merge (Conversations conversation) {
    if (conversation != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            conversation = em.merge(conversation);
            trans.commit();
            return conversation;
        } catch (Exception e) {
            trans.rollback();
            e.printStackTrace();
            return null;
        } finally {
            em.close();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:20,代碼來源:ConversationDataAccessObject.java

示例6: merge

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static ProfileViews merge (ProfileViews view) {
    if (view != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            view = em.merge(view);
            trans.commit();
            return view;
        } catch (Exception e) {
            e.printStackTrace();
            trans.rollback();
        } finally {
            em.close();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:19,代碼來源:ProfileViewDataAccessObject.java

示例7: testQuiz

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
@Test
public void testQuiz(){

    Quiz quiz = new Quiz();
    quiz.setQuestion("Will this test pass?");
    quiz.setFirstAnswer("Yes");
    quiz.setSecondAnswer("No");
    quiz.setThirdAnswer("Maybe");
    quiz.setFourthAnswer("No idea");
    quiz.setIndexOfCorrectAnswer(0);

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("DB");
    EntityManager em = factory.createEntityManager();

    EntityTransaction tx = em.getTransaction();
    tx.begin();

    em.persist(quiz);

    tx.commit();
}
 
開發者ID:arcuri82,項目名稱:testing_security_development_enterprise_systems,代碼行數:22,代碼來源:QuizEntityTest.java

示例8: delete

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static boolean delete(int candidateID) {
    if (candidateID > 0) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        Candidates candidate = em.find(Candidates.class, candidateID);
        if (candidate != null) {
            EntityTransaction trans = em.getTransaction();
            try {
                trans.begin();
                em.remove(candidate);
                trans.commit();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                trans.rollback();
            } finally {
                em.close();
            }
        }
    }
    return false;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:22,代碼來源:CandidatePersistence.java

示例9: insert

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
@Override
public List<CitizenDB> insert(List<CitizenDB> citizens) throws SQLException {
	EntityManager em = Jpa.createEntityManager();
	EntityTransaction trx = em.getTransaction();
	trx.begin();
	List<CitizenDB> users = new ArrayList<CitizenDB>();
	try{
		
		for(CitizenDB citizen : citizens)
			if(checkCitizen.checkCitizenInformation(citizen) && checkCitizen(citizen)){
				citizen.setPassword(generationPassword.passwordGenerator());
				Jpa.getManager().persist(citizen);
				users.add(citizen);
			}
    
	trx.commit();	    
	}catch(RuntimeException e){
		trx.rollback();
    	throw e;
	}
	finally{
		em.close();
	}
	return users;
}
 
開發者ID:Arquisoft,項目名稱:dashboard1b,代碼行數:26,代碼來源:InsertP.java

示例10: addActivityLog

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static CandidateActivityLogs addActivityLog(CandidateActivityLogs activityLog) {
    if (activityLog != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            em.persist(activityLog);
            trans.commit();
            return activityLog;
        } catch (Exception e) {
            e.printStackTrace();
            trans.rollback();
        } finally {
            em.close();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:19,代碼來源:CandidatePersistence.java

示例11: persist

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static ClientPipelines persist (ClientPipelines elt) {
    if (elt != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            em.persist(elt);
            trans.commit();
            return elt;
        } catch (Exception e) {
            e.printStackTrace();
            trans.rollback();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:17,代碼來源:ClientPipelineDataAccessObject.java

示例12: invoke

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
@AroundInvoke
public Object invoke(InvocationContext context) throws Exception {
	EntityTransaction transaction = manager.getTransaction();
	boolean owner = false;

	try {
		if (!transaction.isActive()) {
			transaction.begin();
			transaction.rollback();

			transaction.begin();
			owner = true;
		}

		return context.proceed();
	} catch (Exception e) {
		if (transaction != null && owner) {
			transaction.rollback();
		}

		throw e;
	} finally {
		if (transaction != null && transaction.isActive() && owner) {
			transaction.commit();
		}
	}
}
 
開發者ID:IsadoraScusselFarias,項目名稱:ProjetoFinalInitium,代碼行數:28,代碼來源:TransactionInterceptor.java

示例13: testPersistAndModify

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
@Test
public void testPersistAndModify() {

    String before = "before";
    String during = "during";
    String after = "after";

    User user = new User();
    user.setName(before);

    EntityTransaction tx = em.getTransaction();
    tx.begin();
    try {
        em.persist(user);
        /*
            the transaction is not finished, so, any modification to the attached
            object will be committed to the DB
         */
        user.setName(during);
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        fail();
    }

    //this is done after the transaction, so no effect on DB
    user.setName(after);

    //make sure we clear the cache, making all entity objects detached
    em.clear();

    //read operations like this do not need an explicit transaction
    User found = em.find(User.class, user.getId());

    assertEquals(during, found.getName());
    assertEquals(after, user.getName());
}
 
開發者ID:arcuri82,項目名稱:testing_security_development_enterprise_systems,代碼行數:38,代碼來源:UserTest.java

示例14: doPost

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// Creates EntityManager to query database
	EntityManager em = EMFUtil.getEMFactory().createEntityManager();

	// Transactions are employed to roll back changes in case of error and
	// ensure data integrity
	EntityTransaction trans = em.getTransaction();
	
	//Creates a session object and retrieves entities
	HttpSession session = request.getSession();	
	int clientID = (int) session.getAttribute("clientID");
	Clients user = em.find(Clients.class, clientID);
	String password = request.getParameter("password");
	
	//Updates entities based on input parameters
	CustomUtilities custom = new CustomUtilities();
	String [] passwordArray = custom.hashPassword(password, null);
	
	user.setFirstName(request.getParameter("firstName"));
	user.setLastName(request.getParameter("lastName"));
	user.setPassword(passwordArray[0]);
	user.setSalt(passwordArray[1]);
	
	try {			
		trans.begin();
		em.merge(user);
		trans.commit();
		response.sendRedirect("landing.jsp");
	} catch (IllegalStateException e) {
		e.printStackTrace();
		logger.error("Candidate account update bug", e);
		trans.rollback();
	} finally {
		em.close();
	}
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:37,代碼來源:ClientAccountServlet.java

示例15: doPost

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	// Creates EntityManager to query database
	EntityManager em = EMFUtil.getEMFactory().createEntityManager();

	// Transactions are employed to roll back changes in case of error and
	// ensure data integrity
	EntityTransaction trans = em.getTransaction();

	// Accesses the session object and retrieves entities
	HttpSession session = request.getSession();

	// Retrieves clientID of client
	int clientID = (int) session.getAttribute("clientID");

	int candidateID = Integer.parseInt(request.getParameter("candidateID"));
	int pipelineID = Integer.parseInt(request.getParameter("pipelineID"));
	Clients user = em.find(Clients.class, clientID);
	ClientPipelines elt = em.find(ClientPipelines.class, pipelineID);
	String notes = request.getParameter("notes");

	elt.setNotes(notes);
	user.getClientPipelineByCandidateID(candidateID).setNotes(notes);

	try {
		trans.begin();
		em.merge(user);
		trans.commit();

		if (user != null) {
			session.setAttribute("user", user);
		}
		session.setAttribute("candidatePipelineEntity", elt);
		response.sendRedirect("candidate-profile.jsp");
	} catch (Exception e) {
		e.printStackTrace();
		logger.error("Notes update bug", e);
		trans.rollback();
           response.sendRedirect("candidate-profile.jsp");
	} finally {
		em.close();
	}

}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:45,代碼來源:NotesServlet.java


注:本文中的javax.persistence.EntityTransaction.commit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。