以前还真没注意这个怪问题:
数据库中的test表中有两条记录,如下
test表
id    caption
1     第1篇文章
2     第一篇文章对应的pojo为
public class Test{
   private int id;
   private String caption;
   public void setId(int id){
      this.id=id;
   }
   public int getId(){
      return this.id;
   }
   public void setCaption(String caption){
      this.caption=caption;
   }
   public String getCaption(){
      return caption;
   }
}
映射文件没问题。查询代码如下:
Session s=HibernateSessionFactory.getSession();
Query q=s.createQuery("from Test  where  condition like :condition");
query.setString("condition","%第1篇文章%");
query.list();
没有结果将query.setString("condition","%第一篇文章%");
这时就能搜索到结果这里省略了没必要的代码,我猜测是数字影响了模糊搜索。但是具体原因是为什么呢?数据库中使用%第1篇文章%就能搜索到。不信这怪事的可以自己尝试一下,花不了多长时间。

解决方案 »

  1.   

    Query q=s.createQuery("from Test  where  condition like :condition"); 
    query.setString("condition","%第1篇文章%"); 
    query.list(); Query你给它起的对象名是q   下面写的是query    可能是这里吧
      

  2.   

    。。直接打的,没注意语法错误。
    关键问题不是这里,是hql中数字会影响模糊查询。
    这问题大家迟早都会遇到,不信的话自己做个测试就知道了
      

  3.   

    Session s=HibernateSessionFactory.getSession(); 
    Query q=s.createQuery("from Test  where  condition like :condition"); 
    query.setString("condition","%第1篇文章%"); 
    query.list(); 
    query是那个对象呢
      

  4.   

    LZ态度有问题哦 要和气 要谦虚 ... 看看showsql出来的是啥 直接指定位置 不用:param这种方式看看行不行