Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

In this page you can find the example usage for org.hibernate Query setParameter.

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:ch.eggbacon.app.service.PreisServiceImpl.java

License:Open Source License

@Override
public List<Preis> getPreisByLeistung(Leistung l) {
    Query q = getSession().createQuery("FROM " + TABLE_NAME + " WHERE LeistungID = :leistungid");
    q.setParameter("leistungid", +l.getLeistungId());
    return q.list();
}

From source file:ch.tatool.app.service.impl.ModuleDAO.java

License:Open Source License

/**
 * Get info objects of all available module instances for an account.
 * //from  www.j a  v a  2 s. co  m
 * @param account
 * @return
 */
@SuppressWarnings("unchecked")
public Set<Module.Info> getModules(final UserAccountImpl account) {
    return (Set<Module.Info>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            // query the names and ids of all modules inside an account
            Query query = session.createQuery(
                    "select module.id, module.name FROM ModuleImpl as module where module.accountId = :accountId");
            query.setParameter("accountId", account.getId());
            List<Object[]> results = (List<Object[]>) query.list();
            Set<Module.Info> infos = new TreeSet<Module.Info>();
            for (Object[] result : results) {
                ModuleInfoImpl info = new ModuleInfoImpl();
                info.setAccount(account);
                info.setId((Long) result[0]);
                info.setName((String) result[1]);
                infos.add(info);
            }
            return infos;
        }
    });
}

From source file:ch.tatool.app.service.impl.ModuleSessionDAO.java

License:Open Source License

/** Get a list of all sessions. */
@SuppressWarnings("unchecked")
public List<ModuleSession> getSessions(final ModuleImpl module) {
    return (List<ModuleSession>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            String queryString = "select session from ModuleSessionImpl session where session.module = :module order by session.id";
            Query query = session.createQuery(queryString);
            query.setParameter("module", module);
            List<ModuleSession> result = (List<ModuleSession>) query.list();
            // make sure we property set the module object
            for (ModuleSession s : result) {
                s.setModule(module);/*from  www.java 2 s  .  c  o  m*/
            }
            return result;
        }
    });
}

From source file:ch.tatool.app.service.impl.ModuleSessionDAO.java

License:Open Source License

private Integer findLastSessionIndex(final ModuleImpl module) {
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            String queryString = "select max(session.index) from ModuleSessionImpl session where session.module = :module";
            Query query = session.createQuery(queryString);
            query.setParameter("module", module);
            Integer result = (Integer) query.uniqueResult();

            if (result != null) {
                return result;
            } else {
                return 0;
            }//from w  w  w  .  j  av  a2s  .co  m

        }
    });
}

From source file:ch.tatool.app.service.impl.ModuleSessionDAO.java

License:Open Source License

/**
 * Get all distinct session property names for a module.
 * //from  w w  w  . j a v  a  2 s  . c o m
 * @return An array with [0] containing the item name, [1] the property name
 */
@SuppressWarnings("unchecked")
public List<Object[]> findDistinctSessionPropertyNames(final ModuleImpl module) {
    return (List<Object[]>) getHibernateTemplate().execute(new HibernateCallback() {
        //@SuppressWarnings("unchecked")
        public Object doInHibernate(Session session) {
            StringBuilder queryString = new StringBuilder();
            queryString.append("Select distinct(entry.nodeId), entry.name")
                    .append(" from ModuleSessionImpl session left join session.entriesImpl entry")
                    .append(" where session.module = :module").append(" and entry.nodeId is not NULL")
                    .append(" order by entry.nodeId, entry.name");

            // put together the complete query
            Query query = session.createQuery(queryString.toString());
            query.setParameter("module", module);

            // and get the results
            return (List<Object[]>) query.list();
        }
    });
}

From source file:ch.tatool.app.service.impl.TrialDAO.java

License:Open Source License

/** Get a list of all sessions. */
@SuppressWarnings("unchecked")
public List<Trial> loadAllTrials(final ModuleImpl module) {
    return (List<Trial>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            String queryString = "select trial from TrialImpl trial where trial.session.module = :module";
            Query query = session.createQuery(queryString);
            query.setParameter("module", module);
            return (List<Trial>) query.list();
        }/*  www .ja  v a 2  s.c om*/
    });
}

From source file:ch.tatool.app.service.impl.TrialDAO.java

License:Open Source License

