Example usage for javax.persistence EntityManager find

List of usage examples for javax.persistence EntityManager find

Introduction

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

Prototype

public <T> T find(Class<T> entityClass, Object primaryKey);

Source Link

Document

Find by primary key.

Usage

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreUpdatedEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();/* w ww  .ja  v  a2  s. c om*/

    LivreEntry entry = em.find(LivreEntry.class, e.getIsbn());
    entry.setEditeur(e.getEditeur());
    entry.setFormat(e.getFormat());
    entry.setImagePath(e.getImagePath());
    entry.setIsbn(e.getIsbn());
    entry.setLangue(e.getLangue());
    entry.setNbPage(e.getNbPage());
    entry.setResume(e.getResume());
    entry.setTitre(e.getTitre());
    entry.setTitreOriginal(e.getTitreOriginal());
    entry.setLu(e.isLu());
    entry.setPossede(e.isPossede());
    try {
        entry.setImageAsBase64(this.getImageAsBase64(e.getImagePath()));
    } catch (IOException ioe) {
        LivreListener.LOGGER.warn("Cannot save the thumbnail in database: ", ioe);
        entry.setImageAsBase64(null);
    }

    if (e.getImagePath() == null || e.getImagePath().isEmpty()) {
        entry.setImageAsBase64(null);
    }

    if (e.getSagaId() != null && !e.getSagaId().trim().isEmpty()) {
        SagaEntry saga = em.getReference(SagaEntry.class, e.getSagaId());
        entry.setSaga(saga);
    } else {
        entry.setSaga(null);
    }

    t.commit();
}

From source file:fr.mby.opa.picsimpl.dao.DbProposalDao.java

@Override
public ProposalBranch findBranch(final long branchId) {
    final EmCallback<ProposalBranch> emCallback = new EmCallback<ProposalBranch>(this.getEmf()) {

        @Override/*from w  w  w  .j  a v  a 2  s  .c om*/
        protected ProposalBranch executeWithEntityManager(final EntityManager em) throws PersistenceException {

            return em.find(ProposalBranch.class, branchId);
        }
    };

    return emCallback.getReturnedValue();
}

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

@Override
public Employee getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    Employee employee = null;//  w  w w .  j  a va2s  .  c  om
    try {
        em.getTransaction().begin();
        employee = em.find(Employee.class, id);
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return employee;
}

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

@Override
public Vacancy getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    Vacancy vacancy = null;/*from w w  w .ja  v a2  s. c o  m*/
    try {
        em.getTransaction().begin();
        vacancy = em.find(Vacancy.class, id);
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return vacancy;
}

From source file:org.apache.openjpa.persistence.xmlmapping.query.TestXMLCustomerOrder.java

public void testNullify() {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//w  w  w  . j  a va 2s .  co  m

    Order order = em.find(Order.class, ORDER_1_OID);
    order.setShipAddress(null);

    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();

    order = em.find(Order.class, ORDER_1_OID);
    Address address = order.getShipAddress();
    assertNull(address);

    em.getTransaction().commit();
    em.close();
}

From source file:com.medigy.persist.EntitySaveListener.java

public void deleteEntityList(final EntityManager manager) {
    while (!entityList.empty()) {
        final EntityInfo obj = (EntityInfo) entityList.pop();
        if (log.isInfoEnabled())
            log.info("Deleteing... " + obj.getEntityClass() + ". (id = " + obj.getId() + ")\n");
        final Object entity = manager.find(obj.getEntityClass(), obj.getId());
        if (entity != null)
            manager.remove(entity);/* w w  w.j a va2s . c  o m*/
    }
}

From source file:org.apache.openjpa.persistence.xmlmapping.query.TestXMLCustomerOrder.java

public void testUpdate() {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*from w w  w .  j  a  v  a 2 s.c o m*/

    Order order = em.find(Order.class, ORDER_1_OID);
    USAAddress address = (USAAddress) order.getShipAddress();
    address.setCity("Cupertino");
    address.setZIP(95014);

    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();

    order = em.find(Order.class, ORDER_1_OID);
    address = (USAAddress) order.getShipAddress();
    assertEquals("Cupertino", address.getCity());
    assertEquals(95014, address.getZIP());

    em.getTransaction().commit();
    em.close();
}

From source file:nl.b3p.kaartenbalie.core.server.accounting.AccountManager.java

/**
 * /*from w ww.  j  a v a2s  .  c om*/
 * @param transactionClass
 * @param description
 * @return
 * @throws java.lang.Exception
 */
public Transaction prepareTransaction(int type, String description) throws Exception {
    if (!isEnableAccounting()) {
        return null;
    }
    log.debug("Getting entity manager ......");
    EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);
    Account account = (Account) em.find(Account.class, organizationId);

    Transaction transaction = new Transaction();
    transaction.setType(type);
    transaction.setStatus(Transaction.PENDING);
    transaction.setAccount(account);
    transaction.setDescription(description);
    em.persist(transaction);
    em.flush();

    return transaction;
}

From source file:com.gigglinggnus.controllers.ModifyAppointmentController.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");

    Clock clk = (Clock) (request.getSession().getAttribute("clock"));
    String userId = request.getParameter("u");
    String examId = request.getParameter("e");
    User student = em.find(User.class, userId);
    Exam exam = em.find(Exam.class, examId);
    Appointment appt = student.getAppointmentByExam(exam);

    request.setAttribute("appt", appt);

    RequestDispatcher rd = request.getRequestDispatcher("/admin/modify-apmt.jsp");
    rd.forward(request, response);// w  ww  .  ja  v  a  2s.co m
}

From source file:de.zib.gndms.infra.action.SlicedTaskFlowAction.java

protected Slice findSlice(final String sliceId) {
    getLogger().info("findSlice(" + (sliceId == null ? "null" : '"' + sliceId + '"') + ')');
    if (sliceId == null)
        return null;

    final EntityManager em = getEntityManager();
    final TxFrame txf = new TxFrame(em);
    try {//www  .j  a v a  2s . c  om
        final Slice slice = em.find(Slice.class, sliceId);
        txf.commit();
        return slice;
    } finally {
        txf.finish();
    }
}