List of usage examples for org.hibernate Query getQueryString
String getQueryString();
From source file:org.osaf.cosmo.dao.hibernate.query.StandardItemFilterProcessorTest.java
License:Apache License
public void testContentItemQuery() throws Exception { ContentItemFilter filter = new ContentItemFilter(); CollectionItem parent = new HibCollectionItem(); filter.setParent(parent);/*from ww w .j a va 2 s. c o m*/ filter.setTriageStatusCode(Restrictions.eq(TriageStatus.CODE_DONE)); Query query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibContentItem i join i.parentDetails pd where pd.primaryKey.collection=:parent and i.triageStatus.code=:param1", query.getQueryString()); filter.setTriageStatusCode(Restrictions.isNull()); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibContentItem i join i.parentDetails pd where pd.primaryKey.collection=:parent and i.triageStatus.code is null", query.getQueryString()); filter.setTriageStatusCode(Restrictions.eq(TriageStatus.CODE_DONE)); filter.addOrderBy(ContentItemFilter.ORDER_BY_TRIAGE_STATUS_RANK_ASC); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibContentItem i join i.parentDetails pd where pd.primaryKey.collection=:parent and i.triageStatus.code=:param1 order by i.triageStatus.rank", query.getQueryString()); }
From source file:org.osaf.cosmo.dao.hibernate.query.StandardItemFilterProcessorTest.java
License:Apache License
public void testNoteItemQuery() throws Exception { NoteItemFilter filter = new NoteItemFilter(); CollectionItem parent = new HibCollectionItem(); filter.setParent(parent);//ww w . j a v a 2 s .c om filter.setDisplayName(Restrictions.eq("test")); filter.setIcalUid(Restrictions.eq("icaluid")); filter.setBody(Restrictions.eq("body")); filter.setTriageStatusCode(Restrictions.eq(TriageStatus.CODE_DONE)); Query query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i join i.parentDetails pd, HibTextAttribute ta4 where pd.primaryKey.collection=:parent and i.displayName=:param1 and i.triageStatus.code=:param2 and i.icalUid=:param3 and ta4.item=i and ta4.qname=:ta4qname and ta4.value=:param5", query.getQueryString()); filter = new NoteItemFilter(); filter.setIsModification(true); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals("select i from HibNoteItem i where i.modifies is not null", query.getQueryString()); filter.setIsModification(false); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals("select i from HibNoteItem i where i.modifies is null", query.getQueryString()); filter.setIsModification(null); filter.setHasModifications(true); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals("select i from HibNoteItem i where size(i.modifications) > 0", query.getQueryString()); filter.setHasModifications(false); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals("select i from HibNoteItem i where size(i.modifications) = 0", query.getQueryString()); filter = new NoteItemFilter(); filter.setMasterNoteItem(new HibNoteItem()); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals("select i from HibNoteItem i where (i=:masterItem or i.modifies=:masterItem)", query.getQueryString()); filter = new NoteItemFilter(); Date date1 = new Date(1000); Date date2 = new Date(2000); filter.setReminderTime(Restrictions.between(date1, date2)); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i, HibTimestampAttribute tsa0 where tsa0.item=i and tsa0.qname=:tsa0qname and tsa0.value between :param1 and :param2", query.getQueryString()); }
From source file:org.osaf.cosmo.dao.hibernate.query.StandardItemFilterProcessorTest.java
License:Apache License
public void testEventStampQuery() throws Exception { NoteItemFilter filter = new NoteItemFilter(); EventStampFilter eventFilter = new EventStampFilter(); CollectionItem parent = new HibCollectionItem(); filter.setParent(parent);//from www .j a va 2s . co m filter.setDisplayName(Restrictions.eq("test")); filter.setIcalUid(Restrictions.eq("icaluid")); //filter.setBody("body"); filter.getStampFilters().add(eventFilter); Query query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i join i.parentDetails pd, HibBaseEventStamp es where pd.primaryKey.collection=:parent and i.displayName=:param1 and es.item=i and i.icalUid=:param2", query.getQueryString()); eventFilter.setIsRecurring(true); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i join i.parentDetails pd, HibBaseEventStamp es where pd.primaryKey.collection=:parent and i.displayName=:param1 and es.item=i and (es.timeRangeIndex.isRecurring=true or i.modifies is not null) and i.icalUid=:param2", query.getQueryString()); }
From source file:org.osaf.cosmo.dao.hibernate.query.StandardItemFilterProcessorTest.java
License:Apache License
public void testEventStampTimeRangeQuery() throws Exception { NoteItemFilter filter = new NoteItemFilter(); EventStampFilter eventFilter = new EventStampFilter(); Period period = new Period(new DateTime("20070101T100000Z"), new DateTime("20070201T100000Z")); eventFilter.setPeriod(period);// w ww. ja va 2 s . c om eventFilter.setTimezone(registry.getTimeZone("America/Chicago")); CollectionItem parent = new HibCollectionItem(); filter.setParent(parent); filter.getStampFilters().add(eventFilter); Query query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i join i.parentDetails pd, HibBaseEventStamp es where pd.primaryKey.collection=:parent and es.item=i and ( (es.timeRangeIndex.isFloating=true and es.timeRangeIndex.startDate < '20070201T040000' and es.timeRangeIndex.endDate > '20070101T040000') or (es.timeRangeIndex.isFloating=false and es.timeRangeIndex.startDate < '20070201T100000Z' and es.timeRangeIndex.endDate > '20070101T100000Z') or (es.timeRangeIndex.startDate=es.timeRangeIndex.endDate and (es.timeRangeIndex.startDate='20070101T040000' or es.timeRangeIndex.startDate='20070101T100000Z')))", query.getQueryString()); }
From source file:org.osaf.cosmo.dao.hibernate.query.StandardItemFilterProcessorTest.java
License:Apache License
public void testBasicStampQuery() throws Exception { NoteItemFilter filter = new NoteItemFilter(); StampFilter missingFilter = new StampFilter(); missingFilter.setStampClass(EventStamp.class); filter.getStampFilters().add(missingFilter); Query query = queryBuilder.buildQuery(session, filter); Assert.assertEquals(// w w w .jav a 2 s. co m "select i from HibNoteItem i where exists (select s.id from HibStamp s where s.item=i and s.class=HibEventStamp)", query.getQueryString()); missingFilter.setMissing(true); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i where not exists (select s.id from HibStamp s where s.item=i and s.class=HibEventStamp)", query.getQueryString()); }
From source file:org.osaf.cosmo.dao.hibernate.query.StandardItemFilterProcessorTest.java
License:Apache License
public void testBasicAttributeQuery() throws Exception { NoteItemFilter filter = new NoteItemFilter(); AttributeFilter missingFilter = new AttributeFilter(); missingFilter.setQname(new HibQName("ns", "name")); filter.getAttributeFilters().add(missingFilter); Query query = queryBuilder.buildQuery(session, filter); Assert.assertEquals(//w w w . j a va 2 s. c o m "select i from HibNoteItem i where exists (select a.id from HibAttribute a where a.item=i and a.qname=:param0)", query.getQueryString()); missingFilter.setMissing(true); query = queryBuilder.buildQuery(session, filter); Assert.assertEquals( "select i from HibNoteItem i where not exists (select a.id from HibAttribute a where a.item=i and a.qname=:param0)", query.getQueryString()); }
From source file:org.riotfamily.common.hibernate.HibernateUtils.java
License:Apache License
public static boolean queryContainsParameter(Query query, String name) { return query.getQueryString().matches(".+:" + name + "\\b.*"); }
From source file:org.sakaiproject.component.app.messageforums.MessageForumsMessageManagerImpl.java
License:Educational Community License
public void getChildMsgs(final Long messageId, List returnList) { List tempList;/* ww w . j av a 2s . c o m*/ HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.getNamedQuery(QUERY_CHILD_MESSAGES); Query qOrdered = session.createQuery(q.getQueryString()); qOrdered.setParameter("messageId", messageId, Hibernate.LONG); return qOrdered.list(); } }; tempList = (List) getHibernateTemplate().execute(hcb); if (tempList != null) { for (int i = 0; i < tempList.size(); i++) { getChildMsgs(((Message) tempList.get(i)).getId(), returnList); returnList.add((Message) tempList.get(i)); } } }
From source file:org.sakaiproject.component.app.messageforums.MessageForumsMessageManagerImpl.java
License:Educational Community License
public List getFirstLevelChildMsgs(final Long messageId) { HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.getNamedQuery(QUERY_CHILD_MESSAGES); Query qOrdered = session.createQuery(q.getQueryString()); qOrdered.setParameter("messageId", messageId, Hibernate.LONG); return qOrdered.list(); }//from w w w . ja va2s . co m }; return (List) getHibernateTemplate().executeFind(hcb); }
From source file:org.sakaiproject.component.app.messageforums.ui.PrivateMessageManagerImpl.java
License:Educational Community License
/** * helper method to get messages by type * @param typeUuid/*from w w w.j a v a2 s . c om*/ * @return message list */ public List getMessagesByType(final String typeUuid, final String orderField, final String order) { if (LOG.isDebugEnabled()) { LOG.debug("getMessagesByType(typeUuid:" + typeUuid + ", orderField: " + orderField + ", order:" + order + ")"); } // HibernateCallback hcb = new HibernateCallback() { // public Object doInHibernate(Session session) throws HibernateException, SQLException { // Criteria messageCriteria = session.createCriteria(PrivateMessageImpl.class); // Criteria recipientCriteria = messageCriteria.createCriteria("recipients"); // // Conjunction conjunction = Expression.conjunction(); // conjunction.add(Expression.eq("userId", getCurrentUser())); // conjunction.add(Expression.eq("typeUuid", typeUuid)); // // recipientCriteria.add(conjunction); // // if ("asc".equalsIgnoreCase(order)){ // messageCriteria.addOrder(Order.asc(orderField)); // } // else if ("desc".equalsIgnoreCase(order)){ // messageCriteria.addOrder(Order.desc(orderField)); // } // else{ // LOG.debug("getMessagesByType failed with (typeUuid:" + typeUuid + ", orderField: " + orderField + // ", order:" + order + ")"); // throw new IllegalArgumentException("order must have value asc or desc"); // } // // //todo: parameterize fetch mode // messageCriteria.setFetchMode("recipients", FetchMode.EAGER); // messageCriteria.setFetchMode("attachments", FetchMode.EAGER); // // return messageCriteria.list(); // } // }; HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.getNamedQuery(QUERY_MESSAGES_BY_USER_TYPE_AND_CONTEXT); Query qOrdered = session.createQuery(q.getQueryString() + " order by " + orderField + " " + order); qOrdered.setParameter("userId", getCurrentUser(), Hibernate.STRING); qOrdered.setParameter("typeUuid", typeUuid, Hibernate.STRING); qOrdered.setParameter("contextId", getContextId(), Hibernate.STRING); return qOrdered.list(); } }; return (List) getHibernateTemplate().execute(hcb); }