lucene index not getting sync when any update occurs in DB through hibernate

爷,独闯天下 提交于 2019-12-21 02:14:10

问题


I am working on some POC stuff on Hibernate Search based on Lucene using below env:

  • hibernate-search-engine-4.4.2.Final.jar
  • lucene-core-3.6.2.jar
  • MySQL 5.5
  • Use @Indexed annotation on domain class.
  • Use @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO) over field.
  • Use @IndexedEmbedded over collection of Instance of different domain class.

I did explicit indexing ONLY on start of application (as this is written in Hibernate Search API that Hibernate Search will transparently index every entity persisted, updated or removed through Hibernate Core,Hibernate Search Indexing) through below code:

private static void doIndex() throws InterruptedException {
    Session session = HibernateSearchUtil.getSession();

    FullTextSession fullTextSession = Search.getFullTextSession(session);
    fullTextSession.createIndexer().startAndWait();

    fullTextSession.close();
}

It works fine when indexing & searching is done on already seeded DB as well as on create & delete operation through hibernate core within application.

But NOT on any update operation on existing data through Hibernate Core as I am not getting the updated data when do search on this through hibernate search.

Don't know the reason that whether it is problem with Hibernate Search Or lucene, means either index is NOT getting updated or some other reason due to which not getting the updated result through Hibernate Search.

User Class:

@Entity
@Table(name = "user")
@Indexed
public class User implements java.io.Serializable {

private static final long serialVersionUID = 5753658991436258019L;
private Integer idUser;

@Field(index = Index.YES, analyze = Analyze.YES, norms = Norms.NO, store = Store.NO)
private String Name;

private Set<UserInfo> userInfos = new HashSet<UserInfo>(0);

public User() {
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "iduser", unique = true, nullable = false)
public Integer getIdUser() {
    return idUser;
}

public void setIdUser(Integer idUser) {
    this.idUser = idUser;
}

@Column(name = "name", nullable = false, length = 256)
public String getName() {
    return this.Name;
}

public void setName(String tenantName) {
    this.Name = tenantName;
}

@IndexedEmbedded
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserInfo> getUserInfos() {
    return userInfos;
}

public void setUserInfos(Set<UserInfo> userInfos) {
    this.userInfos = userInfos;
    }
}

UserInfo Class:

@Entity
@Table(name = "userInfo")
public class UserInfo implements java.io.Serializable {

private static final long serialVersionUID = 5753658991436258019L;
private Integer iduserInfo;
private User user;
private String address; 

public UserInfo() {
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "iduserInfo", unique = true, nullable = false)
public Integer getIduserInfo() {
    return iduserInfo;
}

public void setIduserInfo(Integer iduserInfo) {
    this.iduserInfo = iduserInfo;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO, norms=Norms.NO)
@Column(name = "address", nullable = false, length = 256)
public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}
}

回答1:


Try By adding the

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
@ContainedIn
public User getUser() {
   return user;
}


来源:https://stackoverflow.com/questions/20898541/lucene-index-not-getting-sync-when-any-update-occurs-in-db-through-hibernate

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