Example usage for javax.persistence Query unwrap

List of usage examples for javax.persistence Query unwrap

Introduction

In this page you can find the example usage for javax.persistence Query unwrap.

Prototype

<T> T unwrap(Class<T> cls);

Source Link

Document

Return an object of the specified type to allow access to the provider-specific API.

Usage

From source file:gemlite.core.internal.support.jpa.files.dao.GmBatchDaoImpl.java

@Override
public List<Map> queryStepNamesForJob(String jobName) {
    String sql = "SELECT E.JOB_EXECUTION_ID, E.START_TIME, E.END_TIME, E.STATUS, E.EXIT_CODE, E.EXIT_MESSAGE, E.CREATE_TIME, E.LAST_UPDATED, E.VERSION, I.JOB_INSTANCE_ID, I.JOB_NAME"
            + " FROM BATCH_JOB_EXECUTION E, BATCH_JOB_INSTANCE I WHERE E.JOB_INSTANCE_ID=I.JOB_INSTANCE_ID and I.JOB_NAME=? ORDER BY JOB_EXECUTION_ID DESC";

    Query query = em.createNativeQuery(sql);
    SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
    nativeQuery.setParameter(0, jobName);
    nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
    List<Map> list = nativeQuery.list();

    return toLowerCaseKey(list);
}

From source file:gemlite.core.internal.support.jpa.files.dao.GmBatchDaoImpl.java

@Override
public Map queryJobExecutionById(Long executionId) {
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT a.*,b.job_name");
    sb.append(//from  w  ww .j ava 2 s  .com
            " from BATCH_JOB_EXECUTION a left join batch_job_instance b on a.job_instance_id = b.job_instance_id where a.JOB_EXECUTION_ID = ?");

    Query query = em.createNativeQuery(sb.toString());
    SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
    nativeQuery.setParameter(0, executionId);
    nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
    List<Map> list = nativeQuery.list();

    return (list == null || list.isEmpty()) ? null : toLowerCaseKey(list).get(0);
}

From source file:gemlite.core.internal.support.jpa.files.dao.GmBatchDaoImpl.java

@Override
public List<Map> queryStepExecutionsById(Long jobExecutionId) {
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT STEP_EXECUTION_ID, STEP_NAME, START_TIME, END_TIME, STATUS, COMMIT_COUNT,");
    sb.append(" READ_COUNT, FILTER_COUNT, WRITE_COUNT, EXIT_CODE, EXIT_MESSAGE, READ_SKIP_COUNT,");
    sb.append(" WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, ROLLBACK_COUNT, LAST_UPDATED, VERSION from ");
    sb.append(" BATCH_STEP_EXECUTION where JOB_EXECUTION_ID = ?");
    sb.append(" order by STEP_EXECUTION_ID");

    Query query = em.createNativeQuery(sb.toString());
    SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
    nativeQuery.setParameter(0, jobExecutionId);
    nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

    List<Map> list = nativeQuery.list();
    return toLowerCaseKey(list);
}

From source file:gemlite.core.internal.support.jpa.files.dao.GmBatchDaoImpl.java

@Override
public List<Map> queryJobExecutions(String status) {
    String where = "";
    if (!StringUtils.isEmpty(status)) {
        where = " where e.status='" + status + "'";
    }//from ww w .  j a v a  2s.c om
    String getjobs = "select e.job_execution_id as job_execution_id, e.start_time as start_time, e.end_time, e.status, e.exit_code, e.exit_message, e.create_time, e.last_updated, e.version, i.job_instance_id, i.job_name from batch_job_execution e join batch_job_instance i on e.job_instance_id=i.job_instance_id "
            + where + " order by job_execution_id desc";
    Query query = em.createNativeQuery(getjobs);
    SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
    nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
    List<Map> list = null;
    try {
        list = nativeQuery.list();
    } catch (Exception e) {
        LogUtil.getCoreLog().warn("SQL {} Error {}", getjobs, e.getMessage());
    }
    //?
    List<Map> newlist = new ArrayList<Map>();
    if (list != null) {
        for (Map map : list) {
            //duration
            Timestamp t1 = (Timestamp) map.get("START_TIME");
            Timestamp t2 = (Timestamp) map.get("END_TIME");
            String duration = "";
            if (t2 != null)
                duration = format(new Date(t2.getTime() - t1.getTime()), "HH:mm:ss");

            Map newmap = new HashMap<String, Object>();
            newmap.put("duration", duration);

            Iterator<Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> entry = it.next();
                newmap.put(entry.getKey().toLowerCase(), entry.getValue());
            }
            newlist.add(newmap);
        }
    }

    return newlist;
}

