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:de.zib.gndms.infra.system.GNDMSystemDirectory.java

@SuppressWarnings({ "MethodWithTooExceptionsDeclared" })
@NotNull//w w  w.  j  ava2  s . c  o m
public AbstractORQCalculator<?, ?> newORQCalculator(final @NotNull EntityManagerFactory emf,
        final @NotNull String offerTypeKey) throws ClassNotFoundException, IllegalAccessException,
        InstantiationException, NoSuchMethodException, InvocationTargetException {
    EntityManager em = emf.createEntityManager();
    try {
        OfferType type = em.find(OfferType.class, offerTypeKey);
        if (type == null)
            throw new IllegalArgumentException("Unknow offer type: " + offerTypeKey);
        AbstractORQCalculator<?, ?> orqc = orqPark.getInstance(type);
        orqc.setConfigletProvider(this);
        return orqc;
    } finally {
        if (!em.isOpen())
            em.close();
    }
}

From source file:org.sigmah.server.endpoint.export.sigmah.handler.ProjectModelHandler.java

@Override
public String exportModel(OutputStream outputStream, String identifier, EntityManager em)
        throws ExportException {

    String name = "";

    if (identifier != null) {
        final Long projectModelId = Long.parseLong(identifier);

        final ProjectModel hibernateModel = em.find(ProjectModel.class, projectModelId);

        if (hibernateModel == null)
            throw new ExportException(
                    "No project model is associated with the identifier '" + identifier + "'.");

        name = hibernateModel.getName();

        // Removing superfluous links
        hibernateModel.setVisibilities(null);

        // Stripping hibernate proxies from the model.
        final ProjectModel realModel = Realizer.realize(hibernateModel);

        // Serialization
        try {/*from   w  w  w.  ja va  2  s  .  c o  m*/
            final ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(realModel);

        } catch (IOException ex) {
            throw new ExportException("An error occured while serializing the project model " + projectModelId,
                    ex);
        }

    } else {
        throw new ExportException("The identifier is missing.");
    }

    return name;
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public FStoreUser readFStoreUserById(long l) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    return entityManager.find(FStoreUser.class, l);

    //      Query q = entityManager.createQuery("SELECT m FROM FStoreUser m WHERE m.id=" + userid );      
    //      return (q.getResultList().size()==0)?null:(FStoreUser) q.getSingleResult();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public Product readProductByID(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Product u = entityManager.find(Product.class, id);
    return u;/*w w w  . j  av  a2  s .  c  o m*/
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public Category readCategoryByID(int catid) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Category u = entityManager.find(Category.class, catid);
    return u;//  www .j av a 2s  .com
}

From source file:org.apache.juddi.api.impl.UDDIPublicationImplExt.java

private void setOperationalInfo(EntityManager em, org.apache.juddi.model.BusinessEntity uddiEntity,
        UddiEntityPublisher publisher) throws DispositionReportFaultMessage {

    uddiEntity.setAuthorizedName(publisher.getAuthorizedName());

    Date now = new Date();
    uddiEntity.setModified(now);/*from   w  w w  .j a va  2s .  com*/
    uddiEntity.setModifiedIncludingChildren(now);

    uddiEntity.setNodeId(nodeId);

    org.apache.juddi.model.BusinessEntity existingUddiEntity = em.find(uddiEntity.getClass(),
            uddiEntity.getEntityKey());
    if (existingUddiEntity != null)
        uddiEntity.setCreated(existingUddiEntity.getCreated());
    else
        uddiEntity.setCreated(now);

    List<org.apache.juddi.model.BusinessService> serviceList = uddiEntity.getBusinessServices();
    for (org.apache.juddi.model.BusinessService service : serviceList)
        setOperationalInfo(em, service, publisher, true);

    if (existingUddiEntity != null)
        em.remove(existingUddiEntity);

}

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

