Example usage for javax.persistence EntityManager createQuery

List of usage examples for javax.persistence EntityManager createQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createQuery.

Prototype

public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass);

Source Link

Document

Create an instance of TypedQuery for executing a Java Persistence query language statement.

Usage

From source file:nl.waisda.services.EuropeanaImportServiceTest.java

@Test
@Transactional/*  w  ww  .j  a v  a  2  s.  c om*/
@Rollback(value = true)
public void testImportAcceptMP4() {
    HttpUriRequest overview = new HttpGet(
            "http://preview.europeana.eu/api/v2/search.json?wskey=XxxsEZoWj&query=dontcare&start=1&rows=12&profile=minimal");
    HttpUriRequest details = new HttpGet("http://detail");
    try {
        setup();
        // setup http response
        setupHttpResponse("http://www.host.com/image.png", "http://www.host.com/shownby.mp4",
                "http://www.host.com/webresource.mp4");
        // call the service
        service.importEuropeanaData("dontcare");
        EntityManager em = entityManager;//Factory.getObject().createEntityManager();
        TypedQuery q = em.createQuery("select v from Video v", Video.class);
        List<Video> result = q.getResultList();
        Assert.assertEquals(1, result.size());
        Assert.assertEquals("http://www.host.com/shownby.mp4", result.get(0).getSourceUrl());
        Assert.assertEquals("http://www.host.com/image.png", result.get(0).getImageUrl());

    } catch (Exception e) {
        Assert.fail("Not supposed to fail");
        LOG.error(e);
    }
}

From source file:nl.waisda.services.EuropeanaImportServiceTest.java

@Test
@Transactional//  w  w w. j a v a 2s  . co  m
@Rollback(value = true)
public void testImportAcceptAVI() {
    HttpUriRequest overview = new HttpGet(
            "http://preview.europeana.eu/api/v2/search.json?wskey=XxxsEZoWj&query=dontcare&start=1&rows=12&profile=minimal");
    HttpUriRequest details = new HttpGet("http://detail");
    try {
        setup();
        // setup http response
        setupHttpResponse("http://www.host.com/image.png", "http://www.host.com/shownby.avi",
                "http://www.host.com/webresource.mp4");
        // call the service
        service.importEuropeanaData("dontcare");
        EntityManager em = entityManager;//Factory.getObject().createEntityManager();
        TypedQuery q = em.createQuery("select v from Video v", Video.class);
        List<Video> result = q.getResultList();
        Assert.assertEquals(1, result.size());
        Assert.assertEquals("http://www.host.com/shownby.avi", result.get(0).getSourceUrl());
        Assert.assertEquals("http://www.host.com/image.png", result.get(0).getImageUrl());

    } catch (Exception e) {
        Assert.fail("Not supposed to fail");
        LOG.error(e);
    }
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.LexiconBuilderController.java

private TypedQuery<LexiconBuilderDocument> getDocumentsQuery(EntityManager em,
        LexiconBuilderDocumentStore builder, Boolean seen) {
    TypedQuery<LexiconBuilderDocument> query = em.createQuery(
            String.format("SELECT doc FROM LexiconBuilderDocument doc " + "WHERE doc.store=:builder %s "
                    + "ORDER BY doc.weight DESC", seen != null ? " AND doc.flag=:seen" : ""),
            LexiconBuilderDocument.class);
    query.setParameter("builder", builder);
    if (seen != null) {
        query.setParameter("seen", seen);
    }/*w ww .  j  a v  a2 s . c  o  m*/

    return query;
}

From source file:edu.sabanciuniv.sentilab.sare.models.setcover.DocumentSetCover.java

@Override
public Iterable<byte[]> getDocumentIds(EntityManager em) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");

    TypedQuery<byte[]> query = em.createQuery(
            "SELECT scd.id FROM SetCoverDocument scd " + "WHERE scd.store=:sc " + "AND scd.flag=true",
            byte[].class);
    query.setParameter("sc", this);
    return query.getResultList();
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.AspectOpinionMinedCorpusController.java

/**
 * Finds the opinion mined corpus associated with a given corpus, lexicon, and engine code.
 * @param em the {@link EntityManager} to use.
 * @param corpus the {@link DocumentCorpus} being used to mine opinion.
 * @param lexicon the {@link AspectLexicon} being built.
 * @return the {@link AspectOpinionMinedCorpus} object found, if any; {@code null} otherwise.
 *//*from ww w .j av  a2  s.  c om*/
public AspectOpinionMinedCorpus findMinedCorpus(EntityManager em, DocumentCorpus corpus, AspectLexicon lexicon,
        String engineCode) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(corpus, CannedMessages.NULL_ARGUMENT, "corpus");
    Validate.notNull(lexicon, CannedMessages.NULL_ARGUMENT, "lexicon");

    TypedQuery<AspectOpinionMinedCorpus> query = em.createQuery(
            "SELECT mc FROM AspectOpinionMinedCorpus mc "
                    + "WHERE mc.baseStore=:corpus AND :lexicon MEMBER OF mc.referencedObjects",
            AspectOpinionMinedCorpus.class);
    query.setParameter("corpus", corpus).setParameter("lexicon", lexicon);

    for (AspectOpinionMinedCorpus minedCorpus : query.getResultList()) {
        if (StringUtils.equals(engineCode, minedCorpus.getEngineCode())) {
            return minedCorpus;
        }
    }

    return null;
}

