lhlbt3good 2009-12-23 20:10
浏览 241
已采纳

Spring 与 Hibernate-Search 整合后 保存失败

我想做个Spring 和 Hibernate - Search 整和的搜索模块

 

可是失败了,整合没问题,没有错误...

 

但是Hibernate进行保存的时候,能保存进数据库,但是索引没有建立!

 

配置如下:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3309/cs4m</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="connection.autocommit">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property> 
    <property name="hibernate.search.default.indexBase">D:/temp/index</property>
    <property name="hibernate.search.autoregister_listeners">true</property>
    <property name="hibernate.search.indexing_strategy">manual</property>

 

 

    <mapping class="com.cs4m.vo.VOTest"/>

 

  </session-factory>
</hibernate-configuration>

 

------------------------  Spring  -----------------------

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
 <context:component-scan base-package="com.cs4m"/>
 
 <aop:aspectj-autoproxy/>

 <tx:annotation-driven transaction-manager="transactionManager" />

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
 <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > -->
  <property name="configLocation" value="classpath:hibernate.cfg.xml" />
 </bean>
 
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <bean id="hbmtpl" class="org.springframework.orm.hibernate3.HibernateTemplate" >
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

</beans>

 

 

我使用的是Annotation的配置方式

 

实体如下:

 

@Entity
@Table(name="ttttt")
@Indexed(index="ttttt")
public class VOTest {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 @DocumentId
 private int id;
 
 @Column(name="asdf")
 @Field(index=Index.UN_TOKENIZED,store=Store.YES,boost=@Boost(value=2.0f))
 private String name;
 
 @Column(name="asdfdd",length=5000)
 @Field(analyzer=@Analyzer(impl=PaodingAnalyzer.class),index=Index.TOKENIZED,store=Store.YES)
 private String text;

 

...get、set省略...

 

测试如下:

 

--------JUnit4测试--------

 

public class TestHibernate {
 public AbstractApplicationContext ac = null;
 public HibernateTemplate t = null;
 
 @Test
 public void test1(){

  ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  t = (HibernateTemplate)ac.getBean("hbmtpl");

  Session s = t.getSessionFactory().openSession();
  

  FullTextSession fs = Search.getFullTextSession(s);
  

  for(int i = 0;i<10;i++){
   VOTest vt = new VOTest();
   vt.setName("xxxxxxxx");
   vt.setText("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
   fs.save(vt);

  }
  fs.flush();
 }
}

 

运行过程没有错误,数据库上有数据,但是索引目录没有看到有索引文件,搜索也没有结果

 

我在没有用Spring 整合之前用过Hibernate-Search,很成功,一点问题没有

 

各位高手给解答一下吧!

 

谢谢

  • 写回答

2条回答 默认 最新

  • maskainv 2009-12-25 11:46
    关注

    建议吧Hibernate的配置也集成到Spring中去吧 我之前好像也有这种问题 延时提交 试试吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名