From source file:org.artificer.repository.hibernate.HibernatePersistenceManager.java

@Override
public List<StoredQuery> getStoredQueries() throws ArtificerException {
    return new HibernateUtil.HibernateTask<List<StoredQuery>>() {
        @Override//from   ww  w .j  a v a  2 s  .  c  o  m
        protected List<StoredQuery> doExecute(EntityManager entityManager) throws Exception {
            Query q = entityManager.createQuery("FROM ArtificerStoredQuery asq ORDER BY asq.queryName ASC");
            q.unwrap(org.hibernate.Query.class).setCacheable(true);
            List<ArtificerStoredQuery> storedQueries = q.getResultList();
            return HibernateEntityFactory.storedQueries(storedQueries);
        }
    }.execute();
}

From source file:org.artificer.repository.hibernate.HibernateUtil.java

public static ArtificerArtifact getArtifact(String uuid, EntityManager entityManager, boolean fullFetch)
        throws ArtificerException {
    Query q = entityManager.createQuery("FROM ArtificerArtifact a WHERE a.trashed = false AND a.uuid = :uuid");
    q.setParameter("uuid", uuid);
    q.unwrap(org.hibernate.Query.class).setCacheable(true);
    ArtificerArtifact artifact;//from  w  ww .  j a va 2 s. c o m
    try {
        artifact = (ArtificerArtifact) q.getSingleResult();
    } catch (NoResultException e) {
        throw ArtificerNotFoundException.artifactNotFound(uuid);
    }

    if (fullFetch) {
        Hibernate.initialize(artifact.getClassifiers());
        Hibernate.initialize(artifact.getComments());
        Hibernate.initialize(artifact.getNormalizedClassifiers());
        Hibernate.initialize(artifact.getRelationships());
    }

    return artifact;
}

From source file:org.artificer.repository.hibernate.HibernateUtil.java

public static ArtificerOntology getOntology(String uuid, EntityManager entityManager)
        throws ArtificerException {
    Query q = entityManager.createQuery("FROM ArtificerOntology a WHERE a.uuid = :uuid");
    q.setParameter("uuid", uuid);
    q.unwrap(org.hibernate.Query.class).setCacheable(true);
    ArtificerOntology ontology;/*  w  ww . j  a  v  a2 s  .  co m*/
    try {
        ontology = (ArtificerOntology) q.getSingleResult();
    } catch (NoResultException e) {
        throw ArtificerNotFoundException.ontologyNotFound(uuid);
    }
    Hibernate.initialize(ontology.getRootClasses());
    return ontology;
}

From source file:org.hoteia.qalingo.core.dao.RetailerDao.java

