Example usage for javax.persistence EntityManager createNamedQuery

List of usage examples for javax.persistence EntityManager createNamedQuery

Introduction

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

Prototype

public Query createNamedQuery(String name);

Source Link

Document

Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).

Usage

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public List<PendingInstanceBean> getAllInstances() {
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_ALL_PENDING_INSTANCES);
    List result = q.getResultList();

    try {/*ww w .  j  a v  a 2 s.  c om*/
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
    } finally {
        entityManager.close();
    }
    return result;
}

From source file:org.apache.james.mailbox.jpa.mail.JPAModSeqProvider.java

@Override
public long highestModSeq(MailboxSession session, Mailbox mailbox) throws MailboxException {
    EntityManager manager = null;
    try {/*w  w w . j ava 2  s .  c o m*/
        manager = factory.createEntityManager();
        manager.getTransaction().begin();
        JPAId mailboxId = (JPAId) mailbox.getMailboxId();
        long highest = (Long) manager.createNamedQuery("findHighestModSeq")
                .setParameter("idParam", mailboxId.getRawId()).getSingleResult();
        manager.getTransaction().commit();
        return highest;
    } catch (PersistenceException e) {
        if (manager != null && manager.getTransaction().isActive()) {
            manager.getTransaction().rollback();
        }
        throw new MailboxException("Unable to get highest mod-sequence for mailbox " + mailbox, e);
    } finally {
        if (manager != null) {
            manager.close();
        }
    }
}

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

@Override
public Picture loadFullPictureById(final long id) {
    Assert.notNull(id, "Picture Id should be supplied !");

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

        @Override/* w  ww  . j  ava  2 s.  c  o  m*/
        @SuppressWarnings("unchecked")
        protected Picture executeWithEntityManager(final EntityManager em) throws PersistenceException {
            final Query findByIdQuery = em.createNamedQuery(Picture.LOAD_FULL_PICTURE_BY_ID);
            findByIdQuery.setParameter("id", id);

            final Picture picture = Iterables.getFirst(findByIdQuery.getResultList(), null);
            return picture;
        }
    };

    return emCallback.getReturnedValue();
}

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

@Override
public Long findPictureIdByHash(final String hash) {
    Assert.hasText(hash, "Picture hash should be supplied !");

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

        @Override/* www.j  a  v  a  2s.co  m*/
        @SuppressWarnings("unchecked")
        protected Long executeWithEntityManager(final EntityManager em) throws PersistenceException {
            final Query findByHashQuery = em.createNamedQuery(Picture.FIND_PICTURE_ID_BY_HASH);
            findByHashQuery.setParameter("hash", hash);

            final Long pictureId = Iterables.getFirst(findByHashQuery.getResultList(), null);
            return pictureId;
        }
    };

    return emCallback.getReturnedValue();
}

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

@Override
@SuppressWarnings("unchecked")
public List<Picture> findAllPictures(final Long since) {
    final Timestamp sinceTimstamp = new Timestamp((since != null) ? since : 0L);

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

        @Override/*from w  w  w.j a  va  2  s .  c o m*/
        protected List<Picture> executeWithEntityManager(final EntityManager em) throws PersistenceException {
            final Query findAllQuery = em.createNamedQuery(Picture.FIND_ALL_PICTURES_ORDER_BY_DATE);
            findAllQuery.setParameter("since", sinceTimstamp);
            findAllQuery.setMaxResults(DbPictureDao.PAGINATION_SIZE);

            return findAllQuery.getResultList();
        }
    };

    List<Picture> pictures = emCallback.getReturnedValue();

    if (pictures == null) {
        pictures = Collections.emptyList();
    }

    return pictures;
}

From source file:io.symcpe.hendrix.api.ApplicationManager.java

