本文整理匯總了Java中javax.persistence.EntityTransaction類的典型用法代碼示例。如果您正苦於以下問題:Java EntityTransaction類的具體用法?Java EntityTransaction怎麽用?Java EntityTransaction使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
EntityTransaction類屬於javax.persistence包,在下文中一共展示了EntityTransaction類的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: getPermission
import javax.persistence.EntityTransaction; //導入依賴的package包/類
/**
* retrieves the <code>RolesPermission</code> by the role.
*
* @param role
* the role
* @return userPermissions the list of permissions of the user
*
*/
public static Set<String> getPermission(String role) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<RolePermission> q = cb.createQuery(RolePermission.class);
Root<RolePermission> c = q.from(RolePermission.class);
q.select(c).where(cb.equal(c.get("roleName"), role));
TypedQuery<RolePermission> query = em.createQuery(q);
List<RolePermission> permissions = query.getResultList();
Set<String> userPermissions = new HashSet<String>();
for (RolePermission permission : permissions)
userPermissions.add(permission.getPermission());
tx.commit();
em.close();
return userPermissions;
}
示例3: merge
import javax.persistence.EntityTransaction; //導入依賴的package包/類
public static ReceivedTexts merge(ReceivedTexts text) {
if (text != null) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
try {
trans.begin();
text = em.merge(text);
trans.commit();
return text;
} catch (Exception e) {
e.printStackTrace();
trans.rollback();
} finally {
em.close();
}
}
return null;
}
示例4: 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;
}
示例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: main
import javax.persistence.EntityTransaction; //導入依賴的package包/類
/**
* @param args the command line arguments
*/
@PersistenceUnit
public static void main(String[] args) {
System.out.println("Creating entity information...");
EntityManager entityManager = Persistence.createEntityManagerFactory("DataAppLibraryPULocal").createEntityManager();
EntityTransaction et = entityManager.getTransaction();
et.begin();
loadDiscountRate(entityManager);
loadRegion(entityManager);
loadRole(entityManager);
loadTransmission(entityManager);
loadProductType(entityManager);
loadEngine(entityManager);
loadProduct(entityManager);
et.commit();
EntityManager specialEntityManager = new InitialLoadEntityManagerProxy(entityManager);
SalesSimulator simulator = new SalesSimulator(specialEntityManager);
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.clear();
cal.set(year-1, 0, 1, 0, 0, 0); // go back to begining of year, 3 years ago
System.out.println("Creating historical data...");
System.out.println(" This may take 5 to 15min depending on machine speed.");
simulator.run(cal.getTime(), new Date());
entityManager.close();
}
示例7: 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);
}
}
示例8: runInTran
import javax.persistence.EntityTransaction; //導入依賴的package包/類
private <T> T runInTran(Callable<T> arg0) {
EntityTransaction tx = this.em.getTransaction();
try {
tx.begin();
T o = arg0.call();
tx.commit();
callListeners(TransactionStatus.COMMITTED);
return o;
} catch (Exception e) {
tx.rollback();
callListeners(TransactionStatus.ROLLED_BACK);
if(e instanceof ScopedWorkException) {
throw (ScopedWorkException) e;
}
throw new ScopedWorkException("The work failed", e, getCurrentContext());
} finally {
this.txActive.compareAndSet(true, false);
this.context.clear();
this.em.clear();
}
}
示例9: 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,代碼來源:NotStandardTest.java
示例10: createThread
import javax.persistence.EntityTransaction; //導入依賴的package包/類
private Thread createThread(Consumer<EntityManager> command) {
return new Thread(() ->{
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try{
command.accept(em);
tx.commit();
} catch (Exception e){
tx.rollback();
System.out.println("\n\nFailed transaction on separated thread: "+e.getCause().toString()+"\n\n");
}
em.close();
});
}
開發者ID:arcuri82,項目名稱:testing_security_development_enterprise_systems,代碼行數:18,代碼來源:TransactionExecutor.java
示例11: persistInATransaction
import javax.persistence.EntityTransaction; //導入依賴的package包/類
protected 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,代碼來源:EntityTestBase.java
示例12: 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
示例13: persistInATransaction
import javax.persistence.EntityTransaction; //導入依賴的package包/類
protected 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,代碼來源:EntityTestBase.java
示例14: doRollback
import javax.persistence.EntityTransaction; //導入依賴的package包/類
@Override
protected void doRollback(DefaultTransactionStatus status) {
JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
if (status.isDebug()) {
logger.debug("Rolling back JPA transaction on EntityManager [" +
txObject.getEntityManagerHolder().getEntityManager() + "]");
}
try {
EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
if (tx.isActive()) {
tx.rollback();
}
}
catch (PersistenceException ex) {
throw new TransactionSystemException("Could not roll back JPA transaction", ex);
}
finally {
if (!txObject.isNewEntityManagerHolder()) {
// Clear all pending inserts/updates/deletes in the EntityManager.
// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
txObject.getEntityManagerHolder().getEntityManager().clear();
}
}
}
示例15: getUser
import javax.persistence.EntityTransaction; //導入依賴的package包/類
public static User getUser(String username) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> q = cb.createQuery(User.class);
Root<User> c = q.from(User.class);
q.select(c).where(cb.equal(c.get("username"), username));
TypedQuery<User> query = em.createQuery(q);
List<User> users = query.getResultList();
em.close();
LOGGER.info("found " + users.size() + " users with username " + username);
if (users.size() == 1)
return users.get(0);
else
return null;
}