我有一个小项目,我想与lucene和hibernate集成: 这是bean文件: package com.domain.java;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Entity
@Indexed
@Analyzer(impl = org.apache.lucene.analysis.standard.StandardAnalyzer.class)
public class TempFile {
@Field(store = Store.YES)
private String iconName;
@Field(store = Store.YES)
private String name;
@Id
@DocumentId
@Field(store = Store.YES)
private String path;
@Field(store = Store.YES)
private boolean mightHaveThumbnail;
@Field(store = Store.YES)
private boolean folder;
@Field(store = Store.YES)
private boolean file;
public String getIconName() {
return iconName;
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
public boolean isMightHaveThumbnail() {
return mightHaveThumbnail;
}
public boolean isFolder() {
return folder;
}
public boolean isFile() {
return file;
}
public void setIconName(String iconName) {
this.iconName = iconName;
}
public void setName(String name) {
this.name = name;
}
public void setPath(String path) {
this.path = path;
}
public void setMightHaveThumbnail(boolean mightHaveThumbnail) {
this.mightHaveThumbnail = mightHaveThumbnail;
}
public void setFolder(boolean folder) {
this.folder = folder;
}
public void setFile(boolean file) {
this.file = file;
}
}
这是Hibernate文件:
package com.domain.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.service.ServiceRegistry;
public class HibernateTest {
public static void main(String[] args) {
TempFile mm = new TempFile();
mm.setFile(true);
mm.setPath("patdsaasdsadahswdsaaad "+new Date());
mm.setName("nasdasassadasaeeassaaasddas "+new Date());
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./resources/postgres/hibernate.properties"));
// readAPropertyFile("./resources/postgres/hibernate.properties");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Configuration configuration = new Configuration();
File file = new File("./resources/postgres/hibernate.cfg.xml");
System.out.println(file.exists());
configuration.mergeProperties(properties);
configuration.configure(file);
configuration.addAnnotatedClass(TempFile.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
FullTextSession fullTextSession = Search.getFullTextSession(session);
try {
fullTextSession.createIndexer().startAndWait();
fullTextSession.beginTransaction();
fullTextSession.close();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// session.save(mm);
// session.getTransaction().commit();
session.close();
// now lets pull events from the database and list them
session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery("from TempFile").list();
for ( TempFile event : (List<TempFile>) result ) {
System.out.println( "Event (" + event.getPath() + ") : " + event.getPath() );
System.out.println( "Event (" + event.getName() + ") : " + event.getName() );
}
session.getTransaction().commit();
session.close();
}
private static void readAPropertyFile(String propertiesFilePath) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(propertiesFilePath);
// load a properties file
prop.load(input);
// get the property value and print it out
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
<!-- Echo all executed SQL to stdout -->
<!-- >property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.search.default.directory_provider">filesystem</property>
-->
<!--
< Enable Hibernate's automatic session context management >
<property name="current_session_context_class">thread</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_structured_entries">true</property>
<property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property>
mapping resource="hibernate-config/domain/Event.hbm.xml"/>
<mapping resource="hibernate-config/domain/Person.hbm.xml"/>
<mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/>
<mapping resource="hibernate-config/domain/Account.hbm.xml"/>
<mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/>
<mapping resource="hibernate-config/domain/Item.hbm.xml"/-->
<!--
<mapping class="com.domain.java.TempFile"/>
org.hibernate.search.store.impl.RAMDirectoryProvider
-->
<!-- Would set this in production application. Index stored on disk. -->
<property name="hibernate.search.default.directory_provider">
org.hibernate.search.store.impl.FSDirectoryProvider
</property>
<property name="hibernate.search.default.indexBase">c:\aatemp\lucene\indexes</property>
</session-factory>
</hibernate-configuration>
属性文件
######################
### Query Language ###
######################
## define query language constants / function names
hibernate.query.substitutions yes 'Y', no 'N'
## select the classic query parser
#hibernate.query.factory_class org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
#################
### Platforms ###
#################
## JNDI Datasource
#hibernate.connection.datasource jdbc/test
#hibernate.connection.username db2
#hibernate.connection.password db2
## PostgreSQL
hibernate.dialect org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class org.postgresql.Driver
hibernate.connection.url jdbc:postgresql://localhost:5432/hibernatedb
hibernate.connection.username postgres
hibernate.connection.password xxxxxxxx
hibernate.hbm2ddl.auto=update
show_sql=true
#Lucene
hibernate.search.default.directory_provider filesystem
hibernate.search.default.indexBase c:/var/lucene/indexes
hibernate.search.default.locking_strategy simple
运行上面的代码时出现以下异常:
2016年5月27日上午6:39:19 org.hibernate.Version logVersion 信息:HHH000412:Hibernate Core {5.1.0.Final} 2016年5月27日上午6:39:19 org.hibernate.cfg.Environment 信息:HHH000206:找不到hibernate.properties 2016年5月27日上午6:39:19 org.hibernate.cfg.Environment buildBytecodeProvider 信息:HHH000021:字节码提供程序名称:javassist 真正 2016年5月27日上午6:39:19 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity 警告:HHH90000012:已识别的过时的hibernate命名空间http://hibernate.sourceforge.net/hibernate-configuration。使用 名称空间http://www.hibernate.org/dtd/hibernate-configuration 代替。可以删除对过时的DTD / XSD命名空间的支持 随时。 2016年5月27日上午6:39:19 org.hibernate.annotations.common.reflection.java.JavaReflectionManager 信息:HCANN000001:Hibernate Commons Annotations {5.0.1.Final} 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator instantiateProxoolProvider 警告:HHH000209:遇到了proxool属性,但在类路径中找不到proxool提供程序类;这些 属性将被忽略。 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 配置 警告:HHH10001002:使用Hibernate内置连接池(不用于生产!) 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 信息:HHH10001005:在URL [jdbc:postgresql:// localhost:5432 / hibernatedb]上使用驱动程序[org.postgresql.Driver] 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 信息:HHH10001001:连接属性:{user = postgres,password = ****} 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 信息:HHH10001003:自动提交模式:false 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.connections.internal.PooledConnections 信息:HHH000115:Hibernate连接池大小:1(min = 1) 2016年5月27日上午6:39:19 org.hibernate.dialect.Dialect 信息:HHH000400:使用方言:org.hibernate.dialect.PostgreSQLDialect 2016年5月27日上午6:39:19 org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation 信息:HHH000424:禁用上下文LOB创建为createClob()方法引发错误:java.lang.reflect.InvocationTargetException 2016年5月27日上午6:39:19 org.hibernate.type.BasicTypeRegistry注册 信息:HHH000270:类型注册[java.util.UUID]覆盖上一个:org.hibernate.type.UUIDBinaryType@10aa41f2 2016年5月27日上午6:39:19 org.hibernate.envers.boot.internal.EnversServiceImpl configure 信息:启用Envers集成? :是的 2016年5月27日上午6:39:20 org.hibernate.search.engine.Version 信息:HSEARCH000034:Hibernate Search 5.5.2.Final 2016年5月27日上午6:39:20 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 停 信息:HHH10001008:清理连接池[jdbc:postgresql:// localhost:5432 / hibernatedb] 线程&#34; main&#34;中的例外情况java.lang.NoSuchMethodError:org.apache.lucene.index.IndexWriterConfig.setWriteLockTimeout(J)Lorg / apache / lucene / index / IndexWriterConfig; 在org.hibernate.search.store.spi.DirectoryHelper.initializeIndexIfNeeded(DirectoryHelper.java:57) 在org.hibernate.search.store.impl.DirectoryProviderHelper.createFSIndex(DirectoryProviderHelper.java:128) 在org.hibernate.search.store.impl.FSDirectoryProvider.initialize(FSDirectoryProvider.java:53) 在org.hibernate.search.store.spi.BaseDirectoryProviderService.initialize(BaseDirectoryProviderService.java:64) 在org.hibernate.search.store.spi.BaseDirectoryProviderService.create(BaseDirectoryProviderService.java:52) 在org.hibernate.search.indexes.spi.DirectoryBasedIndexManager.createDirectoryProvider(DirectoryBasedIndexManager.java:230) 在org.hibernate.search.indexes.spi.DirectoryBasedIndexManager.initialize(DirectoryBasedIndexManager.java:90) at org.hibernate.search.indexes.impl.IndexManagerHolder.createIndexManager(IndexManagerHolder.java:256) 在org.hibernate.search.indexes.impl.IndexManagerHolder.createIndexManager(IndexManagerHolder.java:513) 在org.hibernate.search.indexes.impl.IndexManagerHolder.createIndexManagers(IndexManagerHolder.java:482) 在org.hibernate.search.indexes.impl.IndexManagerHolder.buildEntityIndexBinding(IndexManagerHolder.java:91) 在org.hibernate.search.spi.SearchIntegratorBuilder.initDocumentBuilders(SearchIntegratorBuilder.java:358) 在org.hibernate.search.spi.SearchIntegratorBuilder.buildNewSearchFactory(SearchIntegratorBuilder.java:199) 在org.hibernate.search.spi.SearchIntegratorBuilder.buildSearchIntegrator(SearchIntegratorBuilder.java:117) 在org.hibernate.search.hcore.impl.HibernateSearchSessionFactoryObserver.sessionFactoryCreated(HibernateSearchSessionFactoryObserver.java:75) 在org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) 在org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:520) 在org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708) 在com.domain.java.HibernateTest.main(HibernateTest.java:48)
com.domain.java.TempFile文件夹是在lucene索引路径创建的,但该文件夹为空,并且上面的异常被抛出。
答案 0 :(得分:1)
真正的问题在于: org.apache.lucene.index.IndexWriterConfig.setWriteLockTimeout(J)Lorg /阿帕奇/ lucene的/索引/ IndexWriterConfig;
您有依赖性问题:您使用的Lucene版本与您正在使用的Hibernate Search版本不兼容。
检查您的依赖项并修复它们,您的问题应该消失。