public void init(AppConfig appConfiguration) {
    config = new Properties(System.getProperties());
    if (System.getenv(PROP_CONFIG_FILE) != null) {
        try {//from   w w w. j a v  a  2 s  .  c  om
            config.load(new FileInputStream(System.getenv(PROP_CONFIG_FILE)));
        } catch (IOException e) {
            throw new RuntimeException("Configuration file not loaded", e);
        }
    } else if (System.getProperty(PROP_CONFIG_FILE) != null) {
        try {
            config.load(new FileInputStream(System.getProperty(PROP_CONFIG_FILE)));
        } catch (IOException e) {
            throw new RuntimeException("Configuration file not loaded", e);
        }
    } else {
        try {
            config.load(
                    ApplicationManager.class.getClassLoader().getResourceAsStream("default-config.properties"));
        } catch (IOException e) {
            throw new RuntimeException("Default configuration file not loaded", e);
        }
    }
    try {
        Utils.createDatabase(config.getProperty(JAVAX_PERSISTENCE_JDBC_URL),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_DB, "hendrix"),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_USER),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_PASSWORD),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_DRIVER));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    config.setProperty(JAVAX_PERSISTENCE_JDBC_URL, config.getProperty(JAVAX_PERSISTENCE_JDBC_URL)
            + config.getProperty(JAVAX_PERSISTENCE_JDBC_DB, "hendrix"));
    factory = Persistence.createEntityManagerFactory("hendrix", config);
    EntityManager em = factory.createEntityManager();
    System.out.println("Rules stats" + em.createNamedQuery("Rules.stats").getResultList());
    em.close();
    if (!LOCAL) {
        initKafkaConnection();
    }
}

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

/**
 * Test if a picture is already presents in DB.
 * /*from   www.j av a2  s. c  om*/
 * @param picture
 * @param em
 * @throws PictureAlreadyExistsException
 */
protected void testHashUniqueness(final Picture picture, final EntityManager em)
        throws PictureAlreadyExistsException {
    final Query findPicByHashQuery = em.createNamedQuery(Picture.FIND_PICTURE_ID_BY_HASH);
    findPicByHashQuery.setParameter("hash", picture.getOriginalHash());
    final List<?> results = findPicByHashQuery.getResultList();
    if (!results.isEmpty()) {
        // A picture with same hash was found
        throw new PictureAlreadyExistsException(picture.getFilename());
    }
}

From source file:uk.ac.edukapp.service.UserAccountService.java

public Message removeFavourite(Useraccount user, Widgetprofile favourite) {
    EntityManager em = this.getEntityManagerFactory().createEntityManager();
    em.getTransaction().begin();/*from w  w w  .  ja  v a  2  s.  c  o m*/
    Query q = em.createNamedQuery("favourite.select");
    q.setParameter("user", user);
    q.setParameter("widgetprofile", favourite);
    WidgetFavourite fav = (WidgetFavourite) q.getSingleResult();
    Message msg = new Message();
    if (fav != null) {
        em.remove(fav);
        msg.setMessage("OK");
    } else {
        msg.setMessage("Favourite could not be removed");
    }
    em.getTransaction().commit();
    em.close();
    return msg;
}

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

@Override
public List<Picture> findPicturesByAlbumId(final Long albumId, final Long since) {
    Assert.notNull(albumId, "Album Id should be supplied !");
    final Timestamp sinceTimstamp = new Timestamp((since != null) ? since : 0L);

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

        @Override//from ww  w .  j a va 2  s.c om
        @SuppressWarnings("unchecked")
        protected List<Picture> executeWithEntityManager(final EntityManager em) throws PersistenceException {
            final Query findByAlbumQuery = em.createNamedQuery(Picture.FIND_PICTURE_BY_ALBUM_ORDER_BY_DATE);
            findByAlbumQuery.setParameter("albumId", albumId);
            findByAlbumQuery.setParameter("since", sinceTimstamp);
            findByAlbumQuery.setMaxResults(DbPictureDao.PAGINATION_SIZE);

            final List<Picture> pictureId = findByAlbumQuery.getResultList();
            return pictureId;
        }
    };

    List<Picture> pictures = emCallback.getReturnedValue();
    if (pictures == null) {
        pictures = Collections.emptyList();
    }

    return pictures;
}

From source file:com.easyjf.core.dao.impl.GenericDAOImpl.java

public List executeNativeNamedQuery(final String nnq) {
    Object ret = this.getJpaTemplate().execute(new JpaCallback() {

        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createNamedQuery(nnq);
            return query.getResultList();
        }/*from  ww w.  j av a  2s . c  o  m*/
    });
    return (List) ret;
}