getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException 
{
try {
FullTextSession fullSession = Search.createFullTextSession(session);
QueryParser parser = new QueryParser("iartTitle",new StandardAnalyzer());   
Query luceneQuery = parser.parse("iartTitle:" + "重庆");  

FullTextQuery query = fullSession.createFullTextQuery( luceneQuery, InfoArticle.class );

List<InfoArticle> list = query.list();


return list;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
});
这是我的查询代码,但是只能查一个字段,怎样多条件联合查询

解决方案 »

  1.   

    http://blog.chinaunix.net/u1/55633/showart_1878062.html  这里有篇文章...你可以参考一下...
      

  2.   


    这个是讲的hibernate关于search的hql用法我的问题是hibernateSearch(全文检索)的hql用法不过同样谢谢你的回答
      

  3.   

     
    方法一:利用hibernate的方法设置参数
    public List<IptvAction> queryByIptvAction() {
    String hql = "from table where cat = 5 and dateTime between ? and ?  group by code order by count(*) desc";
    return this.list(hql, 0, 6, new Object[] { ConvertDate.getOldDate(),
    ConvertDate.getNewDate() });
    }方法二:直接写拼接语句
    String hql = "from table a where a.code="+ code +" and a.uid="+id;
    return  this.getHibernateTemplate().find(hql);
      

  4.   


    这位大虾的回答也是讲的hibernate关于search的hql用法 你没理解我的意思,我明白,因为habernateSearch(全文检索)接触的人不多
      

  5.   


    全文检索?
    你是想一个字段但是要好多个值?
    例如  iartTitle 既为:重庆又为广州?
      

  6.   

    不太懂楼主意思。用 or不行吗?我没明白楼主的需求。 sorry
      

  7.   

    public List<T> findByExample(T instance) {
    try {
    List<T> results = getHibernateTemplate().findByExample(instance);
    return results;
    } catch (RuntimeException re) {
    throw re;
    }
    }
    instance 有什么属性就给什么属性赋值即可。
      

  8.   

    我一个对象有几个属性嘛
    title,content,
    我现在只知道单独的检索,我想联合条件检索HibernateSearch检索不查数据库,而是直接找索引(索引通过habernate的增、删、改建立)
      

  9.   

    看来大家都没用过hibernateSearch你们回答的都是hibernate的基本用法
      

  10.   


    可以
    不是你不明白需求
    是因为你没用过hibernateSearch
    所以看不明白
    你看我贴的代码就知道这不是hibernate的基本查询
      

  11.   

    正在google   HibernateSearch
    发现我确实没接触过。
    确实不知道这个东东。
    而且中文介绍好像比较少啊?
    搜出来都是English.....
      

  12.   

    网上逛了很久,看了一些,发现都是和你一样。FullTextSession fullTextSession = Search.createFullTextSession(session);   
            assertNotNull(session);   
       
            QueryParser parser = new QueryParser("name", new StopAnalyzer());   
            org.apache.lucene.search.Query luceneQuery = parser   
                    .parse("name:Kevin");   
            Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,   
                    Employee.class);   不明白楼主你要的联合查询是撒意思啊?
    是一个字段的联合?还是多个字段啊?不过估计你说了我还是不明白。呵呵。。
    继续关注。
      

  13.   

    楼主:不知道这个是不是啊?  /**  
         * 创建索引  
         * @param path  
         */  
        public void createIndex(String path){   
            try {   
                IndexWriter writer = new IndexWriter(path,new StandardAnalyzer(),true);   
                Document docA = new Document();   
                //相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置   
                //Field.Store 是否覆盖原来的索引文件,而不是重新建一个   
                Field fieldA = new Field("content","搜索引擎",Field.Store.YES,Field.Index.TOKENIZED);   
                //我们把列(fieldA)加到某一行(docA)中   
                docA.add(fieldA);   
                   
                docA.add(new Field("title","你好中国",Field.Store.YES,Field.Index.TOKENIZED));   
                docA.add(new Field("content","欢迎你llying",Field.Store.YES,Field.Index.TOKENIZED));   
                docA.add(new Field("lastModifyTime","2008-9-17",Field.Store.YES,Field.Index.TOKENIZED));   
                docA.add(new Field("testCapital","HelloWangzi",Field.Store.YES,Field.Index.TOKENIZED));   
                docA.add(new Field("testAndOr","test and",Field.Store.YES,Field.Index.TOKENIZED));   
                   
                Document docB = new Document();   
                //相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置   
                Field fieldB = new Field("content","创建索引",Field.Store.YES,Field.Index.TOKENIZED);   
                //我们把列(fieldA)加到某一行(docA)中   
                docB.add(fieldB);   
                docB.add(new Field("title","你好世界",Field.Store.YES,Field.Index.TOKENIZED));   
                docB.add(new Field("content","欢迎加入jee高级开发群46176507",Field.Store.YES,Field.Index.TOKENIZED));   
                docB.add(new Field("lastModifyTime","2008-9-6",Field.Store.YES,Field.Index.TOKENIZED));   
                docB.add(new Field("testCapital","hellowangZi",Field.Store.YES,Field.Index.TOKENIZED));   
                docB.add(new Field("testAndOr","test or",Field.Store.YES,Field.Index.TOKENIZED));   
                   
                writer.addDocument(docA);   
                writer.addDocument(docB);   
                   
                //如果对海量数据进行创建索引的时候,需要对索引进行优化,以便提高速度   
                writer.optimize();   
                   
                //跟数据库类似,打开一个连接,使用完后,要关闭它   
                writer.close();   
            } catch (CorruptIndexException e) {   
                e.printStackTrace();   
            } catch (LockObtainFailedException e) {   
                e.printStackTrace();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
      
      

  14.   

    Hi:lz解决了吗?我还在等你的答案哦!!!记得结贴贴答案啊~~~
      

  15.   

    public Wrapper searchDigitalBook(final PageExtNative page){
    return (Wrapper) this.baseDao.execute(new HibernateCallback(){
    public Object doInHibernate(Session session) throws HibernateException, SQLException{
    // 将SESSION转换成lucence session,执行全文检索
    FullTextSession fullSession = Search.getFullTextSession(session);
    //Map<String, String> search = page.getMSearch();
    StringBuffer searchQuery = new StringBuffer();
    Wrapper wrap = null;
    Query luceneQuery = null;

    QueryParser parser = new QueryParser("dboSourceName", new StandardAnalyzer());
    try {
    searchQuery.append("( dboSourceName:").append(page.getMSearch().get("contantion"));//书名
    searchQuery.append(" OR dboAuthor:").append(page.getMSearch().get("contantion"));//作者
    searchQuery.append(" OR dboSummary:").append(page.getMSearch().get("contantion"));//摘要
    searchQuery.append(" OR dboKeyword:").append(page.getMSearch().get("contantion"));//关键词
    searchQuery.append(") AND ddbId:").append(page.getMSearch().get("ddbId"));//资源库ID

    luceneQuery = parser.parse(searchQuery.toString());
    FullTextQuery query = fullSession.createFullTextQuery(luceneQuery, DigitalBookDetail.class);
    //总记录数
    int size = query.getResultSize();
    query.setFirstResult(page.getStart());
    query.setMaxResults(page.getLimit());
    List<DigitalBookDetail> books = query.list();
    wrap = new Wrapper(books,String.valueOf(size));
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return wrap;
    }
    });
    }这就是加条件的
    终于解决了
      

  16.   

    哪如果多个条件中有的是int,有的是date,怎么办呢。难道都是以string的格式传入吗?让hibernate search自己去转型