hibernate search查不到结果
用了spring+hibernate+hibernatesearch,照着文档上的配置,写代码,写测试,可以成功运行,可就是查不到结果
这个是application.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/security jdbc.username=root jdbc.password=meng hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.search.default.directory_provider=filesystem hibernate.search.default.indexBase=/var/lucene/indexes dbcp.maxIdle=5 dbcp.maxActive=40
这个是spring配置文件
<!-- 读取配置值文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:application.properties" /> </bean> <!-- <context:property-placeholder location="classpath:application.properties"/>--> <!-- 设置datasource --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="defaultAutoCommit" value="false" /> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>--> <!-- <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>--> <prop key="hibernate.search.default.directory_provider">${hibernate.search.default.directory_provider}</prop> <prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop>--> </props> </property> <property name="packagesToScan" value="com.test_spring_security.model" /> </bean> <!-- 事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />--> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(public * com.test_spring_security.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="search*" read-only="true"/> <tx:method name="list*" read-only="true" /> <tx:method name="add*" /> <tx:method name="delete*" /> <tx:method name="update*" /> </tx:attributes> </tx:advice> <bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> <context:component-scan base-package="com.test_spring_security" />
然后这个是userDAOImpl的部分代码
public List<User> search(String search) { Session session=this.hibernateTemplate.getSessionFactory().getCurrentSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( User.class ).get(); org.apache.lucene.search.Query query =qb.keyword().onFields("name","password").matching(search).createQuery(); org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, User.class); List result = hibQuery.list(); return result; }
数据库里有数据,也按文档上说的执行了那两行代码,执行完之后就是什么结果都没有,它还发了一条sql语句。
Hibernate: select this_.id as id2_0_, this_.name as name2_0_, this_.password as password2_0_ from User this_ where ( this_.id in ( ? ) )
ApplicationContext contex = new ClassPathXmlApplicationContext("beans-core.xml"); SessionFactory sf=(SessionFactory) contex.getBean("sessionFactory"); FullTextSession fullTextSession = Search.getFullTextSession(sf.openSession()); try { fullTextSession.createIndexer().startAndWait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
我在debug时候发现他在执行到如下方法的时候,resultTransformer为空,list=resultTransformer.transformList(list);这句话就没能执行,而在执行这句话之前就把之前那个sql语句发出来了,不知道是不是这个原因,又或是其他的原因。
public List list() throws HibernateException { hSearchQuery.getTimeoutManager().start(); final List<EntityInfo> entityInfos = hSearchQuery.queryEntityInfos(); Loader loader = getLoader(); List list = loader.load( entityInfos.toArray( new EntityInfo[entityInfos.size()] ) ); //no need to timeoutManager.isTimedOut from this point, we don't do anything intensive if ( resultTransformer == null || loader instanceof ProjectionLoader ) { //stay consistent with transformTuple which can only be executed during a projection //nothing to do } else { list = resultTransformer.transformList( list ); } hSearchQuery.getTimeoutManager().stop(); return list; }
请教各位大神,我实在是没办法了。