From source file:org.mousephenotype.dcc.exportlibrary.exportworker.ExportWorker.java

private void processProcedureData(EntityManager rawEM, CentreContainer downloadInstructions) {
    TypedQuery<Line> lineQuery = rawEM
            .createQuery("Select l From Line l JOIN l.procedure as p where p.hjid=:hjid", Line.class);
    TypedQuery<Experiment> experimentQuery = rawEM.createQuery(
            "Select e from Experiment e JOIN e.procedure as p where p.hjid = :hjid", Experiment.class);
    TypedQuery<Housing> housingQuery = rawEM
            .createQuery("Select h from Housing h join h.procedure as p where p.hjid=:hjid", Housing.class);

    TypedQuery<CentreProcedure> lineCP = rawEM.createQuery(
            "Select cp From CentreProcedure cp JOIN cp.line as l JOIN l.procedure as p where p.hjid=:hjid",
            CentreProcedure.class);
    TypedQuery<CentreProcedure> experimentCP = rawEM.createQuery(
            "Select cp From CentreProcedure cp JOIN cp.experiment as e JOIN e.procedure as p where p.hjid=:hjid",
            CentreProcedure.class);
    TypedQuery<CentreProcedure> housingCP = rawEM.createQuery(
            "Select cp From CentreProcedure cp JOIN cp.housing as h JOIN h.procedure as p where p.hjid=:hjid",
            CentreProcedure.class);

    CentreProcedureSet cps = new CentreProcedureSet();
    int count = 1;
    int filecount = 1;
    XMLGenerator g = new XMLGenerator();
    for (DataReference d : downloadInstructions.getDataReference()) {
        switch (d.getType()) {
        case EXPERIMENT:
            Experiment e = experimentQuery.setParameter("hjid", d.getHjid()).getSingleResult();
            CentreProcedure ecp = experimentCP.setParameter("hjid", d.getHjid()).getSingleResult();
            CentreProcedure aecp = getCentreProcedure(ecp, cps, downloadInstructions.getCentreILAR());
            aecp.getExperiment().add(e);
            break;
        case LINE:
            Line l = lineQuery.setParameter("hjid", d.getHjid()).getSingleResult();
            CentreProcedure lcp = lineCP.setParameter("hjid", d.getHjid()).getSingleResult();
            CentreProcedure alcp = getCentreProcedure(lcp, cps, downloadInstructions.getCentreILAR());
            alcp.getLine().add(l);/*w ww  . ja  v a2s  .com*/
            break;
        case HOUSING:
            Housing h = housingQuery.setParameter("hjid", d.getHjid()).getSingleResult();
            CentreProcedure hcp = housingCP.setParameter("hjid", d.getHjid()).getSingleResult();
            CentreProcedure ahcp = getCentreProcedure(hcp, cps, downloadInstructions.getCentreILAR());
            ahcp.getHousing().add(h);
            break;
        default:
            logger.info("Don't know thich type this is");
            break;
        }
        if (count % MAX_OBJECTS == 0) {
            logger.info("Writing file containing {} procedures", MAX_OBJECTS);
            writeCentreProcedure(cps, g, filecount, downloadInstructions);
            filecount++;
        }
        count++;
    }
    // Write the final CPS
    writeCentreProcedure(cps, g, filecount, downloadInstructions);
}

