public void testSetProperties() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save(simple, new Long(10)); Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count"); q.setProperties(simple); assertTrue(q.list().get(0) == simple); // misuse of "Single" as a propertyobject, but it was the first testclass i found with a // collection ;) Single single = new Single() { // trivial hack to test properties with arrays. String[] getStuff() { return (String[]) getSeveral().toArray(new String[getSeveral().size()]); } }; List l = new ArrayList(); l.add("Simple 1"); l.add("Slimeball"); single.setSeveral(l); q = s.createQuery("from Simple s where s.name in (:several)"); q.setProperties(single); assertTrue(q.list().get(0) == simple); q = s.createQuery("from Simple s where s.name in (:stuff)"); q.setProperties(single); assertTrue(q.list().get(0) == simple); s.delete(simple); t.commit(); s.close(); }
/** * @param <T> type * @param queryName String * @return List or null * @throws HibernateException */ @SuppressWarnings("unchecked") public <T> List<T> query(String queryName, T type) throws HibernateException { if (queryName == null || queryName.length() < 1) { throw new NullPointerException("queryName is null or empty"); } if (type == null) { throw new NullPointerException("type is null"); } Query query = null; List<T> list = null; try { query = session.getNamedQuery(queryName); query.setProperties(type); list = query.list(); session.getTransaction().commit(); } catch (HibernateException he) { throw he; } finally { HibernateUtil.closeSession(session); } return list; } // end query
public void execute(final String statement, final Map paraMap) { Query query = getSession().createQuery(statement); if (paraMap != null) { query.setProperties(paraMap); } query.executeUpdate(); }
/** * 根据翻页、排序和其他参数查询记录. * * @param start 起始记录行数 * @param limit 查询记录数量 * @param sort 排序字段 * @param dir 排序方向 * @param params 查询参数 * @return */ @Transactional(readOnly = true) @SuppressWarnings("unchecked") public List<Map<String, String>> find( Integer start, Integer limit, String sort, String dir, Map<String, Object> params) { String hql = "select new map(p.id as id,p.name as name,p.description as description,p.type as type,p.order as order,p.creator.name as creator,p.created as created,p.modifier.name as modifier,p.modified as modified) " + "from Position p left join p.creator left join p.modifier where 1=1 "; if (params.keySet().size() > 0) { for (String key : params.keySet()) { if (fields.get(key).equals(FieldType.STRING)) { hql += " and p." + key + " like :" + key; } else { hql += " and p." + key + " = :" + key; } } } hql += " order by p." + sort + " " + dir; Query query = getSession().createQuery(hql).setFirstResult(start).setMaxResults(limit); if (params.keySet().size() > 0) { query.setProperties(params); } return query.list(); }
/** * @param <T> type * @param queryName String * @return List or null * @throws HibernateException */ public <T> ArrayList<T> query(T type, String queryName) throws HibernateException { if (queryName == null || queryName.length() < 1) { throw new NullPointerException("queryName is null or empty"); } if (type == null) { throw new NullPointerException("type is null"); } Query query = null; ArrayList<T> arrayList = null; try { query = session.getNamedQuery(queryName); query.setProperties(type); arrayList = (ArrayList<T>) query.list(); } catch (HibernateException he) { throw he; } finally { HibernateUtil.closeSession(session); } return arrayList; } // end query
/** * execute a <code>hql</code> sentence. * * @param hsql String * @return List or null * @throws HibernateException */ @SuppressWarnings("unchecked") public <T> ArrayList<T> hql(T type, String hql, int maxResults) throws HibernateException { if (hql == null || hql.length() < 1) { throw new IllegalArgumentException("the parameter hql is null"); } if (type == null) { throw new IllegalArgumentException("the parameter type is null"); } ArrayList<T> arrayList = null; Query query = null; try { session.setCacheMode(CacheMode.IGNORE); query = session.createQuery(hql); query.setMaxResults(maxResults); query.setProperties(type); query.setCacheable(false); arrayList = (ArrayList<T>) query.list(); session.flush(); session.clear(); } catch (HibernateException he) { throw he; } finally { HibernateUtil.closeSession(session); type = null; } return arrayList; } // end hql
/** * 根据查询HQL与参数列表创建Query对象. 与find()函数可进行更加灵活的操作. * * @param values 命名参数,按名称绑定. */ public Query createQuery(final String queryString, final Map<String, ?> values) { Assert.hasText(queryString, "queryString不能为空"); Query query = getSession().createQuery(queryString); if (values != null) { query.setProperties(values); } return query; }
public List<game> getGames(int positionId) { System.out.print(positionId); game g = new game(); g.setPositionId(positionId); Query query = session.createQuery("from game g where g.positionId=:positionId"); query.setProperties(g); List<game> list = query.list(); return list; }
public List<position> getPositions() { position p1 = new position(); Query query = session.createQuery("from position"); query.setProperties(p1); List<position> list = query.list(); for (position pos : list) { System.out.println(pos.getPositionId()); } return list; }
public List<question> getQuestions(int positionId) { System.out.print(positionId); question q = new question(); q.setPositionId(positionId); Query query = session.createQuery("from question q where q.positionId=:positionId"); query.setProperties(q); List<question> list = query.list(); return list; }
public List<treasure> getTreasures(int positionId) { System.out.print(positionId); treasure t = new treasure(); t.setPositionId(positionId); Query query = session.createQuery("from treasure t where t.positionId=:positionId"); query.setProperties(t); List<treasure> list = query.list(); return list; }
/* * Execute a named query during initialization that does not include logging or other dependencies. This is a * workaround for issues related to interdependencies between initialization routines in {@link * ApplicationInitializer} */ private List executeNamedQueryAtInit(String queryName, Map queryParameters) throws PersistenceException { Session session = null; try { session = StaticHibernateUtil.getSessionTL(); Query query = session.getNamedQuery(queryName); query.setProperties(queryParameters); return query.list(); } catch (Exception e) { throw new PersistenceException(e); } }
public Object queryUnique(final String queryString, final Map paraMap) { Query query = getSession().createQuery(queryString); if (cacheRegion.get() != null) { query.setCacheRegion(cacheRegion.get()); query.setCacheable(true); cacheRegion.set(null); } if (paraMap != null) { query.setProperties(paraMap); } return query.uniqueResult(); }
/* (non-Javadoc) * @see com.worldbestsoft.dao.hibernate.DocumentNumberDao#findByType(java.lang.String) */ @Override public DocumentNumber findByType(String type) { String hsql = "select o from DocumentNumber o where 1=1 "; final Map<String, Object> params = new HashMap<String, Object>(); hsql += " and o.type = :type"; params.put("type", type); hsql += " order by o.type"; Query queryObj = getSession().createQuery(hsql); queryObj.setProperties(params); List<DocumentNumber> result = queryObj.list(); if (null != result && result.size() > 0) { return result.get(0); } return null; }
public List<mediamsg> getMediaMsg(int positionId) { System.out.println(positionId); mediamsg msg = new mediamsg(); msg.setPositionId(positionId); Query query = session.createQuery("from mediamsg m where m.positionId=:positionId"); query.setProperties(msg); List<mediamsg> list = query.list(); for (mediamsg pos : list) { System.out.println(pos.getPositionId()); } return list; }
/** * 根据参数查询记录数量. * * @param params 查询参数 * @return */ @Transactional(readOnly = true) public Long count(Map<String, Object> params) { String hql = "select count(*) from Position where 1=1 "; if (null != params && params.keySet().size() > 0) { for (String key : params.keySet()) { if (fields.get(key).equals(FieldType.STRING)) { hql += " and " + key + " like :" + key; } else { hql += " and " + key + " = :" + key; } } } Query query = getSession().createQuery(hql); if (null != params && params.keySet().size() > 0) { query.setProperties(params); } return Long.valueOf(query.uniqueResult().toString()); }
/** * @param <T> type * @param queryName String * @param parameters ConcurrentHashMap * @return ArrayList<T> * @throws HibernateException */ public <T> ArrayList<T> query( String queryName, T type, ConcurrentHashMap<String, String> parameters) throws HibernateException { if (queryName == null || queryName.length() < 1) { throw new NullPointerException("parameters are null or empty"); } if (parameters == null || parameters.size() < 1) { throw new NullPointerException("parameters are null or empty"); } if (type == null) { throw new NullPointerException("type is null"); } String key = null; String value = null; Query query = null; ArrayList<T> list = null; try { query = session.getNamedQuery(queryName); query.setProperties(type); for (Map.Entry<String, String> entry : parameters.entrySet()) { key = entry.getKey(); value = entry.getValue(); query.setString(key, value); } list = (ArrayList<T>) query.list(); } catch (HibernateException he) { throw he; } finally { HibernateUtil.closeSession(session); } return list; } // end query
public List query( final String queryString, final Map paraMap, final Integer firstResult, final Integer maxResults) { Query query = getSession().createQuery(queryString); if (cacheRegion.get() != null) { query.setCacheRegion(cacheRegion.get()); query.setCacheable(true); cacheRegion.set(null); } if (paraMap != null) { query.setProperties(paraMap); } if (firstResult != null) { query.setFirstResult(firstResult); } if (maxResults != null) { query.setMaxResults(maxResults); } return query.list(); }
/** * Return the persistent instances of the given entity class which meet given parameters, * * @param clazz a persistent class * @param parameters pairs of key/value parameters which will be added to "where" part of the * query * @return list of persistent instances or null */ @SuppressWarnings("unchecked") public <T> List<T> getAllBy(String hql, Map<String, String> parameters) { Query query = currentSession().createQuery(hql); query.setProperties(parameters); return (List<T>) query.list(); };
/** * Return the persistent instance of the given entity class with the given parameters, It uses * HQL. If there are there several entities that meet given parameters, returns first item. * * @param clazz a persistent class * @param parameters pairs of key/value parameters which will be added to "where" part of the * query * @return a persistent instance or null */ public <T> T getBy(String hql, Map<String, String> parameters) { Query query = currentSession().createQuery(hql); query.setProperties(parameters); return (T) query.uniqueResult(); }