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:com.epam.training.taranovski.web.project.repository.implementation.VacancySkillRepositoryImplementation.java

@Override
public VacancySkill getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    VacancySkill vacancySkill;//  www.j  a  v a 2 s.c  o m
    try {
        em.getTransaction().begin();
        vacancySkill = em.find(VacancySkill.class, id);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return vacancySkill;
}

From source file:org.dragoneronca.nlp.wol.domain.WolDomainContext.java

public void updateScore(int id, double score) {
    WolDomainContext domainContext = WolDomainContext.getInstance();
    EntityManager entityManager = domainContext.getEntityManager();

    SemanticEdge edge = entityManager.find(SemanticEdge.class, id);
    if (edge == null || edge instanceof TermSemanticEdge) {
        Query q = entityManager.createNamedQuery("updateScoreTermSemanticEdge");
        q.setParameter(1, score).setParameter(2, id);

        q.executeUpdate();//from   w  w  w . j a  v  a  2  s  . c  om
    } else {
        Query q = entityManager.createNamedQuery("updateScoreSemanticEdge");
        q.setParameter(1, score).setParameter(2, id);

        q.executeUpdate();
    }
}

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

@Override
public BasicSkill getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    BasicSkill skill = null;// w  ww  .  j  a v  a  2 s  .com
    try {
        em.getTransaction().begin();
        skill = em.find(BasicSkill.class, id);
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return skill;
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

public DataSource getDataSource(int id) {
    EntityManager em = getEntityManager();
    try {//from  ww  w.j a v a2s.  c  o  m
        return em.find(DataSource.class, id);
    } finally {
        em.close();
    }
}

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

public User getUser(EntityManager em) {
    try {/* w w w .ja  v a  2  s .c om*/
        return (User) em.find(User.class, getUserId());
    } catch (Exception e) {
        return null;
    }
}

From source file:org.apache.juddi.v3.auth.JUDDIAuthenticator.java

public UddiEntityPublisher identify(String authInfo, String authorizedName) throws AuthenticationException {
    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {//from w w w.  ja v  a  2  s. c  o  m
        tx.begin();
        Publisher publisher = em.find(Publisher.class, authorizedName);
        if (publisher == null) {
            throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
        }

        return publisher;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:nl.b3p.viewer.search.SolrSearchClient.java

private String getConfigurationNaam(Integer id) {
    Long longValue = new Long(id);
    if (!configMap.containsKey(longValue)) {
        EntityManager em = Stripersist.getEntityManager();
        SolrConf configuration = em.find(SolrConf.class, longValue);
        configMap.put(longValue, configuration);
        return configuration.getName();
    }//from w ww .  ja  v a  2 s.  c  o m
    return configMap.get(longValue).getName();
}

From source file:com.sun.socialsite.business.impl.JPAPersistenceStrategy.java

/**
 * Retrieve object, no transaction needed.
 * @param clazz the class of object to retrieve
 * @param id the id of the object to retrieve
 * @return the object retrieved/*w  w  w. j  a v  a  2s.  co  m*/
 * @throws SocialSiteException on any error retrieving object
 */
public Object load(Class<?> clazz, String id) throws SocialSiteException {
    EntityManager em = getEntityManager(false);
    return em.find(clazz, id);
}

From source file:com.sun.socialsite.business.impl.JPAPersistenceStrategy.java

/**
 * Retrieve object, no transaction needed.
 * @param clazz the class of object to retrieve
 * @param id the id of the object to retrieve
 * @return the object retrieved/*  w  ww  .  j a va2 s  .  com*/
 * @throws SocialSiteException on any error retrieving object
 */
public Object load(Class<?> clazz, int id) throws SocialSiteException {
    EntityManager em = getEntityManager(false);
    return em.find(clazz, id);
}

From source file:org.silverpeas.core.persistence.datasource.model.jpa.AbstractJpaEntity.java

@Override
public boolean isPersisted() {
    if (this.id != null) {
        EntityManager entityManager = EntityManagerProvider.get().getEntityManager();
        return entityManager.find(getClass(), id) != null;
    }//  w  w  w. ja  v  a  2 s. c om
    return false;
}