-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Search with spring-data and jpa
PostPosted: Fri Aug 08, 2014 7:16 am 
Newbie

Joined: Tue Nov 11, 2008 8:33 am
Posts: 5
In my unit tests entities are inserted into the database and the FullTextIndexEventListener.onPostInsert runs without any errors but I don't get any documents written to my index. All I get is the initial index catalog containing segments.gen and segments_1. Needless to say, my search queries return nothing.

Code:
@Indexed
@Entity
@Table(name = "patient")
public class Patient implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @DocumentId
    private Integer id;

    @Basic
    @Column(unique=true)
    @Field(store= Store.YES)
    private String personReference;
}

@Service
public class CareProcessService {

    @PersistenceContext
    private EntityManager em;
    @Resource
    private PatientRepository patientRepository;

    public List<Patient> search(){
        FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
        QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Patient.class).get();

        Query query = qb.keyword().wildcard().onField("personReference").matching("*").createQuery();
        javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Patient.class);

        List<Patient> result = persistenceQuery.getResultList();
        result = em.createQuery("from Patient").getResultList();
        result = patientRepository.findAll();
    }
}

public interface PatientRepository extends JpaRepository<Patient, Integer>, Serializable {}


However, if I query my entity manager directly (em.createQuery) I get the expected entities in return. I also get expected results when I query JpaRepository (repository.findAll). The entity manager (em) is injected with the @PersistenceContext annotation in my @Service where the above code is executed. I have tried running my setup with both hibernate-search-4.5.1 and hibernate-search-5.0.0, always with the same result.

Code:
Hibernate: insert into patient (ID, CREATED, PERSON_REFERENCE) values (null, ?, ?)
Hibernate: insert into patient (ID, CREATED, PERSON_REFERENCE) values (null, ?, ?)
[2014-08-08 13:09:22,154][DEBUG] Opening IndexReader for directoryProvider patient.Patient
[2014-08-08 13:09:22,186][DEBUG] Closing MultiReader: CacheableMultiReader [subReaders=[StandardDirectoryReader(segments_1:1407487114870)], managers=[org.hibernate.search.indexes.impl.SharingBufferReaderProvider@57ab5b2f]]
[2014-08-08 13:09:22,187][DEBUG] Closing IndexReader: StandardDirectoryReader(segments_1:1407487114870)
Hibernate: select patient0_.ID as ID1_5_, patient0_.CREATED as CREATED2_5_, patient0_.PERSON_REFERENCE as PERSON_R3_5_ from patient patient0_
Hibernate: select patient0_.ID as ID1_5_, patient0_.CREATED as CREATED2_5_, patient0_.PERSON_REFERENCE as PERSON_R3_5_ from patient patient0_
[2014-08-08 13:09:26,115][DEBUG] HSEARCH000108: Shutting down backend for IndexManager 'patient.Patient'


What have I missed?


Top
 Profile  
 
 Post subject: Re: Search with spring-data and jpa
PostPosted: Fri Aug 15, 2014 6:18 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hibernate Search only writes to the index when the transaction is committed. I'm guessing this might be your problem as it seems common among Spring users to rollback the transactions at the end of their unit tests?

Apache Lucene is not transactional like the database, so we can't safely apply the changes before the transaction is committed.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.