本文整理汇总了Java中javax.persistence.EntityManager.getTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java EntityManager.getTransaction方法的具体用法?Java EntityManager.getTransaction怎么用?Java EntityManager.getTransaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.EntityManager
的用法示例。
在下文中一共展示了EntityManager.getTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: persist
import javax.persistence.EntityManager; //导入方法依赖的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;
}
示例2: insert
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* Recibe los ciudadanos leídos por el parser. Si hay alguno con dni repetido no se mete.
* Devuelve una lista con los ciudadanos se hayan podido meter en la base de datos.
*/
@Override
public List<Citizen> insert(List<Citizen> citizens) {
EntityManager mapper = Jpa.createEntityManager();
EntityTransaction trx = mapper.getTransaction();
trx.begin();
List<Citizen> ciud = new ArrayList<Citizen>();
for(Citizen c:citizens){
if(CitizenFinder.findByDNI(c.getDNI())==null){
Jpa.getManager().persist(c);
ciud.add(c);
Console.println("Se ha añadido a la base de datos a "+c.toString());
}else{
reporter.report("El usuario con DNI "+c.getDNI()+" ya se encontraba en la base de datos.");
}
}
trx.commit();
return ciud;
}
示例3: changeTask
import javax.persistence.EntityManager; //导入方法依赖的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);
}
}
示例4: createThread
import javax.persistence.EntityManager; //导入方法依赖的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
示例5: changeTaskOwner
import javax.persistence.EntityManager; //导入方法依赖的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: addLoginLog
import javax.persistence.EntityManager; //导入方法依赖的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;
}
示例7: addActivityLog
import javax.persistence.EntityManager; //导入方法依赖的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;
}
示例8: delete
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static boolean delete(int messageID) {
if (messageID > 0) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
ConversationMessages message = em.find(ConversationMessages.class, messageID);
if (message != null) {
EntityTransaction trans = em.getTransaction();
try {
trans.begin();
em.remove(message);
trans.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
trans.rollback();
} finally {
em.close();
}
}
}
return false;
}
示例9: persist
import javax.persistence.EntityManager; //导入方法依赖的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: merge
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Companies merge(Companies company) {
if (company != null) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
try {
trans.begin();
company = em.merge(company);
trans.commit();
return company;
} catch (Exception e) {
e.printStackTrace();
trans.rollback();
return null;
} finally {
em.close();
}
}
return null;
}
示例11: persistAuthor
import javax.persistence.EntityManager; //导入方法依赖的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();
}
示例12: persistAuthors
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static void persistAuthors(List<PublicationAuthor> authors) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("publicationAuthors");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
for (PublicationAuthor author : authors)
em.persist(author);
tx.commit();
em.close();
}
示例13: getAll
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public List<ClassifiableText> getAll() {
Set<ClassifiableText> classifiableTextsWithoutDuplicates = new LinkedHashSet<>();
EntityManager manager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = null;
try {
transaction = manager.getTransaction();
transaction.begin();
classifiableTextsWithoutDuplicates.addAll(manager.createQuery("SELECT c FROM ClassifiableText c JOIN c.characteristics v", ClassifiableText.class).getResultList());
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
manager.close();
}
List<ClassifiableText> classifiableTexts = new ArrayList<>();
classifiableTexts.addAll(classifiableTextsWithoutDuplicates);
return classifiableTexts;
}
示例14: doPost
import javax.persistence.EntityManager; //导入方法依赖的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 candidateID = (int) session.getAttribute("candidateID");
Candidates candidate = em.find(Candidates.class, candidateID);
String source = request.getParameter("source");
System.out.println(source);
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.getName() != null) {
if (item.getName().contains(".pdf") || item.getName().contains(".doc")) {
byte[] file = IOUtils.toByteArray(item.openStream());
candidate.setResume(file);
trans.begin();
em.merge(candidate);
trans.commit();
// For logging activity
SchoolLoginLogs loginLog = (SchoolLoginLogs) session.getAttribute("loginLog");
if (loginLog != null) {
SchoolActivityLogs activityLog = new SchoolActivityLogs();
activityLog.setSchoolLoginLogID(loginLog.getSchoolLoginLogID());
activityLog.setTime(Clock.getCurrentTime());
activityLog.setCandidateID(candidateID);
activityLog.setCandidate(candidate);
activityLog.setSchoolActivity("Uploaded Resume");
SchoolAccountDataAccessObject.persistActivityLog(activityLog);
}
session.setAttribute("resumeSuccess", "Resume uploaded!");
session.setAttribute("resume", "true");
session.setAttribute("candidate", candidate);
session.setAttribute("loginLog", loginLog);
} else {
String resumeError = "Please upload a resume in PDF or DOC/DOCX format.";
session.setAttribute("resume", "true");
session.setAttribute("resumeError", resumeError);
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error uploading resume");
} finally {
em.close();
response.sendRedirect("schools/school-candidate-profile.jsp");
}
}
示例15: doGet
import javax.persistence.EntityManager; //导入方法依赖的package包/类
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Candidates candidate = new Candidates();
// Retrieves candidateID of candidate who's profile needs to be
// displayed
int candidateID = Integer.parseInt(request.getParameter("candidateID"));
// Creates EntityManager to query database
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
// Retrieves user from database based on userID
candidate = em.find(Candidates.class, candidateID);
session.setAttribute("candidate", candidate);
session.setAttribute("candidateID", candidateID);
// For logging activity
SchoolLoginLogs loginLog = (SchoolLoginLogs) session.getAttribute("loginLog");
SchoolActivityLogs activityLog = new SchoolActivityLogs();
activityLog.setSchoolLoginLogID(loginLog.getSchoolLoginLogID());
activityLog.setLoginLog(loginLog);
activityLog.setTime(Clock.getCurrentTime());
activityLog.setCandidateID(candidateID);
activityLog.setCandidate(candidate);
activityLog.setSchoolActivity("Viewed Profile");
try {
trans.begin();
em.persist(activityLog);
trans.commit();
session.setAttribute("loginLog", loginLog);
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
} finally {
em.close();
response.sendRedirect("schools/school-candidate-profile.jsp");
}
}