[hibernate-commits] Hibernate SVN: r11532 - in trunk/HibernateExt/search/src: java/org/hibernate/search/engine and 2 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Wed May 16 19:48:01 EDT 2007
Author: epbernard
Date: 2007-05-16 19:48:00 -0400 (Wed, 16 May 2007)
New Revision: 11532
Added:
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Author.java
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java
trunk/HibernateExt/search/src/java/org/hibernate/search/engine/QueryLoader.java
trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Book.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java
Log:
HSEARCH-52 support injection of the criteria query used to load the objects
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java 2007-05-16 22:25:22 UTC (rev 11531)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -3,6 +3,7 @@
import org.apache.lucene.search.Sort;
import org.hibernate.Query;
+import org.hibernate.Criteria;
/**
* The base interface for lucene powered searches.
@@ -35,4 +36,14 @@
* not in sync with the database at the time of query.
*/
int resultSize();
+
+ /**
+ * Defines the Database Query used to load the Lucene results.
+ * Useful to load a given object graph by refining the fetch modes
+ *
+ * No projection allowed, the root entity must be the only returned type
+ * No where restriction can be defined either.
+ *
+ */
+ FullTextQuery setCriteriaQuery(Criteria criteria);
}
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/engine/QueryLoader.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/engine/QueryLoader.java 2007-05-16 22:25:22 UTC (rev 11531)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/engine/QueryLoader.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -26,6 +26,7 @@
private Session session;
private Class entityType;
private SearchFactory searchFactory;
+ private Criteria criteria;
public void init(Session session, SearchFactory searchFactory) {
this.session = session;
@@ -46,7 +47,7 @@
final int maxResults = entityInfos.length;
if ( maxResults == 0) return EMPTY_LIST;
if (entityType == null) throw new AssertionFailure("EntityType not defined");
- Criteria criteria = session.createCriteria( entityType );
+ if (criteria == null) criteria = session.createCriteria( entityType );
DocumentBuilder builder = searchFactory.getDocumentBuilders().get( entityType );
String idName = builder.getIdentifierName();
@@ -80,4 +81,8 @@
}
return result;
}
+
+ public void setCriteria(Criteria criteria) {
+ this.criteria = criteria;
+ }
}
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java 2007-05-16 22:25:22 UTC (rev 11531)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -29,11 +29,16 @@
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
+import org.hibernate.Criteria;
+import org.hibernate.annotations.common.util.ReflectHelper;
+import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.query.ParameterMetadata;
import org.hibernate.impl.AbstractQueryImpl;
+import org.hibernate.impl.CriteriaImpl;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.FullTextQuery;
+import org.hibernate.search.SearchException;
import org.hibernate.search.engine.DocumentBuilder;
import org.hibernate.search.engine.DocumentExtractor;
import org.hibernate.search.engine.Loader;
@@ -58,19 +63,8 @@
private Integer maxResults;
private Integer resultSize;
private Sort sort;
- private static Loader noLoader = new Loader() {
- public void init(Session session, SearchFactory searchFactory) {
- }
+ private Criteria criteria;
- public Object load(EntityInfo entityInfo) {
- throw new UnsupportedOperationException( "noLoader should not be used" );
- }
-
- public List load(EntityInfo... entityInfos) {
- throw new UnsupportedOperationException( "noLoader should not be used" );
- }
- };
-
/**
* classes must be immutable
*/
@@ -133,12 +127,35 @@
}
private Loader getLoader(Session session, SearchFactory searchFactory) {
- if (classes.length == 1) {
+ if (criteria != null) {
+ if (classes.length > 1) throw new SearchException("Cannot mix criteria and multiple entity types");
+ if ( criteria instanceof CriteriaImpl ) {
+ String targetEntity = ( (CriteriaImpl) criteria).getEntityOrClassName();
+ if ( classes.length == 1 && !classes[0].getName().equals( targetEntity ) ) {
+ throw new SearchException("Criteria query entity should match query entity");
+ }
+ else {
+ try {
+ Class entityType = ReflectHelper.classForName( targetEntity );
+ classes = new Class[] { entityType };
+ }
+ catch (ClassNotFoundException e) {
+ throw new SearchException("Unable to load entity class from criteria: " + targetEntity, e);
+ }
+ }
+ }
QueryLoader loader = new QueryLoader();
loader.init( session, searchFactory );
loader.setEntityType( classes[0] );
+ loader.setCriteria(criteria);
return loader;
}
+ else if (classes.length == 1) {
+ QueryLoader loader = new QueryLoader();
+ loader.init( session, searchFactory );
+ loader.setEntityType( classes[0] );
+ return loader;
+ }
else {
final ObjectLoader objectLoader = new ObjectLoader();
objectLoader.init( session, searchFactory );
@@ -361,6 +378,11 @@
return this.resultSize;
}
+ public FullTextQuery setCriteriaQuery(Criteria criteria) {
+ this.criteria = criteria;
+ return this;
+ }
+
public FullTextQuery setFirstResult(int firstResult) {
this.firstResult = firstResult;
return this;
@@ -382,4 +404,17 @@
protected Map getLockModes() {
return null;
}
+
+ private static Loader noLoader = new Loader() {
+ public void init(Session session, SearchFactory searchFactory) {
+ }
+
+ public Object load(EntityInfo entityInfo) {
+ throw new UnsupportedOperationException( "noLoader should not be used" );
+ }
+
+ public List load(EntityInfo... entityInfos) {
+ throw new UnsupportedOperationException( "noLoader should not be used" );
+ }
+ };
}
Added: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Author.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Author.java (rev 0)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Author.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -0,0 +1,31 @@
+//$Id: $
+package org.hibernate.search.test.query;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Entity
+public class Author {
+ @Id @GeneratedValue private Integer id;
+ private String name;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Book.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Book.java 2007-05-16 22:25:22 UTC (rev 11531)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/Book.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -1,8 +1,11 @@
//$Id: $
package org.hibernate.search.test.query;
+import java.util.Set;
+import java.util.HashSet;
import javax.persistence.Entity;
import javax.persistence.Id;
+import javax.persistence.ManyToMany;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Text;
@@ -19,7 +22,17 @@
private Integer id;
private String body;
private String summary;
+ private Set<Author> authors = new HashSet<Author>();
+ @ManyToMany
+ public Set<Author> getAuthors() {
+ return authors;
+ }
+
+ public void setAuthors(Set<Author> authors) {
+ this.authors = authors;
+ }
+
public Book() {
}
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java 2007-05-16 22:25:22 UTC (rev 11531)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -94,7 +94,8 @@
return new Class[] {
Book.class,
AlternateBook.class,
- Clock.class
+ Clock.class,
+ Author.class
};
}
}
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java 2007-05-16 22:25:22 UTC (rev 11531)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java 2007-05-16 23:48:00 UTC (rev 11532)
@@ -11,6 +11,7 @@
import org.hibernate.Transaction;
import org.hibernate.Hibernate;
import org.hibernate.ScrollableResults;
+import org.hibernate.FetchMode;
import org.hibernate.stat.Statistics;
import org.apache.lucene.search.Query;
import org.apache.lucene.queryParser.QueryParser;
@@ -304,12 +305,52 @@
s.close();
}
+ public void testCriteria() 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.getAuthors().add(emmanuel);
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
+ Query query = parser.parse( "summary:Festina" );
+ org.hibernate.Query hibQuery = s.createFullTextQuery( query, Book.class );
+ List result = hibQuery.list();
+ assertNotNull( result );
+ assertEquals( "Query with no explicit criteria", 1, result.size() );
+ book = (Book) result.get(0);
+ assertFalse( "Association should not be inintialized", Hibernate.isInitialized( book.getAuthors() ) );
+
+ result = s.createFullTextQuery( query ).setCriteriaQuery(
+ s.createCriteria( Book.class ).setFetchMode( "authors", FetchMode.JOIN ) ).list();
+ assertNotNull( result );
+ assertEquals( "Query with explicit criteria", 1, result.size() );
+ book = (Book) result.get(0);
+ assertTrue( "Association should be inintialized", Hibernate.isInitialized( book.getAuthors() ) );
+ assertEquals( 1, book.getAuthors().size() );
+
+ //cleanup
+ Author author = book.getAuthors().iterator().next();
+ book.getAuthors().remove( author );
+
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
+ tx.commit();
+ s.close();
+ }
+
+
protected Class[] getMappings() {
return new Class[] {
Book.class,
AlternateBook.class,
- Clock.class
+ Clock.class,
+ Author.class
};
}
}
More information about the hibernate-commits
mailing list