Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@IndexedEmbedded on a list of lazily-loaded entities, does not get into the search index automatically

I have an issue, which I think should be easily solved.

I use Hibernate Search to index @Entitiy classes which have relations to other entities.

Whenever an entity points to another entity that should be indexed as well, say the User who uploaded a particular Photo, I use @IndexedEmbedded, which has worked absolutely fine with HSearch's automatic indexing.

However, I also have some @IndexedEmbeded annotations set on @ManyToOne relations. Imagine a photo having a list of related comments. These ones are by default lazily-loaded, i.e. not fetched from the DB, until actually needed. I noticed that when I add a comment, no matter how much time passes, it does not get indexed, until I do a manual reindexing. Then everything works fine. I have not observed this with any of the other IndexedEmbedded relations that i have, for instance, if I change the location of a photo, in a few minutes, it gets into the index and is perfectly searcheable.

Any explanation? Solution?

like image 580
Preslav Rachev Avatar asked Nov 12 '22 22:11

Preslav Rachev


1 Answers

Your mapping should look something like this

@OneToMany(mappedBy="photo", cascade = { CascadeType.ALL}, fetch=FetchType.LAZY) 
    @IndexedEmbedded 
    @Type(type="java.util.Set") 
    private Set<Comment> comments; 

...................................................

....................................................

@ContainedIn 
@ManyToOne 
   @JoinColumn(name="PHOTO_ID") 
    private Photo photo; 

Note the bi-directionality of relations (using mappedBy) and the use of @ContainedIn. This is pretty much I think you should need to make your example working.

like image 88
Shailendra Avatar answered Nov 15 '22 13:11

Shailendra