Example usage for org.hibernate Session createSQLQuery

List of usage examples for org.hibernate Session createSQLQuery

Introduction

In this page you can find the example usage for org.hibernate Session createSQLQuery.

Prototype

@Override
    NativeQuery createSQLQuery(String queryString);

Source Link

Usage

From source file:com.gisgraphy.domain.repository.GenericGisDao.java

License:Open Source License

public void createGISTIndexForShapeColumn() {
    this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            session.flush();//  ww w  . j av  a 2 s . c  o  m

            logger.info("will create GIST index for " + persistentClass.getSimpleName().toLowerCase()
                    + " shape column");
            String shapeIndexName = "shapeIndex" + persistentClass.getSimpleName();
            logger.info("checking if " + shapeIndexName + " exists");
            String checkingShapeIndex = "SELECT 1 FROM   pg_class c  JOIN   pg_namespace n ON n.oid = c.relnamespace WHERE  c.relname = '"
                    + shapeIndexName + "'";
            Query checkingShapeIndexQuery = session.createSQLQuery(checkingShapeIndex);
            Object shapeIndexExists = checkingShapeIndexQuery.uniqueResult();
            if (shapeIndexExists != null) {
                logger.info("will create GIST index for  the " + OpenStreetMap.SHAPE_COLUMN_NAME + " column");
                String createIndex = "CREATE INDEX " + shapeIndexName + " ON "
                        + persistentClass.getSimpleName().toLowerCase() + " USING GIST ("
                        + GisFeature.SHAPE_COLUMN_NAME + ")";
                Query createIndexQuery = session.createSQLQuery(createIndex);
                createIndexQuery.executeUpdate();
            } else {
                logger.info("won't create GIST index for " + persistentClass.getSimpleName()
                        + " because it already exists");
            }

            return null;
        }

    });
}

From source file:com.gisgraphy.domain.repository.OpenStreetMapDao.java

License:Open Source License

public Integer updateTS_vectorColumnForStreetNameSearch() {
    return (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            session.flush();/*from  w w w.  java2  s.co m*/
            logger.info("will update " + OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME.toLowerCase()
                    + " field");
            String updateFulltextField = "UPDATE openStreetMap SET "
                    + OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME.toLowerCase()
                    + " = to_tsvector('simple',coalesce(" + OpenStreetMap.FULLTEXTSEARCH_COLUMN_NAME
                    + ",'')) where name is not null";
            Query qryUpdateFulltextField = session.createSQLQuery(updateFulltextField);
            int numberOfLineUpdatedForFulltext = qryUpdateFulltextField.executeUpdate();
            int numberOfLineUpdatedForPartial = 0;
            return Integer.valueOf(numberOfLineUpdatedForFulltext + numberOfLineUpdatedForPartial);

        }
    });
}

From source file:com.gisgraphy.domain.repository.OpenStreetMapDao.java

License:Open Source License

public Integer updateTS_vectorColumnForStreetNameSearchPaginate(final long from, final long to) {
    return (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            session.flush();/*from w  w w .  ja v a  2s . com*/
            logger.info("will update " + OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME.toLowerCase()
                    + " field");
            String updateFulltextField = "UPDATE openStreetMap SET "
                    + OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME.toLowerCase()
                    + " = to_tsvector('simple',coalesce(" + OpenStreetMap.FULLTEXTSEARCH_COLUMN_NAME
                    + ",'')) where gid >= " + from + " and gid <= " + to + " and name is not null";
            Query qryUpdateFulltextField = session.createSQLQuery(updateFulltextField);
            int numberOfLineUpdatedForFulltext = qryUpdateFulltextField.executeUpdate();
            int numberOfLineUpdatedForPartial = 0;
            return Integer.valueOf(numberOfLineUpdatedForFulltext + numberOfLineUpdatedForPartial);

        }
    });
}

From source file:com.gisgraphy.domain.repository.OpenStreetMapDao.java

License:Open Source License

