本文整理匯總了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);
}
}
示例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;
}
示例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);
}
}
示例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;
}
示例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!");
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}
示例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();
}
示例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);
}
示例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();
}
示例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);
}