Lucene + hibernate + spring configuration experience

Source: Internet
Author: User
Packages to be prepared:

Hibernate3.2.0.jar

Hibernate-annotations.jar

Ejb3-persistence.jar

Lucene-core-2.0.0.jar

Spring1.2.6.jar
Basically, this is worth noting. If you use ehcache for hibernate cache, you need
Use a jar package of the ehcache-1.2.1 version.
The ejb3-persistence.jar package can be found in the hibernate-annotations download package

Hibernate Configuration:
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
"

Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd

">
<Hibernate-configuration>
<Session-factory name = "sessionfactory">
<Property name ="
Hibernate. Lucene. index_dir
">/Indexdir </property>
<Property name ="
Hibernate. Lucene. Analyzer
"> Org. Apache. Lucene. analysis. Standard. standardanalyzer </property>
<Event type ="
Post-commit-Update
">
<Listener class = "org. hibernate. Lucene. event. javaseeventlistener"/>
</Event>
<Event type ="
Post-commit-insert
">
<Listener class = "org. hibernate. Lucene. event. javaseeventlistener"/>
</Event>
<Event type ="
Post-commit-delete
">
<Listener class = "org. hibernate. Lucene. event. javaseeventlistener"/>
</Event>
</Session-factory>
 
</Hibernate-configuration>
Hibernate. Lucene. Analyzer
This is to specify a word divider for Lucene
These two items are not required, but after configuration, it saves a lot of trouble
Post-commit-Update, post-commit-insert, post-commit-delete
Specify the listener for the hibernate status. Hibernate updates the Lucene index through the monitoring of these three statuses.
Hibernate. Lucene. index_dir:
This is to specify an index directory for Lucene (you can give the absolute path, because I am a Linux system, so there is no drive letter), of course, only valid for hibernate, if you skip hibernate direct operations
Lucene, this directory is useless

Configuration in Spring:
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource"> <ref local = "datasource"/> </property>
<Property name = "mappingresources">
<List>
<Value> faqmessage. HBM. xml </value>
</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect"> org. hibernate. dialect. postgresqldialect </prop>
<Prop key = "hibernate. cache. provider_class"> org. hibernate. cache. ehcacheprovider </prop>
<Prop key = "hibernate. Connection. release_mode"> on_close </prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
<Prop key = "hibernate. Connection. autocommit"> false </prop>
</Props>
</Property>
 
<Property name = "configurationclass" value = "org. hibernate. cfg. annotationconfiguration"/>
<Property name = "configlocation" value = "classpath: hibernate. cfg. xml"/>

</Bean>
People familiar with spring + hibernate should be familiar with this configuration. It is worth noting that the last two items must be configured.

Define hibernate entities
@ Indexed (Index = "/faqindex ")
Public class FAQ extends basefaq {
 
Private Static final long serialversionuid = 1l;
Private int ID;
Private string subject;
Private string messagecontent;
 
@ Keyword (ID = true)
Public int GETID (){
Return this. ID;
}

@ Text (name = "subject ")
Public String getsubject (){
Return subject;
}
 
Public void setsubject (string subject ){
This. Subject = subject;
}
 
@ Unstored (name = "message ")
Public String getmessagecontent (){
Return this. messagecontent;
}
 
}
First, let's talk about the four annotation types of hibernate.
@ Indexed (Index = "// faqindex ")
This type specifies the path where your object is written to Lucene indexer. If you have configured hibernate. Lucene. index_dir,
The relative path is the relative path of hibernate. Lucene. index_dir, that is, the "// faqindex" folder is placed in the sub-folder under hibernate. Lucene. index_dir.
In addition, you must note that if you specify the index path as "/", It accesses the hibernate. Lucene. index_dir folder,
If you have not created any index in advance in hibernate. Lucene. index_dir, it cannot be appended.
@ Keyword (ID = true)
Basically, you need to declare the hibernate Object ID as the Lucene field keyword type, and set id = true. There is no way, hibernte will always rely on ID to find the object, the field name will be "ID ".

@ Text (name = "subject ")
Public String getmessagesubject (){
Return super. getmessagesubject ();
}
This section declares the messagesubject attribute as a field of the "subject" type and text type.
@ Unstored (name = "message ")
Public String getmessagecontent (){
Return this. messagecontent;
}
 
}
This section declares the messagecontent attribute as a field named "message" and whose type is unstored.
(
Basically, all configurations are complete.
Every time update, save, or delete is performed, Hibernate automatically updates the lucence index,
Instead of writing the hibernate interceptor, it is so painful.
Basically, the annotations for Lucene of Hibernate is used to solve the Index Update problem of Lucene.
I have referred to the official configuration method of hibernate above. However, if you do not configure it according to the official configuration method,
So I wrote this article and hope to give you some guidance.
This article only introduces the configuration. If you want to know the actual usage of Lucene, I will give you an example to explain
Usage. Of course, you can go to Dangdang (

Http://www.dangdang.com

) Order <Ajax + Lucene> this book, which only describes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.