Hi Emmanuel,
Hibernate provides transparent support for mapping Entities to Lucene Index(es), but not the way back (correct me if i'm wrong..).
In a project I'm working which relies on Hibernate, I had to find a simple way to make the Lucene Index(es) to Entities mapping back. I provide you the example which led me to 2 implementation alternatives.
Imagine you have class Foo and class Bar, both are @Indexed, but on two different directories (so, 2 different Lucene indexes).
Now you want to make a ranked search over both of the indexes, through a Lucene MultiIndexSearcher. The search result are Lucene Documents, but you don't know if a Document originated from a Foo or a Bar instance (you would have to check the field names in the Document and the field names on the two classes).
One possibility is to extend the LuceneEventListener so that it is possibile to add a Field named "type" whose value is the canonical name of the domain entity ("x.y.Foo" or "x.y.Bar" in the example).
In this way it would be possible to map back the Lucene Document to a domain entity instance (you know the Class, and the id, you just have to make a findById for the given class and id).
The alternative to rewriting/extending the LuceneEventListener would be otherwise to add to each domain entity a getter stating the type of the class, and make that value go into the Lucene index but *not* going into the relational mapping:
Code:
// class Foo
@Transient
@Text(name="type")
String getType(){
return "x.y.Foo"; // return "x.y.Bar" in class Bar
}
Maybe there is another solution, but so far I can only see those 2 alternatives that I talked about, which summarizing again are:
1) extending/rewriting the LuceneEventListener, adding that "type" Field there after the DocumetBuilder created the Document (see my previous message).
2) adding the "@Text(..) String getType(){ .. }" to the domain entities (but this method has no business value)
If you can suggest me a better approach (different from "use the Compass Framework" :) I would appreciate a lot :-)
Thanks in advance,
-------------------------------
Alessio Pace.
http://jroller.com/page/alessiopace