本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7633033
建立时间:2006年5月29日




[Hibernate]手动构建Hibernate Annotations Lucene的索引
软件技术

lhwork 发表于 2007/1/22 14:35:27

在使用Hibernate的Lucene的时候,是通过事件监听器的方式来更新索引的。但是通常情况下,尤其是测试的时候,通过其他方式插入数据库,比如通过DbUnit。这个时候就需要同步的手动创建索引,我根据Hibernate Lucene的源代码,自己写了一个简单的程序。 原理很简单:先删除所有的索引,然后再添加。 我是对User这个实体进行索引的。 private static void remove(DocumentBuilder builder, Serializable id) {  Term term = builder.getTerm( id );  try {   IndexReader reader = IndexReader.open( builder.getFile() );   reader.delete( term );   reader.close();  }  catch (IOException ioe) {   ioe.printStackTrace();  } }  private static void add(final Object entity, final DocumentBuilder builder, final Serializable id) {  Document doc = builder.getDocument( entity, id );  try {   File file = builder.getFile();   IndexWriter writer = new IndexWriter( file, builder.getAnalyzer(), ! file.exists() );   writer.addDocument( doc );   writer.close();  }  catch (IOException ioe) {   ioe.printStackTrace();  } } 上面两个方法是直接拷贝了Hibernate Lucene 的LuceneEvent源代码中拷贝过来的。用来删除和添加索引。 protected static ApplicationContext ctx = null;  static {  String[] path = { "applicationContext-hibernate.xml };  ctx = new FileSystemXmlApplicationContext(path); }  /**  * @param args  */ public static void main(String[] args) {  UserManager userManager = null;  userManager = (UserManager) ctx.getBean("userManager");  List<User> users = userManager.getAllUsers();    Analyzer analyzer = new CJKAnalyzer();  String indexDirName = "indexes";  File indexDir = indexDirName != null ? new File( indexDirName ) : new File( "." );  final DocumentBuilder documentBuilder = new DocumentBuilder( User.class, analyzer, indexDir );  for (int i = 0; i < users.size(); i++) {   User user = (User) users.get(i);   final Serializable id = user.getUsername();   remove( documentBuilder, id );   add( user, documentBuilder, id );  } } 由于我是用Spring来集成Hibernate的,首先从Spring中获取UserManager 这个Bean,获得所有的用户,然后构建DocumentBuilder 对每个User实体构建索引即可。


阅读全文(5308) | 回复(2) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.777 second(s), page refreshed 144809981 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号