Hibernate Search + DbUnit Indexing

大城市里の小女人 提交于 2019-12-24 13:09:16

问题


I am writing some JUnits for my hibernate search implementation.

I use a HSSQL in memory database. I use DBUnit to populate this DB (an XML file). It definitely works as other non-search tests work with the same data. The search code definitely works as I've tried it in the web-app and it returns the correct records.

I assume that Hibernate Search will only index database entries that have been inserted using Hibernate. I tried to index the db manually using : -

fullTextEntityManager.createIndexer().startAndWait();

I have put this in a bean that runs after Spring initialises

public class SearchIndexer {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  public SearchIndexer(){
  }

  @PostConstruct
  public void doIndexing(){
    EntityManager em = entityManagerFactory.createEntityManager();
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
    try {
      fullTextEntityManager.createIndexer().startAndWait();
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

I also Autowired it into my JUnit class and ran the doIndexing method manually (to be sure it was being picked up correctly AFTER the data was loaded).

@Before
public void setup() throws Exception{  
  dbUnitAdapter.setup("ClubDaoTest.xml");
  searchIndexer.doIndexing();
  super.before();
}  

The dbUnitAdapter simply takes an XML file and inserts it in the db using DBUnit.

The entity is annotated like so: -

  @Field
  private String name;

  @NotBlank
  private String type;

  @Field
  private String address1;

  @Field
  private String county;

  @Field
  private String address2;
  @Field
  private String town;
  @Field
  private String country;
  @Field
  private String postcode;
  private String telephone;
  private String mobile;
  private String fax;
  @Field
  private String email;

  @Field
  @NumericField
  private long lft;
  @Field
  @NumericField
  private long rgt;

I tried also tried inserting the data using hibernate (creating a Club entity), this didn't work either confusingly. I changed the the search index location from RAM to filesystem and used luke to read it. I could see the data that I'd tried inserting using hibernate, but no other data that I could see, although it was my first time using Luke, so I may have made a mistake.

来源:https://stackoverflow.com/questions/12038189/hibernate-search-dbunit-indexing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!