public Resolution tree() throws JSONException {

    EntityManager em = Stripersist.getEntityManager();

    final JSONArray children = new JSONArray();

    String type = nodeId.substring(0, 1);
    int id = Integer.parseInt(nodeId.substring(1));
    if (type.equals("c")) {
        Category c = em.find(Category.class, new Long(id));
        for (Category sub : c.getChildren()) {
            JSONObject j = new JSONObject();
            j.put("id", "c" + sub.getId());
            j.put("name", sub.getName());
            j.put("type", "category");
            j.put("isLeaf", sub.getChildren().isEmpty() && sub.getServices().isEmpty());
            if (sub.getParent() != null) {
                j.put("parentid", sub.getParent().getId());
            }//from  ww  w.  j  ava2s  .  c o  m
            children.put(j);
        }

        for (GeoService service : c.getServices()) {
            JSONObject j = new JSONObject();
            j.put("id", "s" + service.getId());
            j.put("name", service.getName());
            j.put("type", "service");
            j.put("isLeaf", service.getTopLayer() == null);
            j.put("status", service.isMonitoringStatusOK() ? "ok" : "error");
            j.put("parentid", nodeId);
            children.put(j);
        }
    } else if (type.equals("s")) {
        GeoService gs = em.find(GeoService.class, new Long(id));
        // GeoService may be invalid and not have a top layer
        if (gs.getTopLayer() != null) {
            List<Layer> layers;
            if (!gs.getTopLayer().isVirtual()) {
                layers = Collections.singletonList(gs.getTopLayer());
            } else {
                layers = gs.getTopLayer().getChildren();
            }
            for (Layer sublayer : layers) {
                JSONObject j = new JSONObject();
                j.put("id", "l" + sublayer.getId());
                if (sublayer.getTitleAlias() != null) {
                    j.put("name", sublayer.getTitleAlias());
                } else if (sublayer.getTitle() != null) {
                    j.put("name", sublayer.getTitle());
                } else {
                    j.put("name", sublayer.getName());
                }
                j.put("type", "layer");
                j.put("isLeaf", sublayer.getChildren().isEmpty());
                j.put("parentid", nodeId);
                j.put("isVirtual", sublayer.isVirtual());
                children.put(j);
            }
        }
    }
    if (type.equals("l")) {
        Layer layer = em.find(Layer.class, new Long(id));
        for (Layer sublayer : layer.getChildren()) {
            JSONObject j = new JSONObject();
            j.put("id", "l" + sublayer.getId());
            if (sublayer.getTitleAlias() != null) {
                j.put("name", sublayer.getTitleAlias());
            } else if (sublayer.getTitle() != null) {
                j.put("name", sublayer.getTitle());
            } else {
                j.put("name", sublayer.getName());
            }
            j.put("type", "layer");
            j.put("isLeaf", sublayer.getChildren().isEmpty());
            j.put("parentid", nodeId);
            j.put("isVirtual", sublayer.isVirtual());
            children.put(j);
        }
    }

    return new StreamingResolution("application/json") {
        @Override
        public void stream(HttpServletResponse response) throws Exception {
            response.getWriter().print(children.toString());
        }
    };
}

From source file:org.apache.juddi.api.impl.UDDIPublicationImplExt.java

private void setOperationalInfo(EntityManager em, org.apache.juddi.model.BusinessService uddiEntity,
        UddiEntityPublisher publisher, boolean isChild) throws DispositionReportFaultMessage {

    uddiEntity.setAuthorizedName(publisher.getAuthorizedName());

    Date now = new Date();
    uddiEntity.setModified(now);//from  ww  w.  jav  a  2 s.  c  om
    uddiEntity.setModifiedIncludingChildren(now);

    if (!isChild) {
        org.apache.juddi.model.BusinessEntity parent = em.find(org.apache.juddi.model.BusinessEntity.class,
                uddiEntity.getBusinessEntity().getEntityKey());
        parent.setModifiedIncludingChildren(now);
        em.persist(parent);
    }

    uddiEntity.setNodeId(nodeId);

    org.apache.juddi.model.BusinessService existingUddiEntity = em.find(uddiEntity.getClass(),
            uddiEntity.getEntityKey());
    if (existingUddiEntity != null) {
        uddiEntity.setCreated(existingUddiEntity.getCreated());
    } else
        uddiEntity.setCreated(now);

    List<org.apache.juddi.model.BindingTemplate> bindingList = uddiEntity.getBindingTemplates();
    for (org.apache.juddi.model.BindingTemplate binding : bindingList)
        setOperationalInfo(em, binding, publisher, true);

    if (existingUddiEntity != null)
        em.remove(existingUddiEntity);

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteProduct(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Product p = entityManager.find(Product.class, id);

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from w  w w .ja  v  a2s .  c  om
    entityManager.remove(p);
    entityTransaction.commit();
}

From source file:org.apache.juddi.api.impl.UDDIPublicationImplExt.java

private void setOperationalInfo(EntityManager em, org.apache.juddi.model.BindingTemplate uddiEntity,
        UddiEntityPublisher publisher, boolean isChild) throws DispositionReportFaultMessage {

    uddiEntity.setAuthorizedName(publisher.getAuthorizedName());

    Date now = new Date();
    uddiEntity.setModified(now);//from  w w w  .j  av a 2 s  .c  o m
    uddiEntity.setModifiedIncludingChildren(now);

    if (!isChild) {
        org.apache.juddi.model.BusinessService parent = em.find(org.apache.juddi.model.BusinessService.class,
                uddiEntity.getBusinessService().getEntityKey());
        parent.setModifiedIncludingChildren(now);
        em.persist(parent);

        // JUDDI-421:  now the businessEntity parent will have it's modifiedIncludingChildren set
        org.apache.juddi.model.BusinessEntity businessParent = em
                .find(org.apache.juddi.model.BusinessEntity.class, parent.getBusinessEntity().getEntityKey());
        businessParent.setModifiedIncludingChildren(now);
        em.persist(businessParent);
    }

    uddiEntity.setNodeId(nodeId);

    org.apache.juddi.model.BindingTemplate existingUddiEntity = em.find(uddiEntity.getClass(),
            uddiEntity.getEntityKey());
    if (existingUddiEntity != null)
        uddiEntity.setCreated(existingUddiEntity.getCreated());
    else
        uddiEntity.setCreated(now);

    if (existingUddiEntity != null)
        em.remove(existingUddiEntity);

}