Hi,
Is it possible to perform searches from both sides of an hibernate search association?
I have an indexed ClassA with association:
Code:
@IndexedEmbedded
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "ClassB_ID", nullable = false)
private ClassB classB
And an indexed ClassB with a @ContainedIn association:
Code:
@ContainedIn
@OneToMany(mappedBy = "classB", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<ClassA> classAlist = new HashSet<ClassA>();
I can with this associations perform searches with ClassA as root object.
However I would also like to perform other search queries with ClassB as root object and be able to perform searches on classAlist, is this possible.
It is not possible to put both @IndexedEmbedded and @ContainedIn on both sides...
--------------
I edit this query with my solution...
To be able to search from a root object associated with ClassB and then towards the classAlist (@containedIn) I changed the annotation to:
Code:
@ContainedIn
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@FieldBridge(impl = MyBridge.class)
@OneToMany(mappedBy = "classB", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<ClassA> classAlist = new HashSet<ClassA>();
And MyBride.class looks like this:
Code:
public class MyBridge implements TwoWayFieldBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
@SuppressWarnings("unchecked")
Set<ClassA> classAlist= (Set<ClassA>) value;
StringBuilder sb = new StringBuilder();
for (ClassA classA : classAlist) {
sb.append(classA.getClassR().getClassM().getClassMId()).append(" ");
}
luceneOptions.addFieldToDocument(name, sb.toString(), document);
}
@Override
public Object get(String name, Document document) {
return document.get(name);
}
@Override
public String objectToString(Object object) {
return object.toString();
}
}
Also, in the search query we now only reference to classAlist and not the path (classA.classR.classM.classMId)