I have a small project that I want to integrate with lucene and hibernate: This is the bean file: 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;
}
}
And this the Hibernate file:
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();
}
}
}
}
}
The configuration files:
<?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>
The property file
######################
### 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
I am getting following exception running the above code:
May 27, 2016 6:39:19 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.1.0.Final} May 27, 2016 6:39:19 AM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found May 27, 2016 6:39:19 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist true May 27, 2016 6:39:19 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. May 27, 2016 6:39:19 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator instantiateProxoolProvider WARN: HHH000209: proxool properties were encountered, but the proxool provider class was not found on the classpath; these properties are going to be ignored. May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb] May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=postgres, password=****} May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections INFO: HHH000115: Hibernate connection pool size: 1 (min=1) May 27, 2016 6:39:19 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect May 27, 2016 6:39:19 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException May 27, 2016 6:39:19 AM org.hibernate.type.BasicTypeRegistry register INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@10aa41f2 May 27, 2016 6:39:19 AM org.hibernate.envers.boot.internal.EnversServiceImpl configure INFO: Envers integration enabled? : true May 27, 2016 6:39:20 AM org.hibernate.search.engine.Version INFO: HSEARCH000034: Hibernate Search 5.5.2.Final May 27, 2016 6:39:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/hibernatedb] Exception in thread "main" java.lang.NoSuchMethodError: org.apache.lucene.index.IndexWriterConfig.setWriteLockTimeout(J)Lorg/apache/lucene/index/IndexWriterConfig; at org.hibernate.search.store.spi.DirectoryHelper.initializeIndexIfNeeded(DirectoryHelper.java:57) at org.hibernate.search.store.impl.DirectoryProviderHelper.createFSIndex(DirectoryProviderHelper.java:128) at org.hibernate.search.store.impl.FSDirectoryProvider.initialize(FSDirectoryProvider.java:53) at org.hibernate.search.store.spi.BaseDirectoryProviderService.initialize(BaseDirectoryProviderService.java:64) at org.hibernate.search.store.spi.BaseDirectoryProviderService.create(BaseDirectoryProviderService.java:52) at org.hibernate.search.indexes.spi.DirectoryBasedIndexManager.createDirectoryProvider(DirectoryBasedIndexManager.java:230) at org.hibernate.search.indexes.spi.DirectoryBasedIndexManager.initialize(DirectoryBasedIndexManager.java:90) at org.hibernate.search.indexes.impl.IndexManagerHolder.createIndexManager(IndexManagerHolder.java:256) at org.hibernate.search.indexes.impl.IndexManagerHolder.createIndexManager(IndexManagerHolder.java:513) at org.hibernate.search.indexes.impl.IndexManagerHolder.createIndexManagers(IndexManagerHolder.java:482) at org.hibernate.search.indexes.impl.IndexManagerHolder.buildEntityIndexBinding(IndexManagerHolder.java:91) at org.hibernate.search.spi.SearchIntegratorBuilder.initDocumentBuilders(SearchIntegratorBuilder.java:358) at org.hibernate.search.spi.SearchIntegratorBuilder.buildNewSearchFactory(SearchIntegratorBuilder.java:199) at org.hibernate.search.spi.SearchIntegratorBuilder.buildSearchIntegrator(SearchIntegratorBuilder.java:117) at org.hibernate.search.hcore.impl.HibernateSearchSessionFactoryObserver.sessionFactoryCreated(HibernateSearchSessionFactoryObserver.java:75) at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:520) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708) at com.domain.java.HibernateTest.main(HibernateTest.java:48)
The com.domain.java.TempFile folder is created at the lucene index path but the folder is empty and the above exception is trowed.