-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How do I match a text string against the db
PostPosted: Thu Nov 09, 2006 8:10 am 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
I'm creating a search method where people enter the word they want to search for. This word must then be matched against the db.

I've created a hql query, but it dosn't work (Caused by: unexpected token: against):

Code:
public List<Picture> searchPictureByTags(String searchCriteria) throws NeSystemException {

   Session session = null;
   List<Picture> result = null;

   try {
      session = sessionFactory.openSession();
      Query q = session.createQuery("from Picture p where p.publicPicture='1' and match (p.pictureTags) against ('" + searchCriteria+ "') order by p.createdTime desc");
      result = (List<Picture>) q.list();
   } catch (Exception e) {
      throw new NeSystemException("Query failed!", e);
   } finally {
      if (session != null) {
         try {
            session.close();
         } catch (HibernateException he) {
            if (log.isErrorEnabled()) {
               log.error("Failed to close session!", he);
            }
         }
      }
   }

   return result;
}


So if someone can tell me how I can make it work that'd be nice :).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 10:02 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Have you tried that:
from Picture p where p.publicPicture='1' and :searchCriteria in elements(p.pictureTags) order by p.createdTime desc

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 10:24 am 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
MikePloed wrote:
Have you tried that:
from Picture p where p.publicPicture='1' and :searchCriteria in elements(p.pictureTags) order by p.createdTime desc


I changed my code to this:
Code:
public List<Picture> searchPictureByTags(String searchCriteria) throws NeSystemException {

   Session session = null;
   List<Picture> result = null;

   try {
      session = sessionFactory.openSession();
      Query q = session.createQuery("from Picture p where p.publicPicture='1' and :searchCriteria in elements(p.pictureTags) order by p.createdTime desc");
      q.setString("searchCriteria", searchCriteria);
      result = (List<Picture>) q.list();
   } catch (Exception e) {
      throw new NeSystemException("Query failed!", e);
   } finally {
      if (session != null) {
         try {
            session.close();
         } catch (HibernateException he) {
            if (log.isErrorEnabled()) {
               log.error("Failed to close session!", he);
            }
         }
      }
   }

      return result;
}


When I run it I get this exception:

Quote:
no.ne.exceptions.NeSystemException: Query failed!
at no.ne.integration.persistence.impl.HibernateDataProviderImpl.searchPictureByTags(HibernateDataProviderImpl.java:2393)
at no.ne.logic.impl.SearchServiceImpl.searchPictureByTags(SearchServiceImpl.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:165)
at $Proxy13.searchPictureByTags(Unknown Source)
at no.ne.web.controllers.membersite.search.IndexController.getNewSearch(IndexController.java:137)
at no.ne.web.controllers.membersite.search.IndexController.handleRequest(IndexController.java:80)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.NullPointerException
at org.hibernate.hql.ast.tree.MethodNode.handleElements(MethodNode.java:158)
at org.hibernate.hql.ast.tree.MethodNode.resolveCollectionProperty(MethodNode.java:109)
at org.hibernate.hql.ast.tree.CollectionFunction.resolve(CollectionFunction.java:22)
at org.hibernate.hql.ast.HqlSqlWalker.processFunction(HqlSqlWalker.java:832)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.collectionFunction(HqlSqlBaseWalker.java:2552)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.collectionFunctionOrSubselect(HqlSqlBaseWalker.java:4235)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.inRhs(HqlSqlBaseWalker.java:4152)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3835)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1758)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1686)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at no.ne.integration.persistence.impl.HibernateDataProviderImpl.searchPictureByTags(HibernateDataProviderImpl.java:2386)
... 35 more



Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 10:45 am 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
Figured it out :)

This works:

Code:
public List<Picture> searchPictureByTags(String searchCriteria) throws NeSystemException {

   Session session = null;
   List<Picture> result = null;

   try {
      session = sessionFactory.openSession();
      Query q = session.createQuery("from Picture p where p.publicPicture='1' "
            + "and p.pictureTags like :searchCriteria order by p.createdTime desc");
      q.setString("searchCriteria", "%" + searchCriteria + "%");
      result = (List<Picture>) q.list();
   } catch (Exception e) {
      throw new NeSystemException("Query failed!", e);
   } finally {
      if (session != null) {
         try {
            session.close();
         } catch (HibernateException he) {
            if (log.isErrorEnabled()) {
               log.error("Failed to close session!", he);
            }
         }
      }
   }

   return result;
}

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 11:23 am 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
Just one last question ;

Do I need to somehow escape the searchCriteria before giving it to the query? (to avoid sql injection attacks)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.