I am using hibernate search . I have following "User" class and i use "Hibernate Self Join Annotations One To Many mapping"
package com.pericent.dms.data; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import org.hibernate.annotations.Type; import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.ContainedIn; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; import org.hibernate.search.annotations.Store; @Entity @Table(uniqueConstraints = @UniqueConstraint(columnNames = "id")) @Indexed public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private Integer id; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private String userName; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private String email; private String password; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private String gender; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private Date joinDate; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private Date cancellationDate; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private String address; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private String cellPhone; @Column(columnDefinition = "TINYINT") @Type(type = "org.hibernate.type.NumericBooleanType") @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private Boolean enabled; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) private AppRole appRole; @IndexedEmbedded @ManyToOne(cascade = { CascadeType.ALL }) @JoinColumn(name = "vendor_id") private User vendor; @ContainedIn @OneToMany(fetch = FetchType.EAGER, mappedBy = "vendor") private Set<User> customers = new HashSet<User>();
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public Date getJoinDate() { return joinDate; }
public void setJoinDate(Date joinDate) { this.joinDate = joinDate; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public String getCellPhone() { return cellPhone; }
public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; }
public Boolean getEnabled() { return enabled; }
public void setEnabled(Boolean enabled) { this.enabled = enabled; }
public AppRole getAppRole() { return appRole; }
public void setAppRole(AppRole appRole) { this.appRole = appRole; }
public User getVendor() { return vendor; }
public void setVendor(User vendor) { this.vendor = vendor; }
public Set<User> getCustomers() { if (customers == null) { customers = new HashSet<User>(); } return customers; }
public void setCustomers(Set<User> customers) {
this.customers = customers; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public Date getCancellationDate() { return cancellationDate; }
public void setCancellationDate(Date cancellationDate) { this.cancellationDate = cancellationDate; } }
I got the following Exception : org.hibernate.search.SearchException: Circular reference. Duplicate use of com.pericent.dms.data.User in root entity com.pericent.dms.data.User#vendor. at org.hibernate.search.engine.spi.AbstractDocumentBuilder.checkForIndexedEmbedded(AbstractDocumentBuilder.java:706) at org.hibernate.search.engine.spi.AbstractDocumentBuilder.initializeMemberLevelAnnotations(AbstractDocumentBuilder.java:450) at org.hibernate.search.engine.spi.AbstractDocumentBuilder.initializeClass(AbstractDocumentBuilder.java:383) at org.hibernate.search.engine.spi.AbstractDocumentBuilder.<init>(AbstractDocumentBuilder.java:151) at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.<init>(DocumentBuilderIndexedEntity.java:179) at org.hibernate.search.spi.SearchFactoryBuilder.initDocumentBuilders(SearchFactoryBuilder.java:419) at org.hibernate.search.spi.SearchFactoryBuilder.buildNewSearchFactory(SearchFactoryBuilder.java:221) at org.hibernate.search.spi.SearchFactoryBuilder.buildSearchFactory(SearchFactoryBuilder.java:145) at org.hibernate.search.event.impl.FullTextIndexEventListener.initialize(FullTextIndexEventListener.java:129) at org.hibernate.search.hcore.impl.HibernateSearchIntegrator.integrate(HibernateSearchIntegrator.java:82) at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:304) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1740) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778) at com.pericent.dms.data.HibernateSearch.main(HibernateSearch.java:25)
|