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.artificer.repository.hibernate.HibernateUtil.java

public static ArtificerStoredQuery getStoredQuery(String queryName, EntityManager entityManager)
        throws ArtificerException {
    ArtificerStoredQuery storedQuery = entityManager.find(ArtificerStoredQuery.class, queryName);
    if (storedQuery == null) {
        throw ArtificerNotFoundException.storedQueryNotFound(queryName);
    }//  www  .  ja  v a 2s .c  o  m
    return storedQuery;
}

From source file:com.github.jinahya.persistence.ShadowTest.java

protected static boolean puthenticate(final EntityManager manager, final long id, final String username,
        final byte[] password) {

    final Shadow shadow = manager.find(Shadow.class, id);

    if (shadow == null) {
        return false;
    }/*from   w  w  w . j  a  va  2  s  .c  o m*/

    return shadow.puthenticate(shadow, password);

}

From source file:org.eclipse.jubula.client.core.businessprocess.db.NodeBP.java

/**
 * @param editSupport holding the DB session for locking purposes
 * @param node node to lock//from   www  .j  av  a2  s.  co  m
 * @throws PMObjectDeletedException
 * @throws PMDirtyVersionException
 * @throws PMAlreadyLockedException
 */
protected static void lockPO(EditSupport editSupport, INodePO node)
        throws PMObjectDeletedException, PMDirtyVersionException, PMAlreadyLockedException {
    final EntityManager lockSession = editSupport.getSession();
    try {
        try {
            // make sure there is no old version
            // in the session cache
            lockSession.detach(node);
            lockSession.find(node.getClass(), node.getId());
        } catch (PersistenceException e) {
            PersistenceManager.handleDBExceptionForEditor(node, e, editSupport);
        }
    } catch (PMDirtyVersionException e) { // NOPMD by al on 3/19/07 1:25 PM
        // ignore, we are not interested in version checking
    } catch (PMObjectDeletedException e) {
        // OK, this may happen, just forward to caller
        throw e;
    } catch (PMException e) {
        // Continue since we are just refreshing the cache
        LOG.error(Messages.StrayPersistenceException + StringConstants.DOT + StringConstants.DOT, e);
    }
    if (!LockManager.instance().lockPO(lockSession, node, false)) {
        throw new PMAlreadyLockedException(node, Messages.OrginalTestcaseLocked + StringConstants.DOT,
                MessageIDs.E_OBJECT_IN_USE);
    }
}

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

/**
 * /*from w w w. j a v a 2  s  . c  o m*/
 * @param organizationId
 * @return
 */
public synchronized static AccountManager getAccountManager(Integer organizationId) throws Exception {
    AccountManager accountManager = (AccountManager) managers.get(organizationId);
    if (accountManager == null) {
        accountManager = new AccountManager(organizationId);
        if (enableAccounting) {
            log.debug("Getting entity manager ......");
            EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);
            Account account = (Account) em.find(Account.class, organizationId);
            if (account == null) {
                Organization organization = (Organization) em.find(Organization.class, organizationId);
                account = new Account(organization);
                organization.setAccount(account);
                em.persist(account);
                em.flush();
            }
            managers.put(organizationId, accountManager);
        }
    }
    return accountManager;
}

From source file:nl.b3p.kaartenbalie.service.GroupParser.java

public static void addRightsForAllLayers(String[] orgSelected, WfsServiceProvider sp, EntityManager em)
        throws Exception {
    if (orgSelected == null || sp == null) {
        return;/*from ww  w .  ja  v a2  s. c o  m*/
    }

    for (int i = 0; i < orgSelected.length; i++) {
        Organization org = (Organization) em.find(Organization.class, new Integer(orgSelected[i]));

        addAllLayersToGroup(org, sp, em);
    }
}

From source file:nl.b3p.kaartenbalie.service.GroupParser.java

public static void addRightsForAllLayers(String[] orgSelected, ServiceProvider sp, EntityManager em)
        throws Exception {
    if (orgSelected == null || sp == null) {
        return;// www . j  a  va 2s .co m
    }

    for (int i = 0; i < orgSelected.length; i++) {
        Organization org = (Organization) em.find(Organization.class, new Integer(orgSelected[i]));

        addAllLayersToGroup(org, sp, em);
    }
}

From source file:org.rhq.enterprise.server.discovery.AgentInventoryStatusUpdateJob.java

public static void internalizeJobValues(EntityManager entityManager, String valuesCsvList,
        List<Resource> resources) {
    if (valuesCsvList == null)
        return;//  www . j  a  v a 2s .c o  m

    final String[] resourceIdStrings = valuesCsvList.split(",");
    for (String resourceIdString : resourceIdStrings) {
        int resourceId = Integer.parseInt(resourceIdString);
        resources.add(entityManager.find(Resource.class, resourceId));
    }
}

From source file:org.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * deleting monitoring reports by age (days of existence) 
 * or deleting all, if the summaryId is null.
 * @param session The current session (EntityManager)
 * @param summaryId The summaryToDelete. 
 * This value can be null, if all test results were deleted.
 * if summaryId is null, all monitoring reports will also be deleted.  
 *///from   ww  w  . j a  va 2 s .  co m
private static void deleteMonitoringReports(EntityManager session, Long summaryId) {

    ITestResultSummaryPO summaryPo = null;
    if (summaryId != null) {
        summaryPo = session.find(PoMaker.getTestResultSummaryClass(), summaryId);
        if (summaryPo != null) {
            summaryPo.setMonitoringReport(null);
            summaryPo.setReportWritten(false);
        }

    } else {
        // Optimization: Since all monitoring reports need to be deleted,
        //               we delete them with a JPQL query before removing 
        //               them via iteration. This prevents OutOfMemoryErrors
        //               caused by loading all reports in to memory at the 
        //               same time.
        Query deleteMonitoringReportsQuery = session
                .createQuery("UPDATE " + PoMaker.getMonitoringReportClass().getSimpleName() //$NON-NLS-1$
                        + " SET report = null"); //$NON-NLS-1$
        deleteMonitoringReportsQuery.executeUpdate();
        session.flush();

        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("SELECT summary FROM ") //$NON-NLS-1$
                .append(PoMaker.getTestResultSummaryClass().getSimpleName())
                .append(" AS summary where summary.reportWritten = :isReportWritten"); //$NON-NLS-1$

        Query q = session.createQuery(queryBuilder.toString());
        q.setParameter("isReportWritten", true); //$NON-NLS-1$

        @SuppressWarnings("unchecked")
        List<ITestResultSummaryPO> reportList = q.getResultList();
        for (ITestResultSummaryPO summary : reportList) {
            summary.setMonitoringReport(null);
            summary.setReportWritten(false);
        }
    }
}

From source file:org.cesecore.audit.impl.queued.entity.LogManagementData.java

public static LogManagementData findById(final EntityManager em, final Long id) {
    return em.find(LogManagementData.class, id);
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Get access point by access point id.
 * @param id access point id//ww w.  ja v  a  2 s  .c o m
 * @return the access point
 */
public static AccessPoint getAccessPointById(final int id) {
    EntityManager em = DBContext.getEntityManager();
    AccessPoint ap = em.find(AccessPoint.class, id);
    em.close();
    return ap;
}