From source file:cz.fi.muni.pa165.dto.BookDAOTest.java

@Test
public void testDelete() {
    EntityManager em = emf.createEntityManager();
    BookDAOImpl bdao = new BookDAOImpl();
    bdao.setManager(em);//from w  ww  .j a  v  a2  s. co  m
    Book b = new Book();
    b.setIdBook(1);
    em.getTransaction().begin();
    b = bdao.find(b);
    bdao.delete(b);
    em.getTransaction().commit();

    List<Book> books = em.createQuery("SELECT b FROM Book b", Book.class).getResultList();
    em.close();
    assertEquals(books.size(), 0);
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.LexiconBuilderController.java

/**
 * Gets the previously seen tokens for the given {@link LexiconBuilderDocumentStore}.
 * @param em the {@link EntityManager} to use.
 * @param builder the identifier of the {@link LexiconBuilderDocumentStore} to use.
 * @return the {@link List} of {@link LexiconDocument} objects.
 *///from ww w  . j  a  va2  s . c  om
public List<LexiconDocument> getSeenTokens(EntityManager em, LexiconBuilderDocumentStore builder) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(builder, CannedMessages.NULL_ARGUMENT, "builder");

    TypedQuery<LexiconDocument> query = em
            .createQuery("SELECT doc FROM LexiconDocument doc WHERE doc.store=:builder", LexiconDocument.class);
    query.setParameter("builder", builder);

    return query.getResultList();
}

From source file:org.mousephenotype.dcc.exportlibrary.exportworker.ExportWorker.java

private void processSpecimens(EntityManager rawEM, CentreContainer downloadInstructions) {
    XMLGenerator g = new XMLGenerator();

    TypedQuery<Mouse> mouseQuery = rawEM.createQuery("Select s from Mouse s where s.hjid=:hjid", Mouse.class);
    TypedQuery<Embryo> embryoQuery = rawEM.createQuery("Select s from Embryo s where s.hjid=:hjid",
            Embryo.class);

    CentreSpecimenSet css = new CentreSpecimenSet();
    CentreSpecimen cs = new CentreSpecimen();
    cs.setCentreID(downloadInstructions.getCentreILAR());
    // This is just faff
    ArrayList<CentreSpecimen> csl = new ArrayList<CentreSpecimen>();
    csl.add(cs);/* w  w  w. j ava  2 s.co m*/
    css.setCentre(csl);

    int count = 1;
    int filecount = 1;
    ArrayList<Specimen> specimenList = new ArrayList<Specimen>();
    logger.info("There are {} specimens", downloadInstructions.getSpecimenReference().size());
    for (SpecimenReference sp : downloadInstructions.getSpecimenReference()) {
        if (count % MAX_OBJECTS == 0 || count == downloadInstructions.getDataReference().size()) {
            cs.setMouseOrEmbryo(specimenList);
            writeSpecimenSet(css, cs, g, filecount, downloadInstructions);
            filecount++;
            cs.unsetMouseOrEmbryo(); // empty list
            specimenList.clear();
        }
        if (sp.isIsMouse()) {
            Mouse s = mouseQuery.setParameter("hjid", sp.getSpecimenID()).getSingleResult();
            specimenList.add(s);
        } else {
            Embryo s = embryoQuery.setParameter("hjid", sp.getSpecimenID()).getSingleResult();
            specimenList.add(s);
        }
        count++;
    }
    // Write the remaining specimen to file.
    logger.info("There are {} specimens exported", count);
    cs.setMouseOrEmbryo(specimenList);
    writeSpecimenSet(css, cs, g, filecount, downloadInstructions);
}

From source file:com.enioka.jqm.tools.JobManagerHandler.java

JobManagerHandler(JobInstance ji, Map<String, String> prms) {
    p = new Properties();
    p.put("emf", Helpers.getEmf());

    EntityManager em = Helpers.getNewEm();
    this.ji = em.find(JobInstance.class, ji.getId());
    params = prms;/*from www .ja v a  2s. co  m*/

    defaultCon = em.createQuery("SELECT gp.value FROM GlobalParameter gp WHERE gp.key = 'defaultConnection'",
            String.class).getSingleResult();

    this.jd = this.ji.getJd();
    this.application = this.jd.getApplication();
    this.sessionId = this.ji.getSessionID();
    this.node = this.ji.getNode();
    this.node.getDlRepo();
    em.close();
}