//$Id: ProjectionQueryTest.java 11625 2007-06-04 16:21:54Z epbernard $ package org.hibernate.search.test.query; import java.util.List; import org.apache.lucene.analysis.StopAnalyzer; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Query; import org.hibernate.Transaction; import org.hibernate.search.FullTextSession; import org.hibernate.search.Search; import org.hibernate.search.test.SearchTestCase; /** * @author Emmanuel Bernard */ public class ProjectionQueryTest extends SearchTestCase { public void testProjection() throws Exception { FullTextSession s = Search.createFullTextSession( openSession() ); Transaction tx = s.beginTransaction(); Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" ); s.save( book ); Author emmanuel = new Author(); emmanuel.setName( "Emmanuel" ); s.save( emmanuel ); book.setMainAuthor(emmanuel); tx.commit(); s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser( "title", new StopAnalyzer() ); Query query = parser.parse( "summary:Festina" ); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Book.class ); hibQuery.setIndexProjection( "id", "summary", "body", "mainAuthor.name"); List result = hibQuery.list(); assertNotNull( result ); assertEquals( "Query with no explicit criteria", 1, result.size() ); Object[] projection = (Object[]) result.get( 0 ); assertEquals( "id", 1, projection[0] ); assertEquals( "summary", "La chute de la petite reine a travers les yeux de Festina", projection[1] ); assertEquals( "body should be null (non projectable)", null, projection[2] ); assertEquals( "mainAuthor.name (embedded objects)", "Emmanuel", projection[3] ); hibQuery = s.createFullTextQuery( query, Book.class ); hibQuery.setIndexProjection(); result = hibQuery.list(); assertNotNull( result ); assertEquals( 1, result.size() ); assertTrue( "Should not trigger projection", result.get(0) instanceof Book); hibQuery = s.createFullTextQuery( query, Book.class ); hibQuery.setIndexProjection(null); result = hibQuery.list(); assertNotNull( result ); assertEquals( 1, result.size() ); assertTrue( "Should not trigger projection", result.get(0) instanceof Book); //cleanup for (Object element : s.createQuery( "from " + Book.class.getName() ).list()) s.delete( element ); for (Object element : s.createQuery( "from " + Author.class.getName() ).list()) s.delete( element ); tx.commit(); s.close(); } protected Class[] getMappings() { return new Class[] { Book.class, Author.class }; } }