public List<GeolocatedStore> findB2CStoresByGeoloc(final String countryCode, final Long productBrandId,
        final List<String> types, final String latitude, final String longitude, final String distance,
        int maxResults, Object... params) {
    if (StringUtils.isNotEmpty(latitude) && StringUtils.isNotEmpty(longitude)) {
        Float latitudeFloat = new Float(latitude);
        Float longitudeFloat = new Float(longitude);
        StringBuilder queryString = buildSQLStoresByGeoloc(countryCode, productBrandId, types, latitude,
                longitude, distance, maxResults, params);
        queryString.append("AND is_b2c = :b2c ");
        if (distance != null) {
            queryString.append("HAVING distance <= :distanceValue ");
        } else {//from   w  ww  .j  av  a  2s .c om
            queryString.append("HAVING distance IS NOT null ");
        }
        queryString.append("ORDER BY distance ASC");

        Query query = createNativeQuery(queryString.toString());
        query.setParameter("latitude", latitudeFloat);
        query.setParameter("longitude", longitudeFloat);
        if (distance != null) {
            query.setParameter("distanceValue", distance);
        }
        query.setParameter("b2c", true);
        if (StringUtils.isNotEmpty(countryCode)) {
            query.setParameter("countryCode", countryCode);
        }
        if (productBrandId != null) {
            query.setParameter("productBrandId", productBrandId);
        }
        if (types != null && !types.isEmpty()) {
            int count = 1;
            for (String type : types) {
                query.setParameter("type" + count, "%" + type + "%");
                count++;
            }
        }
        query.setParameter("active", true);
        query.setMaxResults(maxResults);
        query.unwrap(SQLQuery.class).addScalar("id", LongType.INSTANCE).addScalar("code", StringType.INSTANCE)
                .addScalar("distance", DoubleType.INSTANCE);

        @SuppressWarnings("unchecked")
        List<Object[]> objects = query.getResultList();
        List<GeolocatedStore> stores = new ArrayList<GeolocatedStore>();
        for (Object[] object : objects) {
            GeolocatedStore geolocatedStore = new GeolocatedStore();
            geolocatedStore.setId((Long) object[0]);
            geolocatedStore.setCode((String) object[1]);
            geolocatedStore.setDistance((Double) object[2]);
            stores.add(geolocatedStore);
        }
        return stores;
    }
    return null;
}

From source file:org.hoteia.qalingo.core.dao.RetailerDao.java

public List<GeolocatedStore> findB2BStoresByGeoloc(final String countryCode, final Long productBrandId,
        final List<String> types, final String latitude, final String longitude, final String distance,
        int maxResults, Object... params) {
    if (StringUtils.isNotEmpty(latitude) && StringUtils.isNotEmpty(longitude)) {
        Float latitudeFloat = new Float(latitude);
        Float longitudeFloat = new Float(longitude);
        StringBuilder queryString = buildSQLStoresByGeoloc(countryCode, productBrandId, types, latitude,
                longitude, distance, maxResults, params);
        queryString.append("AND is_b2b = :b2b ");

        if (distance != null) {
            queryString.append("HAVING distance <= :distanceValue ");
        } else {//ww w. j  av a 2  s .  c  o m
            queryString.append("HAVING distance IS NOT null ");
        }
        queryString.append("ORDER BY distance ASC");

        Query query = createNativeQuery(queryString.toString());
        query.setParameter("latitude", latitudeFloat);
        query.setParameter("longitude", longitudeFloat);
        query.setParameter("countryCode", countryCode);
        if (distance != null) {
            query.setParameter("distanceValue", distance);
        }
        query.setParameter("b2b", true);
        if (StringUtils.isNotEmpty(countryCode)) {
            query.setParameter("countryCode", countryCode);
        }
        if (productBrandId != null) {
            query.setParameter("productBrandId", productBrandId);
        }
        if (types != null && !types.isEmpty()) {
            int count = 1;
            for (String type : types) {
                query.setParameter("type" + count, "%" + type + "%");
                count++;
            }
        }
        query.setParameter("active", true);
        query.setMaxResults(maxResults);
        query.unwrap(SQLQuery.class).addScalar("id", LongType.INSTANCE).addScalar("code", StringType.INSTANCE)
                .addScalar("distance", DoubleType.INSTANCE);

        @SuppressWarnings("unchecked")
        List<Object[]> objects = query.getResultList();
        List<GeolocatedStore> stores = new ArrayList<GeolocatedStore>();
        for (java.lang.Object[] object : objects) {
            GeolocatedStore geolocatedStore = new GeolocatedStore();
            geolocatedStore.setId((Long) object[0]);
            geolocatedStore.setCode((String) object[1]);
            geolocatedStore.setDistance((Double) object[2]);
            stores.add(geolocatedStore);
        }
        return stores;
    }
    return null;
}

From source file:org.jnap.core.persistence.jpa.QueryPagingSetup.java

/**
 * Gets the query string representation.
 * TODO: remove underlying persistence provider (Hibernate) dependency.
 * //from w w  w  . j a  va  2  s . c o  m
 * @param query The Query object
 * @return the string representation of the Query
 */
private String getQueryString(Query query) {
    return query.unwrap(org.hibernate.Query.class).getQueryString();
}