Example usage for javax.persistence EntityManager getTransaction

List of usage examples for javax.persistence EntityManager getTransaction

Introduction

In this page you can find the example usage for javax.persistence EntityManager getTransaction.

Prototype

public EntityTransaction getTransaction();

Source Link

Document

Return the resource-level EntityTransaction object.

Usage

From source file:com.enioka.jqm.tools.JobManagerHandler.java

/**
 * Update the {@link com.enioka.jqm.jpamodel.History} with the given progress data.
 * /*from   w ww  .j a  v a  2s  .  co  m*/
 * @param msg
 * @throws JqmKillException
 */
private void sendProgress(Integer msg) {
    EntityManager em = Helpers.getNewEm();
    try {
        em.getTransaction().begin();
        this.ji = em.find(JobInstance.class, this.ji.getId(), LockModeType.PESSIMISTIC_WRITE);
        ji.setProgress(msg);
        em.getTransaction().commit();
    } finally {
        em.close();
    }
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

private void initExampleData() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {//from  w  w  w  . ja  v  a  2  s  . c  o m
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        User player1 = new User("student@busimu.com", "ssssss", "s", User.Type.STUDENT);
        player1.setStatus(User.Status.ACTIVE);
        User player2 = new User("zhangsan@busimu.com", "123456", "", User.Type.STUDENT);
        player2.setStatus(User.Status.ACTIVE);
        User player3 = new User("lisi@busimu.com", "123456", "?", User.Type.STUDENT);
        player3.setStatus(User.Status.ACTIVE);
        User teacher = new User("teacher@busimu.com", "tttttt", "t", User.Type.TEACHER);
        teacher.setStatus(User.Status.ACTIVE);
        em.persist(player1);
        em.persist(player2);
        em.persist(player3);
        em.persist(teacher);

        Campaign campaign = teacher.createCampagin("", Campaign.Type.COURSE);
        SimMarket south = campaign.addMarket("??");
        SimMarket central = campaign.addMarket("?");
        SimMarket north = campaign.addMarket("?");
        SimMarket east = campaign.addMarket("?");
        SimMarket west = campaign.addMarket("");
        SimCorporation ibm = south.addCorporation("IBM", new Color(128, 128, 255));
        SimCorporation ericsson = south.addCorporation("Ericsson", Color.BLUE);
        SimCorporation oracle = south.addCorporation("Oracle", Color.RED);
        for (int i = 0; i < 8; i++) {
            campaign.addRound();
        }

        player2.addCampaign(campaign);
        player3.addCampaign(campaign);
        em.persist(campaign);

        Team t1 = new Team("anoym1");
        Team t2 = new Team("anoym2");
        Team t3 = new Team("anoym3");
        t1.addCorporation(ibm);
        t2.addCorporation(ericsson);
        t3.addCorporation(oracle);
        em.persist(t1);
        em.persist(t2);
        em.persist(t3);

        Campaign runningCampaign = teacher.createCampagin("", Campaign.Type.COURSE);
        SimMarket m1 = runningCampaign.addMarket("");
        SimMarket m2 = runningCampaign.addMarket("");
        SimMarket m3 = runningCampaign.addMarket("");
        SimCorporation corp1 = m1.addCorporation("", Color.ORANGE);
        for (int i = 0; i < 3; i++) {
            runningCampaign.addRound();
        }
        player1.addCampaign(runningCampaign);
        player2.addCampaign(runningCampaign);
        player3.addCampaign(runningCampaign);
        runningCampaign.setStatus(Campaign.Status.ONGOING);

        Team t11 = new Team("anoym11");
        t11.addCorporation(corp1);
        t11.addUser(player1);
        t11.addUser(player2);
        t11.addUser(player3);
        em.persist(t11);

        tx.commit();
    } finally {
        em.close();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public boolean create(OfferBid something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {//from  www  .j a v  a 2 s .c  o m
        em.getTransaction().begin();
        em.persist(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public boolean update(OfferBid something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {/*from www .j  a va  2s  .c o  m*/
        em.getTransaction().begin();
        em.merge(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public boolean deleteAllBidsForVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {/*from ww  w . java 2 s.c o  m*/
        em.getTransaction().begin();
        Query query = em.createNamedQuery("OfferBid.deleteByVacancy");
        query.setParameter("vacancy", vacancy);
        query.executeUpdate();
        em.getTransaction().commit();

        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public boolean deleteAllOffersForEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {/*from w  w w . j  ava  2 s. c  o  m*/
        em.getTransaction().begin();
        Query query = em.createNamedQuery("OfferBid.deleteByEmployee");
        query.setParameter("employee", employee);
        query.executeUpdate();
        em.getTransaction().commit();

        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.headissue.pigeon.admin.AdminSurveyHandler.java

public boolean disableSurvey(int _surveyId) {
    if (_surveyId <= 0) {
        return false;
    }//from  www. ja  va 2s  .c o m
    EntityManager _manager = factory.createEntityManager();
    try {
        Survey s = _manager.find(Survey.class, _surveyId);
        if (s == null) {
            return false;
        }
        _manager.getTransaction().begin();
        s.setStatus(SurveyStatus.DISABLED);
        _manager.getTransaction().commit();
        return true;
    } catch (Exception e) {
        LogUtils.warn(log, "set survey '%s' to disabled failed", _surveyId);
        _manager.getTransaction().rollback();
        return false;
    } finally {
        _manager.close();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public OfferBid getOfferByEmployeeAndVacancy(Employee employee, Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    OfferBid offer = null;//from w  ww .j a v  a 2 s  . com
    try {
        em.getTransaction().begin();

        TypedQuery<OfferBid> query = em.createNamedQuery("OfferBid.findOfferByEmployeeAndVacancy",
                OfferBid.class);
        query.setParameter("employee", employee);
        query.setParameter("vacancy", vacancy);
        offer = query.getSingleResult();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        offer = null;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return offer;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public OfferBid getBidByEmployeeAndVacancy(Employee employee, Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    OfferBid offer = null;//from  ww w .  ja  v  a2  s. c o m
    try {
        em.getTransaction().begin();

        TypedQuery<OfferBid> query = em.createNamedQuery("OfferBid.findBidByEmployeeAndVacancy",
                OfferBid.class);
        query.setParameter("employee", employee);
        query.setParameter("vacancy", vacancy);
        offer = query.getSingleResult();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        offer = null;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return offer;
}

From source file:edu.csueb.cs6320.utils.UserService.java

public boolean delete(long userid) {
    EntityManager em = Persistence.createEntityManagerFactory("TestPU").createEntityManager();

    User u = em.find(User.class, userid);
    if (u == null) {
        return false;
    }//from  w w  w. j  a v  a  2s .com
    em.getTransaction().begin();
    em.remove(u);
    em.getTransaction().commit();
    em.close();

    return true;
}