我正在使用hibernate搜索。我有以下User类,我使用Hibernate Self Join Annotations One To Many映射
这是我的POJO:
@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>();
}
我遇到了以下问题:
org.hibernate.search.SearchException: Circular reference. Duplicate use of com.pericent.dms.data.User in root entity com.pericent.dms.data.User#vendor.
如何解决上述问题?
答案 0 :(得分:0)
我今天遇到了同样的问题并在此找到答案: https://forum.hibernate.org/viewtopic.php?f=9&t=991727&view=previous
您需要在供应商注释上设置深度,如下所示:
@IndexedEmbedded(depth = 1)
否则,Indexer会抱怨可能永无止境的引用链。
您设置的深度将限制您可以在关系中搜索的距离,例如如果深度为1,您可以查询vendor.email,但要求vendor.vendor.email则需要深度为2.
此外,您应该使用
注释id@DocumentId