Hello everybody!
I have a Spring and hibernate/JPA Application which works well.
I started to integrate Hibernate search a few days ago, but I'm a beginner so it's hard.
Here are some .jar I use for this app:
hibernate3.jar
hibernate-annotations.jar
hibernate-entitymanager-3.2.1.ga.jar
hibernate-entitymanager.jar
spring-2.5.6.jar
lucene-core-3.1.0.jar
hibernate-core-3.6.4.Final.jar
hibernate-search-3.4.0.Final.jar
hibernate-search-infinispan-3.4.0.Final.jar
I checked the directory I indicated for HibSearch indexes but there is nothing, and I get these errors:
[color=#0000FF]java.lang.NoClassDefFoundError: javax/persistence/LockTimeoutException
at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.createFullTextQuery(FullTextEntityManagerImpl.java:101)
at dao.Dao.filterPhotoWithArticle(Dao.java:279)
at service.Service.filterPhotoWithArticle(Service.java:320)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
[/color]
Here are my spring.xml, persistence.xml, my class (in which I want to use HibSearch) and my function using HibSearch:
Spring.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- couches applicatives -->
<bean id="dao" class="dao.Dao" />
<bean id="service" class="service.Service">
<property name="dao" ref="dao" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--
<property name="showSql" value="true" />
-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="generateDdl" value="true" />
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- la source de donnéees DBCP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="jpa" />
<property name="password" value="" />
</bean>
<!-- le gestionnaire de transactions -->
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- traduction des exceptions -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- persistence -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
persistence.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<!-- HSearch settings -->
<property name="hibernate.search.default.indexBase" value="/Users/Amine%20NABIL/workspace/article" />
<property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" />
<!--<property name="post-update" value="org.hibernate.search.event.FullTextIndexEventListener"/>
<property name="post-insert" value="org.hibernate.search.event.FullTextIndexEventListener"/>
<property name="post-delete" value="org.hibernate.search.event.FullTextIndexEventListener"/>
</properties>-->
</persistence-unit>
<!--<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL"/>-->
</persistence>
Classe:
Code:
package entites;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.solr.analysis.PhoneticFilterFactory;
import org.apache.solr.analysis.StandardTokenizerFactory;
import org.apache.solr.analysis.StopFilterFactory;
import org.apache.solr.analysis.SynonymFilterFactory;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Parameter;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;
import org.hibernate.search.annotations.Boost;
@SuppressWarnings( { "unused", "serial" })
@Entity
@Indexed
@Table(name="article")
@Analyzer(impl=StandardAnalyzer.class)
public class Article implements Serializable {
@Id
@DocumentId
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "DATE_MODIF", nullable = true)
@Temporal(TemporalType.TIMESTAMP)// TIMESTAMP pour coder une date avec heure
private Date dateModif;
@Column(name = "COMMENTAIRE", length = 1000, nullable = false)
//@org.hibernate.annotations.Index(name = "commentaireIndex")
@Field(index=Index.TOKENIZED, store=Store.YES)
private String commentaire;
@Column(nullable = true)
@Version
private int version;
//@OneToOne(mappedBy = "article")
//private Photo photo;
// relation inverse Article -> Photo
//@ManyToMany(mappedBy = "articles")
//private Set<Photo> photos = new HashSet<Photo>();
// relation principale Article (many) -> Category (one)
// implémentée par une clé étrangère (categorie_id) dans Article
// 1 Article a nécessairement 1 Categorie (nullable=false)
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "ID_PHOTO", nullable = false)
@IndexedEmbedded
private Photo photo;
//Constructors
public Article() {
}
public Article(int id, String commentaire, int version, Date dateModif) {
this.id = id;
this.dateModif = dateModif;
this.commentaire = commentaire;
this.version = version;
}
public Article(String commentaire, int version, Date dateModif) {
this.dateModif = dateModif;
this.commentaire = commentaire;
this.version = version;
}
public Article(String commentaire, Date dateModif) {
this.dateModif = dateModif;
this.commentaire = commentaire;
}
public Article(String commentaire) {
this.commentaire = commentaire;
}
//getters and setters
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the dateModif
*/
public Date getDateModif() {
return dateModif;
}
/**
* @return the commentaire
*/
public String getCommentaire() {
return commentaire;
}
/**
* @param dateModif the dateModif to set
*/
public void setDate_modif(Date dateModif) {
this.dateModif = dateModif;
}
/**
* @param commentaire the commentaire to set
*/
public void setCommentaire(String commentaire) {
this.commentaire = commentaire;
}
public Photo getPhoto() {
return photo;
}
public void setPhoto(Photo photo) {
this.photo = photo;
}
}
and finally the code to release a search:
Code:
package dao;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.util.Version;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.jpa.impl.FullTextEntityManagerImpl;
import org.slf4j.impl.StaticLoggerBinder;
import entites.Photo;
import entites.Tag;
import entites.Article;
import entites.PhotoTag;
//@SuppressWarnings("unused")
public class Dao implements IDao {
@PersistenceContext
private EntityManager em;
FullTextEntityManager fullTextEntityManager;
public static final Version v = Version.valueOf("LUCENE_31");
@SuppressWarnings("unchecked")
public List<Article> filterPhotoWithArticle(String filter) {
try {
String fields = new String("Article.commentaire");
//Récupération d'un entitymanager compatible hibernate search
fullTextEntityManager = Search.getFullTextEntityManager(em);
org.apache.lucene.search.Query standardQuery = null;
QueryParser parser = new QueryParser(v, fields, new StandardAnalyzer(v));
try {
standardQuery = parser.parse(filter);
} catch (ParseException ex) {
Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
}
FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(standardQuery, Article.class);
return persistenceQuery.getResultList();
} finally {
em.close();
}
}
I hope someone can help me! I really don't see what's wrong :(
Thank you for your time!