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:org.traccar.web.server.model.DataServiceImpl.java

public List<Position> getPositions(Device device, int hoursAgo) {
    EntityManager entityManager = getSessionEntityManager();
    synchronized (entityManager) {
        List<Position> positions = new LinkedList<Position>();
        TypedQuery<Position> query = null;
        if (hoursAgo > -1) {
            long nowMs = new Date().getTime();
            Date time = new Date();
            time.setTime(nowMs - hoursAgo * 60 * 60 * 1000);
            query = entityManager.createQuery(
                    "SELECT x FROM Position x WHERE x.device = :device AND x.time > :time ORDER BY x.time ASC",
                    Position.class);
            query.setParameter("time", time);
        } else {//  w  ww.  ja  va2 s .  c  o  m
            query = entityManager.createQuery(
                    "SELECT x FROM Position x WHERE x.device = :device ORDER BY x.time ASC", Position.class);
        }

        query.setParameter("device", device);
        positions.addAll(query.getResultList());
        return positions;
    }
}

From source file:info.dolezel.jarss.rest.v1.FeedsService.java

@GET
@Produces(MediaType.APPLICATION_JSON)//from  ww  w  .  j  av a2  s  . c o m
@Path("{id}/headlines")
public Response getArticleHeadlines(@Context SecurityContext context, @PathParam("id") int feedId,
        @QueryParam("skip") int skip, @QueryParam("limit") int limit) {
    EntityManager em;
    List<Object[]> articles;
    Query query;
    ArticleHeadlineData[] result;
    Feed feed;
    User user;

    user = (User) context.getUserPrincipal();
    em = HibernateUtil.getEntityManager();

    try {

        feed = em.find(Feed.class, feedId);
        if (feed == null) {
            return Response.status(Response.Status.NOT_FOUND)
                    .entity(new ErrorDescription("Feed does not exist")).build();
        }
        if (!feed.getUser().equals(user)) {
            return Response.status(Response.Status.FORBIDDEN)
                    .entity(new ErrorDescription("Feed not owned by user")).build();
        }

        query = em.createQuery(
                "SELECT fid, fi from FeedItemData fid LEFT OUTER JOIN fid.feedItems AS fi where fid.feedData = :fd and (fi is null or fi.feed = :feed) order by fid.date desc",
                Object[].class).setParameter("fd", feed.getData()).setParameter("feed", feed)
                .setFirstResult(skip);

        if (limit > 0)
            query.setMaxResults(limit);

        articles = query.getResultList();

        result = new ArticleHeadlineData[articles.size()];
        for (int i = 0; i < articles.size(); i++) {
            FeedItemData article = (FeedItemData) articles.get(i)[0];
            FeedItem feedItem = (FeedItem) articles.get(i)[1];

            ArticleHeadlineData data = new ArticleHeadlineData();
            String text;

            data.setPublished(article.getDate().getTime());
            data.setTitle(article.getTitle());
            data.setId(article.getId());

            text = StringUtils.html2text(article.getText());

            if (text.length() > 130)
                text = text.substring(0, 130);

            data.setExcerpt(text);
            data.setLink(article.getLink());

            if (feedItem != null) {
                data.setRead(feedItem.isRead());
                data.setStarred(feedItem.isStarred());
            }

            result[i] = data;
        }

        return Response.ok(result).build();
    } finally {
        em.close();
    }
}