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:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.PersistentDocumentController.java

/**
 * Gets all UUIDs for {@link PersistentDocument} objects that are associated with a particular {@link PersistentDocumentStore}.
 * @param em the {@link EntityManager} to use.
 * @param storeId the ID of the store to look for.
 * @return a {@link List} containing {@link String} representations of the UUIDs.
 *///from   w ww.  j  a v  a2 s  .  c o  m
public List<String> getAllUuids(EntityManager em, String storeId) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(storeId, CannedMessages.NULL_ARGUMENT, "storeId");

    PersistentDocumentStore store = em.find(PersistentDocumentStore.class, UuidUtils.toBytes(storeId));
    return Lists
            .newArrayList(Iterables.transform(store.getDocumentIds(em), UuidUtils.uuidBytesToStringFunction()));
}

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

/**
 * Retrieves the task details. Task details include all the fields a task
 * consist of as described in the documentation. This include all the
 * information included in the task collection and many others.
 *
 * @param id The task id. This is a path parameter retrieved from the url
 * @return The task/*  w  ww .ja v a  2s . co m*/
 */
@GET
@Produces(Constants.INDIGOMIMETYPE)
public final Task getTaskDetails(@PathParam("id") final String id) {
    Task task;
    EntityManager em = getEntityManager();
    try {
        task = em.find(Task.class, id);
    } catch (IllegalArgumentException re) {
        log.error("Impossible to retrieve the task");
        log.error(re);
        throw new RuntimeException("Impossible to access the task list");
    } finally {
        em.close();
    }
    if (task == null) {
        throw new NotFoundException();
    } else {
        log.debug("Associated " + task.getInputFiles().size() + " files");
        return task;
    }
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a atualizao do usurio
 * @author Richel Sensineli//from  w  w w .j a va2  s  .  com
 * @param id int - ID do usurio
 * @param nome String - Nome do usurio
 * @param sobrenome String - Nome do usurio
 */
@Override
public void updateUsuario(final int id, final String nome, final String sobrenome)
        throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    UsuarioImpl user = em.find(UsuarioImpl.class, id);
    user.setNome(nome);
    user.setSobrenome(sobrenome);

    em.getTransaction().begin();
    try {
        em.merge(user);
        em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
    }

    em.clear();
    em.close();
    emf.close();
}

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