/** Get all trials for a given session. */
@SuppressWarnings("unchecked")
public List<Trial> getTrials(final ModuleSession moduleSession) {
    return (List<Trial>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            getHibernateTemplate().update(moduleSession);
            Query query = session.createQuery(
                    "select trial from TrialImpl trial where trial.session = :session order by trial.id");
            query.setParameter("session", moduleSession);
            List<Trial> trials = (List<Trial>) query.list();
            // make sure we set the moduleSession correctly
            for (Trial t : trials) {
                t.setSession(moduleSession);
            }//from   w w w.  j  a v a 2s.com

            return trials;
        }
    });
}

From source file:ch.tatool.app.service.impl.TrialDAO.java

License:Open Source License

/** Get all trials for a given session. */
@SuppressWarnings("unchecked")
public List<Trial> getTrials(final ModuleImpl module, final ModuleSession moduleSession,
        final String elementNameLike, final String propertyNameLike, final int offset, final int maxResults) {
    if (elementNameLike == null && propertyNameLike == null) {
        throw new RuntimeException("Either elementName or propertyName needs to be non-null");
    }//from  w  ww.  j  a  va  2  s  .c  o  m
    return (List<Trial>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {

            getHibernateTemplate().update(module);
            if (moduleSession != null) {
                getHibernateTemplate().update(moduleSession);
            }

            StringBuilder q = new StringBuilder();
            q.append("select distinct(trial) from TrialImpl trial ");
            q.append(" join trial.entriesImpl as entry where ");
            if (moduleSession != null) {
                q.append(" trial.session = :session ");
            } else {
                q.append(" trial.session.module = :module ");
            }
            if (elementNameLike != null) {
                q.append(" and entry.nodeId like :elementName");
            }
            if (propertyNameLike != null) {
                q.append(" and entry.name like :propName");
            }

            // sort reverse
            q.append(" order by trial.id DESC");

            // create the query and set the various parameters
            Query query = session.createQuery(q.toString());
            if (moduleSession != null) {
                query.setParameter("session", moduleSession);
            } else {
                query.setParameter("module", module);
            }
            if (elementNameLike != null) {
                query.setParameter("elementName", elementNameLike);
            }
            if (propertyNameLike != null) {
                query.setParameter("propName", propertyNameLike);
            }
            // limit results if requested
            if (offset > 0) {
                query.setFirstResult(offset);
            }
            if (maxResults > -1) {
                query.setMaxResults(maxResults);
            }
            List<Trial> trials = (List<Trial>) query.list();

            // make sure we set the moduleSession correctly
            if (moduleSession != null) {
                for (Trial t : trials) {
                    t.setSession(moduleSession);
                }
            }

            return trials;
        }
    });
}

From source file:ch.tatool.app.service.impl.TrialDAO.java

License:Open Source License

/** Get the last trial index for a given session. */
@SuppressWarnings("unchecked")
public int getLastTrialIndex(final ModuleSession moduleSession) {
    List<Integer> index = (List<Integer>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Query query = session
                    .createQuery("select max(trial.index) from TrialImpl trial where trial.session = :session");
            query.setParameter("session", moduleSession);
            return (List<Trial>) query.list();
        }//from  w  w w  .  j  a  v  a  2s  .  c  o m
    });
    if (index != null && index.isEmpty()) {
        return 0;
    } else {
        return index.get(0);
    }
}

From source file:ch.tatool.app.service.impl.TrialDAO.java

License:Open Source License

/**
 * Get all distinct trial property names for a module.
 * /* w  w  w. j  a  v  a  2s .  com*/
 * @return An array with [0] containing the item name, [1] the property name
 */
@SuppressWarnings("unchecked")
public List<Object[]> findDistinctTrialPropertyNames(final ModuleImpl module) {
    return (List<Object[]>) getHibernateTemplate().execute(new HibernateCallback() {
        //@SuppressWarnings("unchecked")
        public Object doInHibernate(Session session) {
            StringBuilder queryString = new StringBuilder();
            queryString.append("Select distinct(entry.nodeId), entry.name")
                    .append(" from TrialImpl trial left join trial.entriesImpl entry")
                    .append(" where trial.session.module = :module").append(" and entry.nodeId is not NULL")
                    .append(" order by entry.nodeId, entry.name");

            // put together the complete query
            Query query = session.createQuery(queryString.toString());
            query.setParameter("module", module);

            // and get the results
            return (List<Object[]>) query.list();
        }
    });
}