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:com.epam.training.taranovski.spring.repository.oracle.AdminRepositoryOracle.java

/**
 *
 * @param id//from  w w  w .j  av a  2s.  c  om
 * @return
 */
@Override
public Admin getById(int id) {
    EntityManager em = emf.createEntityManager();
    Admin admin;
    try {
        em.getTransaction().begin();
        admin = em.find(Admin.class, id);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return admin;
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java

@Override
public ConferenceUserImpl getUser(long userId) {
    final EntityManager entityManager = this.getEntityManager();
    return entityManager.find(ConferenceUserImpl.class, userId);
}

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

/**
 *
 * @param request servlet request/* w w w .  j  a v  a 2  s .  c o  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(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("username");
    User user = em.find(User.class, userId);

    List<Appointment> appointments = user.getAppointments();
    request.setAttribute("appointments", appointments);

    RequestDispatcher rd = request.getRequestDispatcher("/admin/view-apmts.jsp");
    rd.forward(request, response);
}

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

@Override
public Picture findPictureById(final long id) {
    Assert.notNull(id, "Picture Id should be supplied !");

    final EmCallback<Picture> emCallback = new EmCallback<Picture>(this.getEmf()) {

        @Override/*from w w w .  ja va 2  s. co  m*/
        protected Picture executeWithEntityManager(final EntityManager em) throws PersistenceException {
            return em.find(Picture.class, id);
        }
    };

    return emCallback.getReturnedValue();
}

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

@Override
public BinaryImage findImageById(final long id) {
    Assert.notNull(id, "Image Id should be supplied !");

    final EmCallback<BinaryImage> emCallback = new EmCallback<BinaryImage>(this.getEmf()) {

        @Override//from  w ww  .j  av a2 s.c  om
        protected BinaryImage executeWithEntityManager(final EntityManager em) throws PersistenceException {
            return em.find(BinaryImage.class, id);
        }
    };

    return emCallback.getReturnedValue();
}

From source file:de.zib.gndms.infra.tests.FileTransferActionTest.java

@BeforeClass(groups = { "net", "db", "sys", "action", "task" })
public void beforeClass() throws ServerException, IOException, ClientException {

    PropertyConfigurator.configure(logFileConfig);
    runDatabase();/*  www  . ja  va  2 s.  c o  m*/
    transferData.initialize();

    // create orq
    FileTransferORQ orq = new FileTransferORQ();
    orq.setSourceURI(transferData.getSourceURI());
    orq.setTargetURI(transferData.getDestinationURI());
    orq.setFileMap(transferData.getFileMap());

    // create orq-calc
    FileTransferORQCalculator calc = new FileTransferORQCalculator();
    calc.setORQArguments(orq);
    // calc.setNetAux( getSys().getNetAux() );

    TransientContract con = calc.createOffer();
    PersistentContract pcon = con.acceptAt(new DateTime());

    // creating offertype
    OfferType ot;
    EntityManager em = null;
    try {
        em = getSys().getEntityManagerFactory().createEntityManager();
        ot = em.find(OfferType.class, "http://gndms.zib.de/ORQTypes/FileTransfer");
        if (ot == null) {
            ot = createFTOfferType();
            em.getTransaction().begin();
            em.persist(ot);
            em.getTransaction().commit();
        }

    } finally {
        if (em != null && em.isOpen())
            em.close();
        ot = createFTOfferType();
    }

    // create task
    task = new Task();
    task.setId(getSys().nextUUID());
    task.setDescription(orq.getDescription());
    task.setTerminationTime(pcon.getCurrentTerminationTime());
    task.setOfferType(ot);
    task.setOrq(orq);
    task.setContract(pcon);
    Calendar tt = pcon.getDeadline();
    tt.add(Calendar.YEAR, 10);
    task.setTerminationTime(tt);
}

From source file:nl.b3p.viewer.admin.stripes.ComponentConfigListActionBean.java

@DefaultHandler
public Resolution layerlist() {
    EntityManager em = Stripersist.getEntityManager();
    JSONArray jsonArray = new JSONArray();

    if (appId != null) {
        Application app = em.find(Application.class, appId);
        List<ApplicationLayer> layers = LayerListHelper.getLayers(app, filterable, bufferable, editable,
                influence, arc, wfs, attribute, false, null, em);
        for (ApplicationLayer layer : layers) {
            try {
                jsonArray.put(layer.toJSONObject(em));
            } catch (JSONException je) {
                log.error("Error while getting JSONObject of Layer with id: " + layer.getId(), je);
            }/*from w  w w  . j  av  a2 s.co m*/
        }
    }
    return new StreamingResolution("application/json", new StringReader(jsonArray.toString()));
}

From source file:kirchnerei.note.model.DataService.java

public NoteData getNoteById(Long id) {
    if (NumberUtils.isEmpty(id)) {
        return new NoteView();
    }//from  ww  w . java2 s . com
    EntityManager em = null;
    try {
        em = entityService.get();
        Note note = em.find(Note.class, id);
        if (note == null) {
            return new NoteView();
        }
        return new NoteView(note.getId(), note.getTitle(), note.getText(), note.getCategory().getTitle());
    } finally {
        EntityService.close(em);
    }
}

From source file:org.apache.james.mailbox.jpa.mail.JPAModSeqProvider.java

@Override
protected long lockedNextModSeq(MailboxSession session, Mailbox mailbox) throws MailboxException {
    EntityManager manager = null;
    try {//  w w w  .j a v a2  s .  co  m
        manager = factory.createEntityManager();
        manager.getTransaction().begin();
        JPAId mailboxId = (JPAId) mailbox.getMailboxId();
        JPAMailbox m = manager.find(JPAMailbox.class, mailboxId.getRawId());
        long modSeq = m.consumeModSeq();
        manager.persist(m);
        manager.getTransaction().commit();
        return modSeq;
    } catch (PersistenceException e) {
        if (manager != null && manager.getTransaction().isActive()) {
            manager.getTransaction().rollback();
        }
        throw new MailboxException("Unable to save highest mod-sequence for mailbox " + mailbox, e);
    } finally {
        if (manager != null) {
            manager.close();
        }
    }
}

From source file:nl.b3p.kaartenbalie.struts.ReportingAction.java

public ActionForward download(ActionMapping mapping, DynaValidatorForm dynaForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Integer id = FormUtils.StringToInteger(dynaForm.getString("id"));

    log.debug("Getting entity manager ......");
    EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);

    if (id != null) {
        Report report = (Report) em.find(Report.class, id);
        if (report != null) {
            String fileName = reportingDate.format(report.getReportDate()) + "_report_" + id;
            String xml = report.getReportXML();
            if (xml == null || xml.length() == 0) {
                xml = "empty or unfinished";
            }/* w  w w  . j a v  a  2s. c o m*/
            response.setContentType(report.getReportMime());
            response.setContentLength(xml.length());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            PrintWriter pw = response.getWriter();
            pw.print(xml);
        }
    }
    return null;
}