星期三, 一月 04, 2006
Hibernate Lucene Integration
http://www.hibernate.org/hib_docs/annotations/reference/en/html/lucene.html
Chapter 4. Hibernate Lucene Integration
Lucene is a high-performance Java
search engine library available from the Apache Software Foundation. Hibernate
Annotations includes a package of annotations that allows you to mark any domain
model object as indexable and have Hibernate maintain a Lucene index of any
instances persisted via Hibernate.
4.1. Using Lucene to index your entities
4.1.1. Annotating your domain
model
First, we must declare a persistent class as @Indexed:
@Entity
@Indexed(index="indexes/essays")
public class Essay {
...
}
The index attribute tells Hibernate where the Lucene index is
located (a directory on your file system).
Lucene indexes contain four kinds of fields: keyword fields, text fields, unstored fields and unindexed fields. Hibernate Annotations provides annotations to mark a property of an entity as one of the first three kinds of indexed fields.
@Entity
@Indexed(index="indexes/essays")
public class Essay {
...
@Id
@Keyword(id=true)
public Long getId() { return id;
}
@Text(name="Abstract")
public String getSummary() {
return summary; }
@Lob
@Unstored
public String
getText() { return text; }
}
These annotations define an index
with three fields: Id, Abstract and Text.
Note: you must specify @Keyword(id=true) on the identifier property of your entity class.
4.1.2. Enabling automatic indexing
Finally, we enable the
LuceneEventListener for the three Hibernate events that occur after changes are
committed to the database.
<hibernate-configuration>
...
<event
type="post-commit-update"
<listener
class="org.hibernate.lucene.event.LuceneEventListener"/>
</event>
<event type="post-commit-insert"
<listener
class="org.hibernate.lucene.event.LuceneEventListener"/>
</event>
<event type="post-commit-delete"
<listener
class="org.hibernate.lucene.event.LuceneEventListener"/>
</event>
</hibernate-configuration>
星期三, 十二月 21, 2005
Hibernate,left join fetch,重复对象(记录)的问题
Hibernate 3.0中,mapping文件缺省配置的one-to-many的子集合是lazy declarations的,fetch的模式是join,当应用需要访问子表(1:M)中的数据时,hibernate框架会主动发起一条查询的sql。当我们需要在获取主记录的同时得到子记录的数据时,我们可以使用HSQL,“left join fetch”。但是当子记录存在时,主对象记录会有重复。
解决方法:http://forum.hibernate.org/viewtopic.php?t=938705&highlight=onetomany+query
fetch return distinct root instances
join has the same meaning as SQL join, so just use
Set results = new HashSet(query.list()) if you don't want duplicate results.