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


Java EntityTransaction.begin方法代碼示例

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


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

示例1: changeTask

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
private void changeTask(long taskId,String username,StatusOperation operation,String faultMessage) {
	EntityManager em=getEntityManager();
	EntityTransaction trans=em.getTransaction();
	try{
		trans.begin();
		Task task=em.find(Task.class, taskId);
		if(StringUtils.isNotEmpty(faultMessage)){
			task.setFaultMessage(faultMessage);
		}
		if(changeTaskStatus(task,operation)){
			em.persist(task);				
		}
		TaskStatus status=task.getStatus();
		if(status.equals(TaskStatus.Completed) || status.equals(TaskStatus.Error) || status.equals(TaskStatus.Exited) || status.equals(TaskStatus.Obsolete) || status.equals(TaskStatus.Failed)){
			StatefulKnowledgeSession session=this.bpmService.getSession(task.getSessionId());
			session.getWorkItemManager().completeWorkItem(task.getWorkItemId(),null);	
			session.dispose();
		}
		trans.commit();
	}catch(Exception ex){
		trans.rollback();
		throw new RuntimeException(ex);
	}
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:25,代碼來源:TaskServiceImpl.java

示例2: 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

示例3: delegate

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public void delegate(long taskId, String username, String targetUsername) {
	EntityManager em=this.getEntityManager();
	EntityTransaction trans=em.getTransaction();
	try{
		trans.begin();
		String jpql="select d from "+TaskDelegate.class.getName()+" d where d.task.id=:taskId and d.delegator=:targetUsername";
		int size=em.createQuery(jpql).setParameter("taskId", taskId).setParameter("targetUsername", targetUsername).getResultList().size();
		if(size>0){
			throw new IllegalArgumentException("User "+targetUsername+" has already delegate task "+taskId+"");
		}
		Task task=new Task(taskId);
		TaskDelegate taskDelegate=new TaskDelegate();
		taskDelegate.setTask(task);
		em.persist(taskDelegate);
		taskEventSupport.fireEvents(StatusOperation.Delegate, task);
		trans.commit();
	}catch(Exception ex){
		trans.rollback();
		throw new RuntimeException(ex);
	}
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:22,代碼來源:TaskServiceImpl.java

示例4: addLoginLog

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static CandidateLoginLogs addLoginLog(Candidates candidate) {
    if (candidate != null) {
        EntityManager em = EMFUtil.getEMFactory().createEntityManager();
        EntityTransaction trans = em.getTransaction();
        CandidateLoginLogs loginLog = new CandidateLoginLogs(candidate.getCandidateID(), Clock.getCurrentDate(),
                Clock.getCurrentTime());
        loginLog.setCandidateID(candidate.getCandidateID());
        try {
            trans.begin();
            em.persist(loginLog);
            trans.commit();
            loginLog.setCandidate(candidate);
            return loginLog;
        } catch (Exception e) {
            e.printStackTrace();
            trans.rollback();
        }
    }
    return null;
}
 
開發者ID:faizan-ali,項目名稱:full-javaee-app,代碼行數:21,代碼來源:CandidatePersistence.java

示例5: changeTaskOwner

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public void changeTaskOwner(long taskId, String newOwner) {
	Task task=this.getTask(taskId);
	if(task.getStatus().equals(TaskStatus.Created)){
		EntityManager em=getEntityManager();
		EntityTransaction trans=em.getTransaction();
		try{
			trans.begin();
			task.setOwner(newOwner);
			em.persist(task);
			trans.commit();
		}catch(Exception ex){
			trans.rollback();
			throw new RuntimeException(ex);
		}
	}else{
		throw new IllegalStateException("Current task status is "+task.getStatus()+",can not change it owner!");
	}
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:19,代碼來源:TaskServiceImpl.java

示例6: persistInATransaction

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
private boolean persistInATransaction(Object... obj) {
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    try {
        for(Object o : obj) {
            em.persist(o);
        }
        tx.commit();
    } catch (Exception e) {
        System.out.println("FAILED TRANSACTION: " + e.toString());
        tx.rollback();
        return false;
    }

    return true;
}
 
開發者ID:arcuri82,項目名稱:testing_security_development_enterprise_systems,代碼行數:18,代碼來源:UserTest.java

示例7: persist

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

示例8: merge

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
public static WeeklyStats merge (WeeklyStats stats) {
	if (stats != null) {
           EntityManager em = EMFUtil.getEMFactory().createEntityManager();
           EntityTransaction trans = em.getTransaction();
           try {
               trans.begin();
               stats = em.merge(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

示例9: persist

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

示例10: 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

示例11: testOrphanRemovalFail

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

    Address address = new Address();
    User user = new User();
    user.setAddress(address);

    assertTrue(persistInATransaction(user,address));

    User found = em.find(User.class, user.getId());
    assertNotNull(found.getAddress());

    //now try to remove Address from DB
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    try {
        em.remove(address);
        tx.commit();
        fail();
    } catch (Exception e){
        tx.rollback();
    }

    //the remove should had fail, because User is owner and still in DB

    found = em.find(User.class, user.getId());
    assertNotNull(found.getAddress());
}
 
開發者ID:arcuri82,項目名稱:testing_security_development_enterprise_systems,代碼行數:29,代碼來源:UserTest.java

示例12: insert

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
/**
 * persists a user role
 * 
 * @param r
 *            the role
 * 
 */
public static void insert(UserRole r) {
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
	EntityManager em = emf.createEntityManager();
	EntityTransaction tx = em.getTransaction();
	tx.begin();
	em.persist(r);
	tx.commit();
	em.close();
}
 
開發者ID:ETspielberg,項目名稱:bibliometrics,代碼行數:17,代碼來源:UserRoleDAO.java

示例13: enlistInCurrentTransaction

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
/**
 * Enlist this application-managed EntityManager in the current transaction.
 */
private void enlistInCurrentTransaction() {
	// Resource local transaction, need to acquire the EntityTransaction,
	// start a transaction now and enlist a synchronization for commit or rollback later.
	EntityTransaction et = this.target.getTransaction();
	et.begin();
	if (logger.isDebugEnabled()) {
		logger.debug("Starting resource-local transaction on application-managed " +
				"EntityManager [" + this.target + "]");
	}
	ExtendedEntityManagerSynchronization extendedEntityManagerSynchronization =
			new ExtendedEntityManagerSynchronization(this.target, this.exceptionTranslator);
	TransactionSynchronizationManager.bindResource(this.target, extendedEntityManagerSynchronization);
	TransactionSynchronizationManager.registerSynchronization(extendedEntityManagerSynchronization);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:18,代碼來源:ExtendedEntityManagerCreator.java

示例14: persistAuthor

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
static void persistAuthor(PublicationAuthor author) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("publicationAuthors");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(author);
    LOGGER.info("author persisted");
    tx.commit();
    em.close();
}
 
開發者ID:ETspielberg,項目名稱:bibliometrics,代碼行數:11,代碼來源:PublicationAuthorDAO.java

示例15: testRemove

import javax.persistence.EntityTransaction; //導入方法依賴的package包/類
@Test
public void testRemove(){
    String name = "foo";

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

    assertTrue(persistInATransaction(user));
    assertTrue(em.contains(user));

    User found = em.find(User.class, user.getId());
    assertEquals(name, found.getName());


    EntityTransaction tx = em.getTransaction();
    tx.begin();
    try {
        em.remove(user);
        tx.commit();
    } catch (Exception e){
        tx.rollback();
        fail();
    }

    assertFalse(em.contains(user));

    //can't be found anymore in the DB
    found = em.find(User.class, user.getId());
    assertNull(found);
}
 
開發者ID:arcuri82,項目名稱:testing_security_development_enterprise_systems,代碼行數:31,代碼來源:UserTest.java


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