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:org.eclipse.jubula.client.core.persistence.NodePM.java

/**
 * Returns all test cases that reference the test case given information. 
 * This method opens a new session to gather the test cases and then closes
 * the session in order to prevent accidental DB commits for test cases
 * external to the current project./*  w  ww  . j  a  v  a2  s.  c  o  m*/
 * Warning: the fetched ExecTestCases have no parent, because the database
 * doesn't know the parent.
 * @param specTcGuid GUID of the test case being reused.
 * @param parentProjectId ID of the parent project of the test case being
 *                        reused.
 * @return <em>all</em> test cases that reference the test case with the 
 *         given information, regardless of which project they are in.
 * @see getInternalExecTestCases
 * @see getExternalExecTestCases
 */
public static List<IExecTestCasePO> getAllExecTestCases(String specTcGuid, long parentProjectId)
        throws JBException {
    // a SpecTC with guid == null can't be reused
    if (specTcGuid == null) {
        return new ArrayList<IExecTestCasePO>(0);
    }

    EntityManager s = Persistor.instance().openSession();

    try {
        EntityTransaction tx = Persistor.instance().getTransaction(s);

        IProjectPO parentProject = s.find(NodeMaker.getProjectPOClass(), parentProjectId);

        if (parentProject == null) {
            String error = Messages.ParentProjectDoesNotExistWithID + StringConstants.COLON
                    + StringConstants.SPACE + parentProjectId;
            log.error(error);
            throw new JBException(error, MessageIDs.E_DATABASE_GENERAL);
        }

        List<Long> projectsThatReuse = ProjectPM.findIdsThatReuse(parentProject.getGuid(),
                parentProject.getMajorProjectVersion(), parentProject.getMinorProjectVersion());
        // Project technically "reuses" itself
        projectsThatReuse.add(parentProjectId);

        List<IExecTestCasePO> tcList = getExecTestCasesFor(specTcGuid, projectsThatReuse, s);

        Persistor.instance().commitTransaction(s, tx);

        return tcList;
    } finally {
        Persistor.instance().dropSessionWithoutLockRelease(s);
    }
}

From source file:fr.amapj.service.services.saisiepermanence.PermanenceService.java

/**
 * Permet de supprimer une distribution//w w  w .j a v  a  2s. c  o  m
 * Ceci est fait dans une transaction en ecriture
 */
@DbWrite
public void deleteDistribution(final Long id) {
    EntityManager em = TransactionHelper.getEm();

    DatePermanence d = em.find(DatePermanence.class, id);

    List<DatePermanenceUtilisateur> dus = getAllDateDistriUtilisateur(em, d);
    for (DatePermanenceUtilisateur du : dus) {
        em.remove(du);
    }

    em.remove(d);
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testInsertNull() {
    insert(newLobEntity(null, 1));//from   w w w .  ja  v  a  2s.com
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    LobEntity le = (LobEntity) em.find(getLobEntityClass(), 1);
    assertNull(le.getStream());
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testLifeCycleLoadFlushModifyFlush() {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*from  w w w  .j ava2 s  . c  o m*/
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    em.flush();
    changeStream(entity, createLobData2());
    em.flush();
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testUpdateWithNull() {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*from w  ww . j a  v a2 s.  c o  m*/
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    entity.setStream(null);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    entity = (LobEntity) em.find(getLobEntityClass(), 1);
    assertNull(entity.getStream());
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testDelete() {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//from  w  w w  .j a va  2  s .com
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    em.remove(entity);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query q = em.createQuery(getSelectQuery());
    assertEquals(0, q.getResultList().size());
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testReadingMultipleTimesWithASingleConnection() throws IOException {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//from ww w.ja  v a2 s  .c  o m
    LobEntity le = (LobEntity) em.find(getLobEntityClass(), 1);
    String string = createLobData2();
    changeStream(le, string);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    le = (LobEntity) em.find(getLobEntityClass(), 1);
    assertNotNull(le.getStream());
    LobEntity entity = newLobEntity(createLobData(), 2);
    em.persist(entity);
    assertEquals(string, getStreamContentAsString(le.getStream()));
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testUpdate() throws IOException {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*www.  ja va2 s . c o m*/
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    String string = createLobData2();
    changeStream(entity, string);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    entity = (LobEntity) em.find(getLobEntityClass(), 1);
    assertEquals(string, getStreamContentAsString(entity.getStream()));
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testUpdateANullObjectWithoutNull() throws IOException {
    insert(newLobEntity(null, 1));//from   w w  w .jav a2s. c  o m
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    String string = createLobData2();
    changeStream(entity, string);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    entity = (LobEntity) em.find(getLobEntityClass(), 1);
    assertEquals(string, getStreamContentAsString(entity.getStream()));
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.juddi.v3.auth.LdapExpandedAuthenticator.java

public UddiEntityPublisher identify(String authInfo, String authorizedName)
        throws AuthenticationException, FatalErrorException {
    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {/*  www  .j a  va2  s.co m*/
        tx.begin();
        Publisher publisher = em.find(Publisher.class, authorizedName);
        if (publisher == null)
            throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
        return publisher;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}