public void updateObject(Object entity, long id, Class<T> ClassName) {
    EntityManager em = this.getEMF().createEntityManager();
    try {/*from w  w  w  .  j  av  a  2 s  .  c om*/
        em.getTransaction().begin();
        this.t = (T) em.find(ClassName, id);
        Object orig = entity;
        Object dest = this.t;
        BeanUtils.copyProperties(dest, orig);
        entity = t;
        t = null;
        em.getTransaction().commit();
    } catch (IllegalAccessException ex) {
        Logger.getLogger(ObjectDao.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvocationTargetException ex) {
        Logger.getLogger(ObjectDao.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        em.close();
    }
}

From source file:fr.mby.opa.picsimpl.dao.DbAlbumDao.java

@Override
public Album findAlbumById(final Long id) {
    Assert.notNull(id, "Album Id should be supplied !");

    final EmCallback<Album> emCallback = new EmCallback<Album>(this.getEmf()) {

        @Override/* ww w .j  a  v a2 s  .c o  m*/
        protected Album executeWithEntityManager(final EntityManager em) throws PersistenceException {
            return em.find(Album.class, id);
        }
    };

    return emCallback.getReturnedValue();
}

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

/**
 * Uploads input files. The method store input files for the specified task.
 * Input files are provided as a <i>multipart form data</i> using the field
 * file. This can contains multiple file using the html input attribute
 * <i>multiple="multiple"</i> which allows to associate multiple files with
 * a single field.//from  w  w  w  . ja v a2s  . co m
 *
 * @param id The task id retrieved from the url path
 * @param lstFiles List of file in the POST body
 */
@Path("/input")
@POST
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public final void setInputFile(@PathParam("id") final String id,
        @FormDataParam("file") final List<FormDataBodyPart> lstFiles) {
    if (lstFiles == null || lstFiles.isEmpty()) {
        throw new BadRequestException("Input not accessible!");
    }
    EntityManager em = getEntityManager();
    Task task = em.find(Task.class, id);
    task.addObserver(new TaskObserver(getEntityManagerFactory(), getSubmissionThreadPool()));
    if (task == null) {
        throw new NotFoundException("Task " + id + " does not exist");
    }
    for (FormDataBodyPart fdbp : lstFiles) {
        final String fName = fdbp.getFormDataContentDisposition().getFileName();
        try {
            Storage store = getStorage();
            store.storeFile(Storage.RESOURCE.TASKS, id, fdbp.getValueAs(InputStream.class), fName);
            EntityTransaction et = em.getTransaction();
            try {
                et.begin();
                task.updateInputFileStatus(fName, TaskFile.FILESTATUS.READY);
                et.commit();
            } catch (RuntimeException re) {
                if (et != null && et.isActive()) {
                    et.rollback();
                }
                log.error(re);
                log.error("Impossible to update the task");
                throw new InternalServerErrorException("Errore to update " + "the task");
            } finally {
                em.close();
            }
        } catch (IOException ex) {
            log.error(ex);
            throw new InternalServerErrorException("Errore to store input " + "files");
        }
    }
    em.close();
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserSkillRepositoryImplementation.java

@Override
public UserSkill getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    UserSkill userSkill;/*from w w  w  .ja  v a  2s.com*/
    try {
        em.getTransaction().begin();
        userSkill = em.find(UserSkill.class, id);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return userSkill;
}

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

/**
 * Removes the task. Task is deleted and all the associated activities and
 * or files removed./*www. j a  v a 2  s  . c  o m*/
 *
 * @param id Id of the task to remove
 */
@DELETE
public final void deleteTask(@PathParam("id") final String id) {
    Task task;
    EntityManager em = getEntityManager();
    try {
        task = em.find(Task.class, id);
        if (task == null) {
            throw new NotFoundException();
        }
        EntityTransaction et = em.getTransaction();
        try {
            et.begin();
            em.remove(task);
            et.commit();
        } catch (RuntimeException re) {
            if (et != null && et.isActive()) {
                et.rollback();
            }
            log.error(re);
            log.error("Impossible to remove the task");
            em.close();
            throw new InternalServerErrorException("Errore to remove " + "the task " + id);
        }
        try {
            Storage store = getStorage();
            store.removeAllFiles(Storage.RESOURCE.TASKS, id);
        } catch (IOException ex) {
            log.error("Impossible to remove the directory associated with " + "the task " + id);
        }
    } catch (IllegalArgumentException re) {
        log.error("Impossible to retrieve the task list");
        log.error(re);
        throw new BadRequestException("Task '" + id + "' has a problem!");
    } finally {
        em.close();
    }
}

From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

/**
 * Find by simple id./*from   w  w  w.  ja  v  a 2  s .  c  om*/
 * 
 * @param dbAccess
 *            the db access
 * @param id
 *            the id
 * @param target
 *            the target
 * @return the entity
 * @since 28/08/2014, 11:20:27 PM
 */
@Override
public Entity findBySimpleId(EntityManager dbAccess, Number id, Class<Entity> target) {
    return dbAccess.find(target, id);
}

From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

/**
 * Find by complex id./*from   w  w w .  j ava 2  s . c o  m*/
 * 
 * @param dbAccess
 *            the db access
 * @param id
 *            the id
 * @param target
 *            the target
 * @return the entity
 * @since 28/08/2014, 11:20:27 PM
 */
@Override
public Entity findByComplexId(EntityManager dbAccess, ComplexId id, Class<Entity> target) {
    return dbAccess.find(target, id);
}