Example usage for javax.persistence EntityManager getReference

List of usage examples for javax.persistence EntityManager getReference

Introduction

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

Prototype

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

Source Link

Document

Get an instance, whose state may be lazily fetched.

Usage

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * @param <T>//w  w w . j a  va 2 s. c o m
 * @param entityManager
 * @param c
 * @param returnType
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> List<T> listUsingCache(final EntityManager entityManager, final Criteria c,
        final Class<T> returnType) {

    final List<T> resultList = new ArrayList<T>();
    final List<Long> idList = c.setProjection(Projections.id()).list();
    for (Long id : idList) {
        resultList.add(entityManager.getReference(returnType, id));
    }
    return resultList;
}

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

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

    EntityTransaction t = em.getTransaction();

    t.begin();//  ww w .  j a  v  a2s  .  c  om

    LivreEntry entry = em.getReference(LivreEntry.class, e.getIsbn());
    em.remove(entry);

    t.commit();
}

From source file:commonSession.PersistAttivitaFacadeBean.java

public Object getReference(Integer id, Class clazz, String unit) throws Exception {
    try {/*from w  w w  .  jav a 2 s  . c  o  m*/

        //      EntityManager em;
        EntityManager em = GestionaleEm;

        //      if(unit.equals("GestionaleEm")) em = GestionaleEm;
        //          else em = TerrEm;

        return em.getReference(clazz, id);

    } catch (Exception e) {

        throw e;
    }

}

From source file:com.edugility.substantia.substance.TestCasePerson.java

@Test
public void testingJPA() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    assertNotNull(emf);//from w ww . j  av  a  2 s  .c  o  m

    final EntityManager em = emf.createEntityManager();
    assertNotNull(em);

    final EntityTransaction et = em.getTransaction();
    assertNotNull(et);
    et.begin();

    final Person p = new Person();
    em.persist(p);
    em.flush();
    assertFalse(p.isTransient());
    assertTrue(p.isVersioned());
    assertEquals(Long.valueOf(1L), p.getId());
    assertEquals(Integer.valueOf(1), p.getVersion());

    et.commit();
    et.begin();

    final Person p2 = em.find(Person.class, 1L, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    assertNotNull(p2);
    assertFalse(p2.isTransient());
    assertTrue(p2.isVersioned());
    assertEquals(Long.valueOf(1L), p2.getId());
    assertEquals(Integer.valueOf(1), p2.getVersion());

    et.commit();
    et.begin();

    final Person p3 = em.getReference(Person.class, 1L);
    assertNotNull(p3);
    assertFalse(p3.isTransient());
    assertTrue(p3.isVersioned());
    assertEquals(Long.valueOf(1L), p3.getId());
    assertEquals(Integer.valueOf(2), p3.getVersion());

    et.commit();
    et.begin();

    assertTrue(em.contains(p));
    em.remove(p);

    et.commit();

    em.close();

    emf.close();
}

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

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

    EntityTransaction t = em.getTransaction();

    t.begin();//w  w w. java2s  .  c o  m

    LivreEntry entry = new LivreEntry();
    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.getSagaId() != null && !e.getSagaId().trim().isEmpty()) {
        SagaEntry saga = em.getReference(SagaEntry.class, e.getSagaId());
        entry.setSaga(saga);
    }

    em.persist(entry);

    t.commit();
}

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();//from  w ww . j  a  v a2 s .co m

    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:org.nuxeo.theme.webwidgets.providers.PersistentProvider.java

public synchronized void removeWidget(final Widget widget) throws ProviderException {
    if (widget == null) {
        throw new ProviderException("Widget is undefined");
    }/*from w  w w. j av  a  2 s. c  om*/
    WidgetEntity widgetEntity = (WidgetEntity) widget;
    final int id = widgetEntity.getId();
    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                WidgetEntity w = em.getReference(WidgetEntity.class, id);
                em.remove(w);
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }
    List<Widget> widgets = getWidgets(widgetEntity.getRegion());
    reorderWidgets(widgets);
}

From source file:org.odk.aggregate.servlet.SubmissionServlet.java

/**
 * Handler for HTTP Get request that processes a form submission
 * // w w w  .  j av  a  2  s.co  m
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 */
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType(ServletConsts.RESP_TYPE_XML);

    // get parameter
    String odkFormKey = getParameter(req, ServletConsts.ODK_FORM_KEY);
    if (odkFormKey == null) {
        errorMissingKeyParam(resp);
        return;
    }

    // get form
    EntityManager em = EMFactory.get().createEntityManager();
    Key formKey = KeyFactory.stringToKey(odkFormKey);
    Form form = em.getReference(Form.class, formKey);

    if (form != null) {
        resp.getWriter().print(form.getOriginalForm());
    } else {
        odkIdNotFoundError(resp);
    }
    em.close();
}

From source file:org.rhq.enterprise.server.resource.test.ResourceStorageTest.java

public void testFindResourceComposite() throws Exception {
    getTransactionManager().begin();/*w w w.  j av  a  2s .com*/
    EntityManager em = getEntityManager();
    try {
        ResourceManagerLocal resourceManager = LookupUtil.getResourceManager();
        SubjectManagerLocal subjectManager = LookupUtil.getSubjectManager();
        PageControl pc = new PageControl(1, 5, new OrderingField("res.name", PageOrdering.ASC));
        Subject subject = subjectManager.loginUnauthenticated("ghinkle");

        //Subject subject = subjectManager.getOverlord();
        subject = createSession(subject);
        ResourceType t = em.getReference(ResourceType.class, new Integer(501064));
        String typeNameFilter = t == null ? null : t.getName();
        PageList<ResourceComposite> resources = resourceManager.findResourceComposites(subject,
                ResourceCategory.SERVER, typeNameFilter, null, null, "g", false, pc);
        System.out.println("Found resource composites: " + resources.size());
        for (ResourceComposite resourceComposite : resources) {
            System.out.println("\t" + resourceComposite);
        }
    } finally {
        getTransactionManager().rollback();
    }
}