FullTextEntityManager hibernate search" Session is closed"

时间:2014-04-04 15:42:47

标签: java spring hibernate

我正在尝试使用FullTextEntityManager(和Spring),但是获得了“Session is closed”异常。我能够第一次查询,但第二次抛出异常。这是我的配置:

@Service
@Transactional(readOnly = true, propagation=Propagation.SUPPORTS)
public class SearchServiceImpl extends BaseService implements SearchService {

    public List<StrainSearchResultsListItem> advancedSearch(Pageable page,String species) {
      return searchRepository.advancedSearch(page,   species);
   }

Repo impl:

@Repository
@Transactional(readOnly = true)
public class SearchRepositoryImpl implements SearchRepository {

  @PersistenceContext
   public void setEntityManager(EntityManager entityManager) {
      this.entityManager = entityManager;
   }

   protected FullTextEntityManager getFullTextEntityManager() {

      if (fullTextEntityManager == null) {
         fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
      }

      return fullTextEntityManager;
   }

一旦我第二次调用fullTestQuery.getResultList(),就会出现'Session is closed'异常。

FullTextQuery fullTextQuery = 
         getFullTextEntityManager()
            .createFullTextQuery(booleanQuery, Strain.class);
fullTextQuery.getResultList()

任何想法都表示赞赏。

感谢

1 个答案:

答案 0 :(得分:0)

您的实体类应该是这样的-

@Entity
@Table(name="keywordsentity")
@Indexed
@AnalyzerDef(
          name="fulltext",
          tokenizer=@TokenizerDef(factory=StandardTokenizerFactory.class),
          filters={
            @TokenFilterDef(factory=LowerCaseFilterFactory.class),
            @TokenFilterDef(factory=SnowballPorterFilterFactory.class, 
              params={@Parameter(name="language", value="English") })
          }
        )
public class Keywordsentity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @JsonProperty
    //index = Index.YES, analyze=Analyze.YES, store = Store.YES
    @Field
    @Analyzer(definition="fulltext") 
    private String keywordname;

您的服务类别应该是这样

@Service
public class KeywordService {


    @PersistenceContext(type = PersistenceContextType.EXTENDED, name = "keywordPU")
    private EntityManager em;

    private FullTextEntityManager ftem;

      public void updateFullTextIndex() throws Exception {

            getFullTextEntityManager().createIndexer().startAndWait();
        }



    protected FullTextEntityManager getFullTextEntityManager() {
        if (ftem == null) {
            ftem = Search.getFullTextEntityManager(em);
        }
        return ftem;
    }

    @Transactional
      public List<Keywordsentity> search(String summary, String description)
      {


          String searchString = summary.concat(" ").concat(description);
          System.out.println("searchString-----------------------------"+searchString);
          QueryBuilder qb = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(Keywordsentity.class).get();


    //lucene query
          org.apache.lucene.search.Query query = qb
                    .keyword()
                    .onField("keywordname").matching(searchString)
                    .createQuery();

            Query fullTextQuery = getFullTextEntityManager().createFullTextQuery(query, Keywordsentity.class);

            System.out.println("fullTextQuery------------------================="+fullTextQuery);

            List<Keywordsentity> result = new ArrayList<Keywordsentity>();
            try
            {
                 result = fullTextQuery.getResultList();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }



            System.out.println("size --------------------=========="+result.size());
            for (Keywordsentity keywordone : result) {
             System.out.println("keyword------------"+keywordone);
             Map<String,String> team =new HashMap<String,String>();
         Set<Teamsentity> teams=     keywordone.getTeamsentity();
         {
             for(Teamsentity teamsentityone :teams )
            {
                String ids = String.valueOf(teamsentityone.getId());
                team.put("id",ids);
                team.put("name",teamsentityone.getName());
                team.put("description",teamsentityone.getDescription());
            }
             System.out.println("teams =================="+teams);

             }

            }
            return result;
      }

}

然后,存储库和控制器应该是正常的方式。 仅依赖项-

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>5.10.5.Final</version>
    </dependency>