Issue with the Hibernate Search index

Hello,

we have an issue with the Hibernate Search index - regarding these three entities (root is only listed because it is the index root, but probably isn’t part of the problem).

@Indexed
@Entity
Root {

    @IndexedEmbedded
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "root")
    private Info info;
    
    [...]
}
@Entity
@Table(name = "Info")
Info {

    @IndexedEmbedded
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "Info_2_User_Data", joinColumns = {
        @JoinColumn(name = "Info_ID", referencedColumnName = "Info_ID", columnDefinition = "INT(7)")},
            inverseJoinColumns = {
        @JoinColumn(name = "User_ID", referencedColumnName = "User_ID", columnDefinition = "INT(7)")})
    private Set<User> userList = new HashSet<>();

    @IndexedEmbedded
    @ManyToOne
    @JoinColumn(name = "Info_MainUser", referencedColumnName = "User_ID", columnDefinition = "INT(7)")
    private User mainUser;
    
    [...]
}
@Entity
@Table(name = "User_Data")
public class User {

    @FullTextField(name = "user_name")
    @Column(name = "User_Name", length = 20)
    private String name;
    
    @ManyToMany(mappedBy = "userList")
    private Set<Info> infoForUserList = new HashSet<>();
    
    @OneToMany(mappedBy = "mainUser")
    private Set<Info> infoForMainUser = new HashSet<>();
    
    [...]
}

As you can see there are two different relations between Info and User. If we change via our application the userList, everything is fine - new entries are added to the index. Buf if we do the same with the mainUser, the index is not updated. The DAO-Operation is in both cases the same - we edit Info and update the entity. In the database all changes are stored correctly.
My first assumption was that Hibernate has a problem because it gets confused with the two relations. So I removed the userList completely. But still no index updates for the mainUser. I also tried it without the infoForMainUser in User (we don’t need this bidirectional relation, it’s only for Hibernate Search/Lucene), but couldn’t make it work - it seems like I don’t understand extractions and how to configure it. But I don’t know if it really would fix the problem.

I’m not sure, if it is important, but these are the names of the indexes:
info.mainUser.user_name
info.userList.user_name

Do you have any idea, why the index isn’t updated for mainUser?

Thanks in advance and best regards.

Hello,

I’m going to need more information about what the “DAO-Operation” is exactly. What is the exact code of that operation?

I tried to reproduce your problem, to no avail: hibernate-test-case-templates/YourIT.java at reindexing · yrodiere/hibernate-test-case-templates · GitHub. Maybe you can send a pull request to edit that reproducer and make it fail?

Hi Yoann,

thank you for your response. My colleague is currently on vacation, so I’ll answer.
With your hint to look into the DAO operation, I could solve the issue. I also found out that the userList wasn’t always updated correctly.

So the problem was that we use a EntityManager Producer for injection, which looks like this:

@Produces
@RequestScoped
public EntityManager getEntityManager() {
	return entityManagerFactory.createEntityManager();
}

public void destroy(@Disposes final EntityManager entityManager) {
	entityManager.close();
}

(entityManagerFactory is also injected here in the class by a applicationscoped producer)

And he was the triggered DAO operation:

@Override
public Info updateInfo(final Info info) {
	entityManager.getTransaction().begin(); // this is the injected EntityManager
	final Info updated = entityManager.merge(info);
	entityManager.getTransaction().commit();
	return updated;
}

I changed the injected EntityManager and use now the EntityManagerFactory instead, call the createEntityManager() method and close it manually after the commit. So there is a difference wether I call the create directly or via the injection.

Thanks again.