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:nl.b3p.viewer.search.AttributeSourceSearchClient.java

private void getResults(String query, Integer solrConfigId, JSONArray processedResults) {

    try {/*from   ww w . j  a  v a2s .c o m*/
        EntityManager em = Stripersist.getEntityManager();

        SolrConf conf = em.find(SolrConf.class, solrConfigId.longValue());
        List<String> queryAttributes = conf.getIndexAttributes();
        List<String> resultAttributes = conf.getResultAttributes();

        SimpleFeatureType ft = conf.getSimpleFeatureType();

        org.geotools.data.FeatureSource gtFS = ft.openGeoToolsFeatureSource();

        FeatureToJson ftoj = new FeatureToJson(false, false, false, true, null);
        Query q = createQuery(queryAttributes, gtFS, query);
        q.setMaxFeatures(FeatureToJson.MAX_FEATURES);
        JSONArray features = ftoj.getJSONFeatures(null, ft, gtFS, q, null, null, em, null, null);

        for (Object feature : features) {
            JSONObject newFeature = new JSONObject();
            JSONObject oldFeature = (JSONObject) feature;
            String label = "";
            for (String name : resultAttributes) {
                Object value = oldFeature.optString(name, "");
                newFeature.put(name, value);
                label += " " + value;
            }
            Geometry g = (Geometry) oldFeature.get(ft.getGeometryAttribute());
            Envelope env = g.getEnvelopeInternal();

            Map bbox = new HashMap();
            bbox.put("minx", env.getMinX());
            bbox.put("miny", env.getMinY());
            bbox.put("maxx", env.getMaxX());
            bbox.put("maxy", env.getMaxY());

            newFeature.put("location", bbox);
            newFeature.put("type", conf.getName());
            newFeature.put("label", label);
            processedResults.put(newFeature);
        }

    } catch (Exception ex) {
        log.error("Error searching", ex);
    }
}

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

@Override
@Transactional//  w  w w .j av  a 2 s . c o m
public void deleteMultimedia(Multimedia multimedia) {
    Validate.notNull(multimedia, "multimedia can not be null");

    final EntityManager entityManager = this.getEntityManager();

    final MultimediaImpl multimediaImpl = entityManager.find(MultimediaImpl.class,
            multimedia.getMultimediaId());

    final ConferenceUser creator = multimediaImpl.getCreator();
    final ConferenceUserImpl creatorImpl = this.conferenceUserDao.getUser(creator.getUserId());
    creatorImpl.getMultimedias().remove(multimediaImpl);

    entityManager.remove(multimediaImpl);
    //entityManager.remove(creatorImpl);      
}

From source file:org.viafirma.persistencia.dao.GenericEjb3Dao.java

/**
 * Elimina la entidad indicada/*from w  w w  .  j av  a  2s . c  o m*/
 * @throws ExcepcionNoCreado 
 */
public void delete(ID id) throws ExcepcionNoCreado {
    log.finest("Eliminando " + persistentClass + " con identificador " + id);
    EntityManager manager = getEntityManager();
    T entidad = null;
    entidad = manager.find(persistentClass, id);
    if (entidad == null) {
        throw new ExcepcionNoCreado("No se puede eliminar " + id);
    }

    try {

        manager.getTransaction().begin();
        manager.remove(entidad);
        manager.getTransaction().commit();
    } catch (Exception e) {
        log.severe("No se puede eliminar " + entidad + " " + e.getMessage());
        manager.getTransaction().rollback();
    } finally {
        manager.close();
    }
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para realizar a busca de usurio pelo ID.
 * @author Richel Sensineli//from   w  ww . ja  va 2 s  .  com
 * @param id int - ID do usurio
 * @return Usuario usuario - Objeto Usuario
 */
@Override
public Usuario buscaUsuarioPorId(final int id) throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();
    Usuario u = em.find(UsuarioImpl.class, id);
    if (u == null) {
        throw new UsuarioNaoEncontradoException("usuario no encontrado");
    }
    em.clear();
    em.close();
    emf.close();
    return u;
}

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

@Override
@Transactional/*from w w w.  j av a  2  s  . c o m*/
public void deletePresentation(Presentation presentation) {
    Validate.notNull(presentation, "presentation can not be null");

    final EntityManager entityManager = this.getEntityManager();

    final PresentationImpl presentationImpl = entityManager.find(PresentationImpl.class,
            presentation.getPresentationId());

    final ConferenceUser creator = presentationImpl.getCreator();
    final ConferenceUserImpl creatorImpl = this.conferenceUserDao.getUser(creator.getUserId());
    creatorImpl.getPresentations().remove(presentationImpl);

    entityManager.remove(presentationImpl);
    entityManager.persist(creatorImpl);
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a remoo de usurio.
 * @author Richel Sensineli//from  w  w w.  j a v a 2  s  .  com
 * @param id int - ID do usurio
 * @throws UsuarioNaoEncontradoException - usurio no encontrado
 */
@Override
public void removeUsuario(final int id) throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    Usuario u = em.find(UsuarioImpl.class, id);

    em.getTransaction().begin();
    if (u == null) {
        throw new UsuarioNaoEncontradoException("usuario no encontrado");
    } else {
        em.remove(u);
        em.getTransaction().commit();
    }
    em.clear();
    em.close();
    emf.close();

}

From source file:com.soen.ebanking.dao.ObjectDao.java

public void deleteObject(Object object, long id, Class<T> ClassName) {
    EntityManager em = this.getEMF().createEntityManager();
    try {//from  w w w .  ja va2 s.  co m
        em.getTransaction().begin();
        T entity = em.find(ClassName, id);
        em.remove(entity);
        em.getTransaction().commit();
    } finally {
        em.close();
    }
}

From source file:de.zib.gndms.dspace.slice.service.globus.resource.SliceResource.java

@Override
public void remove() {

    logger.debug("removing slice resource: " + getID());
    EntityManager em = resourceHome.getEntityManagerFactory().createEntityManager();
    Slice sl = em.find(Slice.class, getID());
    logger.debug("removing slice directory: " + sl.getDirectoryId());
    // the action takes care of the EntityManager from now on
    /*Future f =*/ resourceHome.getSystem().submitAction(em, new DeleteSliceAction(sl), logger);
}

From source file:org.eclipse.skalli.core.storage.jpa.JPAStorageComponent.java

private StorageItem findStorageItem(String category, String id, EntityManager em) throws StorageException {
    StorageItem item;//w  w  w  .  jav  a 2s  .c  om
    try {
        item = em.find(StorageItem.class, new StorageId(category, id));
    } catch (Exception e) {
        throw new StorageException("Failed to find item", e);
    }
    return item;
}

From source file:nl.b3p.kaartenbalie.service.requesthandler.GetMapRequestHandler.java

/**
 * Returns a set of all SRSes (not SrsBoundingBox entities, the SRS id
 * strings) of the layer and all parent layers upto the toplayer. May return
 * an empty Set if the layer id is unknown or there simply are no SRSes for
 * the layers./*from   w  w w .  j ava 2 s.  c om*/
 */
private Set<String> getSRS(int layerId, EntityManager em) {
    Layer l = em.find(Layer.class, layerId);
    if (l == null) {
        log.warn("getSRS(): layer with id " + layerId + " not found");
        return new HashSet<String>();
    }

    Set<String> srses = new HashSet<String>();

    srses.addAll(getSRSStrings(l.getSrsbb()));

    while (l.getParent() != null) {
        /* This is not the toplayer, so get the SRS's of the
         * layer higher up in the tree
         */
        l = l.getParent();
        srses.addAll(getSRSStrings(l.getSrsbb()));
    }
    return srses;
}