public void createSpatialIndexes() {
    this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            session.flush();//from   w  ww.j a va  2 s  . co  m

            String locationIndexName = OpenStreetMap.LOCATION_COLUMN_NAME.toLowerCase() + "indexopenstreetmap";
            logger.info("checking if " + locationIndexName + " exists");
            String checkingLocationIndex = "SELECT 1 FROM   pg_class c  JOIN   pg_namespace n ON n.oid = c.relnamespace WHERE  c.relname = '"
                    + locationIndexName + "'";
            Query checkingLocationIndexQuery = session.createSQLQuery(checkingLocationIndex);
            Object locationIndexExists = checkingLocationIndexQuery.uniqueResult();
            if (locationIndexExists != null) {
                logger.info(
                        "will create GIST index for  the " + OpenStreetMap.LOCATION_COLUMN_NAME + " column");
                String createIndexForLocation = "CREATE INDEX " + locationIndexName
                        + " ON openstreetmap USING GIST (" + OpenStreetMap.LOCATION_COLUMN_NAME.toLowerCase()
                        + ")";
                Query qryUpdateLocationIndex = session.createSQLQuery(createIndexForLocation);
                qryUpdateLocationIndex.executeUpdate();
            } else {
                logger.info("won't create GIST index for  the " + OpenStreetMap.LOCATION_COLUMN_NAME
                        + " column because it already exists");
            }

            String shapeIndexName = OpenStreetMap.SHAPE_COLUMN_NAME.toLowerCase() + "indexopenstreetmap";
            logger.info("checking if " + shapeIndexName + " exists");
            String checkingShapeIndex = "SELECT 1 FROM   pg_class c  JOIN   pg_namespace n ON n.oid = c.relnamespace WHERE  c.relname = '"
                    + shapeIndexName + "'";
            Query checkingShapeIndexQuery = session.createSQLQuery(checkingShapeIndex);
            Object shapeIndexExists = checkingShapeIndexQuery.uniqueResult();
            if (shapeIndexExists != null) {
                logger.info("will create GIST index for  the " + OpenStreetMap.SHAPE_COLUMN_NAME + " column");
                String createIndexForShape = "CREATE INDEX " + OpenStreetMap.SHAPE_COLUMN_NAME.toLowerCase()
                        + "indexopenstreetmap ON openstreetmap USING GIST ("
                        + OpenStreetMap.SHAPE_COLUMN_NAME.toLowerCase() + ")";
                Query qryUpdateShapeIndex = session.createSQLQuery(createIndexForShape);
                qryUpdateShapeIndex.executeUpdate();
            } else {
                logger.info("won't create GIST index for  the " + OpenStreetMap.SHAPE_COLUMN_NAME
                        + " column because it already exists");
            }
            return null;
        }
    });
}

From source file:com.gisgraphy.domain.repository.OpenStreetMapDao.java

License:Open Source License

public void createFulltextIndexes() {
    this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            session.flush();//from   w ww .  ja  v a 2  s. com
            logger.info("will create Fulltext index");
            String createFulltextIndex = "CREATE INDEX "
                    + OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME.toLowerCase()
                    + "indexopenstreetmap ON openstreetmap USING gin("
                    + OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME.toLowerCase() + ")";
            Query fulltextIndexQuery = session.createSQLQuery(createFulltextIndex);
            fulltextIndexQuery.executeUpdate();

            return null;
        }
    });
}

From source file:com.github.cutstock.db.TransRuleDaoImpl.java

License:Apache License

@Override
public List<String> getAllCategories() {
    Session session = hibernateUtil.openSession();
    String sql = "SELECT distinct category FROM CodeNameTransRule";
    return session.createSQLQuery(sql).list();
}

From source file:com.github.javarch.persistence.orm.test.HibernateDataBaseTestBuilder.java

License:Apache License

@Override
public void executeSQL(final String sql) throws SQLException {
    Session s = source.getCurrentSession();
    s.createSQLQuery(sql).executeUpdate();
}

From source file:com.glaf.jbpm.dao.JbpmEntityDAO.java

License:Apache License

public void executeSqlUpdate(JbpmContext jbpmContext, String sql) {
    Session session = jbpmContext.getSession();
    Query query = session.createSQLQuery(sql);
    query.executeUpdate();/*  w  w  w. jav  a2  s  . co  m*/
}

From source file:com.glaf.jbpm.dao.JbpmEntityDAO.java

License:Apache License

public void executeSqlUpdate(JbpmContext jbpmContext, String sql, List<Object> values) {
    Session session = jbpmContext.getSession();
    Query query = session.createSQLQuery(sql);
    if (values != null && values.size() > 0) {
        for (int i = 0; i < values.size(); i++) {
            query.setParameter(i, values.get(i));
        }/*from   w ww .  j  av a2s . co  m*/
    }
    query.executeUpdate();
}

From source file:com.globalsight.calendar.CalendarManagerLocal.java

License:Apache License

/**
 * @see CalendarManager.removeScheduledActivities(long);
 *//* w  ww  .java  2 s  . c  om*/
public void removeScheduledActivities(long p_taskId) throws RemoteException, CalendarManagerException {
    String ownerId = "";

    Session session = HibernateUtil.getSession();

    try {
        String sql = "select r.* from RESERVED_TIME r, USER_CALENDAR u "
                + " where r.TASK_ID = :TASK_ID and r.USER_CALENDAR_ID = u.ID ";

        SQLQuery query = session.createSQLQuery(sql).addEntity(ReservedTime.class);
        query.setParameter("TASK_ID", new Long(p_taskId));

        Object[] rts = query.list().toArray();

        HashMap map = new HashMap(4);

        for (int i = 0; i < rts.length; i++) {
            ReservedTime rt = (ReservedTime) rts[i];

            Long calId = rt.getUserFluxCalendar().getIdAsLong();
            UserFluxCalendar cal = (UserFluxCalendar) map.get(calId);
            if (cal == null) {
                cal = rt.getUserFluxCalendar();
                map.put(calId, cal);
            }

            ownerId = cal.getOwnerUserId();

            cal.getCollectionByType(rt.getType()).remove(rt);

            HibernateUtil.saveOrUpdate(cal);
        }

        HibernateUtil.delete(Arrays.asList(rts));
    } catch (Exception e) {
        String[] args = { ownerId, String.valueOf(p_taskId) };
        throw new CalendarManagerException(CalendarManagerException.MSG_REMOVE_RESERVED_TIME_FAILED, args, e);
    }
}