首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

HibernateSearch学习二

2012-10-09 
HibernateSearch学习2Using projection instead of returning the full domain objectorg.hibernate.searc

HibernateSearch学习2
Using projection instead of returning the full domain object

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery,Book.class );query.setProjection( "id", "summary", "body", "mainAuthor.name" );List results = query.list();Object[] firstResult = (Object[]) results.get(0);Integer id = firstResult[0];String summary = firstResult[1];String body = firstResult[2];String authorName = firstResult[3];

Using ResultTransformer in conjunction(联合) with projections
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery,Book.class );query.setProjection( "title", "mainAuthor.name" );query.setResultTransformer(new StaticAliasToBeanResultTransformer( BookView.class, "title", "author" ));List<BookView> results = (List<BookView>) query.list();for(BookView view : results) {log.info( "Book: " + view.getTitle() + ", " + view.getAuthor() );}


Adding instances to the Index
Using FullTextSession.index(T entity) you can directly add or update a specific object
instance to the index. If this entity was already indexed, then the index will be updated. Changes
to the index are only applied at transaction commit
Indexing an entity via FullTextSession.index(T entity)
FullTextSession fullTextSession = Search.getFullTextSession(session);Transaction tx = fullTextSession.beginTransaction();Object customer = fullTextSession.load( Customer.class, 8 );fullTextSession.index(customer);tx.commit(); //index only updated at commit time


Deleting instances from the Index: Purging 以下清除不会影响数据库,只会对索引起效,不过他们也是具有事务性的
It is equally possible to remove an entity or all entities of a given type from a Lucene index without
the need to physically remove them from the database. This operation is named purging and is
also done through the FullTextSession.
Purging a specific instance of an entity from the index
FullTextSession fullTextSession = Search.getFullTextSession(session);Transaction tx = fullTextSession.beginTransaction();for (Customer customer : customers) {fullTextSession.purge( Customer.class, customer.getId() );}tx.commit(); //index is updated at commit time

Purging all instances of an entity from the index
FullTextSession fullTextSession = Search.getFullTextSession(session);Transaction tx = fullTextSession.beginTransaction();fullTextSession.purgeAll( Customer.class );//optionally optimize the index//fullTextSession.getSearchFactory().optimize( Customer.class );tx.commit(); //index changes are applied at commit time

热点排行