I'm using the following code from Hibernate Search in Action to do mass indexing:
Code:
Criteria query = session.createCriteria(entityClass)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.setCacheMode(CacheMode.IGNORE)
.setFetchSize(FETCH_SIZE)
.setFlushMode(FlushMode.MANUAL);
ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY);
int items = 0;
while (scroll.next()) {
items++;
session.index(scroll.get(0));
if (items % BATCH_SIZE == 0) {
session.flushToIndexes();
session.clear();
}
}
The entity in question is "Note", which has the following one-to-many association:
Code:
@OneToMany(mappedBy="note", fetch=FetchType.EAGER)
@IndexedEmbedded
public Collection<NoteEntity> getEntities() {
return this.entities;
}
When FetchType is EAGER, the mass indexing query comes back with multiple rows per Note, up to one per NoteEntity. Instead of serialising all of the note entities into a single document in Lucene, it creates multiple documents per Note.
When I change FetchType is LAZY, everything works as expected.
Can the above code be adapted to work in this case, or should I move to using a JPA query?