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.croodie.resource.UserServerResource.java

@Get
public Representation retrieveUsers(Representation entity) {
    Form form = new Form(entity);
    String id = form.getFirstValue("id");
    logger.info("Fetching user with id: " + id);
    EntityManagerFactory emf = EMF.get();
    EntityManager em = emf.createEntityManager();
    User user = null;//from   www  .  j  ava 2  s . c om
    try {
        user = em.find(User.class, id);
    } finally {
        em.close();
    }
    return null;
}

From source file:com.chiralbehaviors.CoRE.access.resource.CollectionResource.java

@POST
@Path("{parentId}")
@Produces(MediaType.APPLICATION_JSON)/*from  w  w w . j a v  a 2s .c  o m*/
public Product createNewProduct(@PathParam("parentId") String parentId, @QueryParam("relId") String relId,
        Product child) throws JsonProcessingException {
    return perform(new Transactionally<Product>() {

        @Override
        public Product exec(Model model) throws SQLException {
            EntityManager em = model.getEntityManager();
            Product parent = em.find(Product.class, parentId);
            Relationship rel = em.find(Relationship.class, relId);
            em.persist(child);
            ProductNetwork net = new ProductNetwork(parent, rel, child, parent.getUpdatedBy());
            em.persist(net);
            em.flush();
            em.refresh(child);
            return child;
        }
    });
}

From source file:it.infn.ct.futuregateway.apiserver.v1.InfrastructureService.java

/**
 * Retrieve the infrastructure details.//from  ww w  .  j av a 2s . c om
 * Infrastructure details include all the information needed to access the
 * remote infrastructure.
 *
 * @param id The infrastructure id. This is a path parameter retrieved from
 * the URL
 * @return The infrastructure
 */
@GET
@Produces(Constants.INDIGOMIMETYPE)
public final Infrastructure getInfraDetails(@PathParam("id") final String id) {
    Infrastructure infra;
    EntityManager em = getEntityManager();
    try {
        infra = em.find(Infrastructure.class, id);
    } catch (IllegalArgumentException re) {
        log.error("Impossible to retrieve the application");
        log.error(re);
        throw new RuntimeException("Impossible to access the application " + "list");
    } finally {
        em.close();
    }
    if (infra == null) {
        throw new NotFoundException();
    } else {
        return infra;
    }
}

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

public T getObjectById(long id, Class<T> ClassName) {
    EntityManager em = this.getEMF().createEntityManager();
    try {//from ww  w .  j a  va 2s  .  c  o m
        return em.find(ClassName, id);
    } finally {
        em.close();
    }
}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public Troop getTroop(Long id) throws IllegalArgumentException {
    if (id == null) {
        throw new IllegalArgumentException("getTroop called with null");
    }//from  w ww .jav  a 2 s  .  c  o  m
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Troop troop = em.find(Troop.class, id);
    em.getTransaction().commit();
    em.close();
    return troop;
}

From source file:it.infn.ct.futuregateway.apiserver.v1.ApplicationService.java

/**
 * Retrieves the application details.// www .java  2  s . c  om
 * Application details include all the fields an application
 * consist of as described in the documentation. This include all the
 * information included in the task collection and many others.
 *
 * @param id The application id. This is a path parameter retrieved from
 * the URL
 * @return The application
 */
@GET
@Produces(Constants.INDIGOMIMETYPE)
public final Application getAppDetails(@PathParam("id") final String id) {
    Application app;
    EntityManager em = getEntityManager();
    try {
        app = em.find(Application.class, id);
        log.debug("Find the application " + id + " associated with " + app.getInfrastructureIds().size()
                + " infrastructures " + "and " + app.getParameters().size() + " parameters");
    } catch (IllegalArgumentException re) {
        log.error("Impossible to retrieve the application");
        log.error(re);
        throw new RuntimeException("Impossible to access the application " + "list");
    } finally {
        em.close();
    }
    if (app == null) {
        throw new NotFoundException();
    } else {
        return app;
    }
}

From source file:com.emc.plants.service.impl.MailerBean.java

/**
 * Create the email message./*ww  w.  j  a  va  2s.  com*/
 *
 * @param orderKey The order number.
 * @return The email message.
 */
private String createMessage(long orderKey) {
    Util.debug("creating email message for order:" + orderKey);
    StringBuffer msg = new StringBuffer();
    /*
     OrderHome orderHome = (OrderHome) Util.getEJBLocalHome("java:comp/env/ejb/Order",
     OrderHome.class);
     Order order = orderHome.findByPrimaryKey(new OrderKey(orderKey));
     */
    EntityManager em = entityManagerFactory.createEntityManager();
    Order order = em.find(Order.class, orderKey);
    msg.append("Thank you for your order " + orderKey + ".\n");
    msg.append("Your order will be shipped to: " + order.getShipName() + "\n");
    msg.append("                               " + order.getShipAddr1() + " " + order.getShipAddr2() + "\n");
    msg.append("                               " + order.getShipCity() + ", " + order.getShipState() + " "
            + order.getShipZip() + "\n\n");
    msg.append("Please save it for your records.\n");
    return msg.toString();
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User setApiKey(User user, String apiKey) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*from   ww w .ja v  a2s .c o  m*/
    User u = (User) em.find(User.class, user.getId());
    u.setApiKey(apiKey);
    em.getTransaction().commit();

    return u;
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User findUser(int id) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//w  w w  . j  a  v  a2  s  .co m
    User user = (User) em.find(User.class, id);
    em.getTransaction().commit();
    return user;
}

From source file:com.boylesoftware.web.impl.auth.AbstractUserRecordHandler.java

@Override
public T getUser(final int id, final int salt, final EntityManagerFactory emf) {

    final boolean debug = this.log.isDebugEnabled();

    if (debug) {/*w  w  w  .jav  a  2  s  .  c o m*/
        this.log.debug("looking up user id " + id + ", salt " + salt + " in the database");
        this.log.debug("creating entity manager");
    }
    final EntityManager em = emf.createEntityManager();
    try {
        final T user = em.find(this.userRecordClass, Integer.valueOf(id));

        if ((user == null) || (this.getUserSalt(user) != salt)) {
            if (debug)
                this.log.debug("user not found or salt does not match");
            return null;
        }

        if (debug)
            this.log.debug("user found");
        return user;

    } finally {
        if (debug)
            this.log.debug("closing entity manager");
        em.close();
    }
}