[hibernate-commits] Hibernate SVN: r14268 - in search/trunk/src: test/org/hibernate/search/test/query and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jan 7 08:43:04 EST 2008


Author: epbernard
Date: 2008-01-07 08:43:04 -0500 (Mon, 07 Jan 2008)
New Revision: 14268

Added:
   search/trunk/src/java/org/hibernate/search/engine/LoaderHelper.java
   search/trunk/src/test/org/hibernate/search/test/query/ObjectLoaderTest.java
Modified:
   search/trunk/src/java/org/hibernate/search/engine/ObjectLoader.java
   search/trunk/src/java/org/hibernate/search/engine/QueryLoader.java
   search/trunk/src/test/org/hibernate/search/test/query/Author.java
   search/trunk/src/test/org/hibernate/search/test/query/QueryLoaderTest.java
Log:
HSEARCH-146 ignore JPA EnitytNotFoundException

Added: search/trunk/src/java/org/hibernate/search/engine/LoaderHelper.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/engine/LoaderHelper.java	                        (rev 0)
+++ search/trunk/src/java/org/hibernate/search/engine/LoaderHelper.java	2008-01-07 13:43:04 UTC (rev 14268)
@@ -0,0 +1,46 @@
+//$
+package org.hibernate.search.engine;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hibernate.annotations.common.util.ReflectHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public abstract class LoaderHelper {
+	private static final List<Class> objectNotFoundExceptions;
+
+	static {
+		objectNotFoundExceptions = new ArrayList<Class>(2);
+		try {
+			objectNotFoundExceptions.add(
+					ReflectHelper.classForName( "org.hibernate.ObjectNotFoundException" )
+			);
+		}
+		catch (ClassNotFoundException e) {
+			//leave it alone
+		}
+		try {
+			objectNotFoundExceptions.add(
+					ReflectHelper.classForName( "javax.persistence.EntityNotFoundException" )
+			);
+		}
+		catch (ClassNotFoundException e) {
+			//leave it alone
+		}
+	}
+
+	public static boolean isObjectNotFoundException(RuntimeException e) {
+		boolean objectNotFound = false;
+		Class exceptionClass = e.getClass();
+		for ( Class clazz : objectNotFoundExceptions) {
+			if ( clazz.isAssignableFrom( exceptionClass ) ) {
+				objectNotFound = true;
+				break;
+			}
+		}
+		return objectNotFound;
+	}
+}

Modified: search/trunk/src/java/org/hibernate/search/engine/ObjectLoader.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/engine/ObjectLoader.java	2008-01-03 14:43:19 UTC (rev 14267)
+++ search/trunk/src/java/org/hibernate/search/engine/ObjectLoader.java	2008-01-07 13:43:04 UTC (rev 14268)
@@ -7,9 +7,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.Hibernate;
-import org.hibernate.ObjectNotFoundException;
 import org.hibernate.Session;
-import org.hibernate.search.engine.EntityInfo;
 
 /**
  * @author Emmanuel Bernard
@@ -28,10 +26,15 @@
 		try {
 			Hibernate.initialize( maybeProxy );
 		}
-		catch (ObjectNotFoundException e) {
-			log.debug( "Object found in Search index but not in database: "
-					+ e.getEntityName() + " wih id " + e.getIdentifier() );
-			maybeProxy = null;
+		catch (RuntimeException e) {
+			if ( LoaderHelper.isObjectNotFoundException( e ) ) {
+				log.debug( "Object found in Search index but not in database: "
+						+ entityInfo.clazz + " wih id " + entityInfo.id );
+				maybeProxy = null;
+			}
+			else {
+				throw e;
+			}
 		}
 		return maybeProxy;
 	}
@@ -42,16 +45,21 @@
 		for (EntityInfo entityInfo : entityInfos) {
 			session.load( entityInfo.clazz, entityInfo.id );
 		}
-		List result = new ArrayList(entityInfos.length);
+		List result = new ArrayList( entityInfos.length );
 		for (EntityInfo entityInfo : entityInfos) {
 			try {
 				Object entity = session.load( entityInfo.clazz, entityInfo.id );
 				Hibernate.initialize( entity );
 				result.add( entity );
 			}
-			catch (ObjectNotFoundException e) {
-				log.debug( "Object found in Search index but not in database: "
-						+ e.getEntityName() + " wih id " + e.getIdentifier() );
+			catch (RuntimeException e) {
+				if ( LoaderHelper.isObjectNotFoundException( e ) ) {
+					log.debug( "Object found in Search index but not in database: "
+							+ entityInfo.clazz + " wih id " + entityInfo.id );
+				}
+				else {
+					throw e;
+				}
 			}
 		}
 		return result;

Modified: search/trunk/src/java/org/hibernate/search/engine/QueryLoader.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/engine/QueryLoader.java	2008-01-03 14:43:19 UTC (rev 14267)
+++ search/trunk/src/java/org/hibernate/search/engine/QueryLoader.java	2008-01-07 13:43:04 UTC (rev 14268)
@@ -4,24 +4,22 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hibernate.Criteria;
+import org.hibernate.Hibernate;
 import org.hibernate.Session;
-import org.hibernate.Hibernate;
-import org.hibernate.ObjectNotFoundException;
 import org.hibernate.annotations.common.AssertionFailure;
 import org.hibernate.criterion.Disjunction;
 import org.hibernate.criterion.Restrictions;
-import org.hibernate.search.engine.EntityInfo;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 /**
  * @author Emmanuel Bernard
  */
 public class QueryLoader implements Loader {
 	private static final int MAX_IN_CLAUSE = 500;
-	private static final List EMPTY_LIST = new ArrayList(0);
-	private static Log log  = LogFactory.getLog( QueryLoader.class );
+	private static final List EMPTY_LIST = new ArrayList( 0 );
+	private static Log log = LogFactory.getLog( QueryLoader.class );
 
 	private Session session;
 	private Class entityType;
@@ -38,40 +36,44 @@
 	}
 
 
-
 	public Object load(EntityInfo entityInfo) {
 		//be sure to get an initialized object
 		Object maybeProxy = session.get( entityInfo.clazz, entityInfo.id );
 		try {
 			Hibernate.initialize( maybeProxy );
 		}
-		catch (ObjectNotFoundException e) {
-			log.debug( "Object found in Search index but not in database: "
-					+ e.getEntityName() + " wih id " + e.getIdentifier() );
-			maybeProxy = null;
+		catch (RuntimeException e) {
+			if ( LoaderHelper.isObjectNotFoundException( e ) ) {
+				log.debug( "Object found in Search index but not in database: "
+						+ entityInfo.clazz + " wih id " + entityInfo.id );
+				maybeProxy = null;
+			}
+			else {
+				throw e;
+			}
 		}
 		return maybeProxy;
 	}
 
 	public List load(EntityInfo... entityInfos) {
 		final int maxResults = entityInfos.length;
-		if ( maxResults == 0) return EMPTY_LIST;
-		if (entityType == null) throw new AssertionFailure("EntityType not defined");
-		if (criteria == null) criteria = session.createCriteria( entityType );
+		if ( maxResults == 0 ) return EMPTY_LIST;
+		if ( entityType == null ) throw new AssertionFailure( "EntityType not defined" );
+		if ( criteria == null ) criteria = session.createCriteria( entityType );
 
 		DocumentBuilder builder = searchFactoryImplementor.getDocumentBuilders().get( entityType );
 		String idName = builder.getIdentifierName();
 		int loop = maxResults / MAX_IN_CLAUSE;
 		boolean exact = maxResults % MAX_IN_CLAUSE == 0;
-		if (!exact) loop++;
+		if ( !exact ) loop++;
 		Disjunction disjunction = Restrictions.disjunction();
-		for (int index = 0 ; index < loop ; index++) {
-			int max = index*MAX_IN_CLAUSE + MAX_IN_CLAUSE <= maxResults ?
-					index*MAX_IN_CLAUSE + MAX_IN_CLAUSE :
+		for (int index = 0; index < loop; index++) {
+			int max = index * MAX_IN_CLAUSE + MAX_IN_CLAUSE <= maxResults ?
+					index * MAX_IN_CLAUSE + MAX_IN_CLAUSE :
 					maxResults;
-			List ids = new ArrayList(max - index*MAX_IN_CLAUSE);
-			for (int entityInfoIndex = index * MAX_IN_CLAUSE ; entityInfoIndex < max ; entityInfoIndex++) {
-				ids.add(entityInfos[entityInfoIndex].id);
+			List ids = new ArrayList( max - index * MAX_IN_CLAUSE );
+			for (int entityInfoIndex = index * MAX_IN_CLAUSE; entityInfoIndex < max; entityInfoIndex++) {
+				ids.add( entityInfos[entityInfoIndex].id );
 			}
 			disjunction.add( Restrictions.in( idName, ids ) );
 		}
@@ -79,7 +81,7 @@
 		criteria.list(); //load all objects
 
 		//mandatory to keep the same ordering
-		List result = new ArrayList(entityInfos.length);
+		List result = new ArrayList( entityInfos.length );
 		for (EntityInfo entityInfo : entityInfos) {
 			Object element = session.load( entityInfo.clazz, entityInfo.id );
 			if ( Hibernate.isInitialized( element ) ) {

Modified: search/trunk/src/test/org/hibernate/search/test/query/Author.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/query/Author.java	2008-01-03 14:43:19 UTC (rev 14267)
+++ search/trunk/src/test/org/hibernate/search/test/query/Author.java	2008-01-07 13:43:04 UTC (rev 14268)
@@ -2,19 +2,23 @@
 package org.hibernate.search.test.query;
 
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
-import javax.persistence.GeneratedValue;
 
+import org.hibernate.search.annotations.DocumentId;
 import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
 import org.hibernate.search.annotations.Store;
-import org.hibernate.search.annotations.Index;
 
 /**
  * @author Emmanuel Bernard
  */
 @Entity
+ at Indexed
 public class Author {
-	@Id @GeneratedValue private Integer id;
+	@Id @GeneratedValue @DocumentId
+	private Integer id;
 	private String name;
 
 	public Integer getId() {

Added: search/trunk/src/test/org/hibernate/search/test/query/ObjectLoaderTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/query/ObjectLoaderTest.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/query/ObjectLoaderTest.java	2008-01-07 13:43:04 UTC (rev 14268)
@@ -0,0 +1,56 @@
+//$
+package org.hibernate.search.test.query;
+
+import java.sql.Statement;
+import java.util.List;
+
+import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.FullTextQuery;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.analysis.KeywordAnalyzer;
+import org.apache.lucene.search.Query;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class ObjectLoaderTest extends SearchTestCase {
+
+	public void testObjectNotFound() throws Exception {
+		Session sess = openSession();
+		Transaction tx = sess.beginTransaction();
+		Author author = new Author();
+		author.setName( "Moo Cow" );
+		sess.persist( author );
+
+		tx.commit();
+		sess.clear();
+		Statement statement = sess.connection().createStatement();
+		statement.executeUpdate( "DELETE FROM Author" );
+		statement.close();
+		FullTextSession s = Search.createFullTextSession( sess );
+		tx = s.beginTransaction();
+		QueryParser parser = new QueryParser( "title", new KeywordAnalyzer() );
+		Query query = parser.parse( "name:moo" );
+		FullTextQuery hibQuery = s.createFullTextQuery( query, Author.class, Music.class );
+		List result = hibQuery.list();
+		assertEquals( "Should have returned no author", 0, result.size() );
+
+		for (Object o : s.createCriteria( Object.class ).list()) {
+			s.delete( o );
+		}
+
+		tx.commit();
+		s.close();
+	}
+
+	protected Class[] getMappings() {
+		return new Class[] {
+				Author.class,
+				Music.class
+		};
+	}
+}

Modified: search/trunk/src/test/org/hibernate/search/test/query/QueryLoaderTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/query/QueryLoaderTest.java	2008-01-03 14:43:19 UTC (rev 14267)
+++ search/trunk/src/test/org/hibernate/search/test/query/QueryLoaderTest.java	2008-01-07 13:43:04 UTC (rev 14268)
@@ -2,86 +2,85 @@
 package org.hibernate.search.test.query;
 
 import java.util.List;
+import java.sql.Statement;
 
-import org.hibernate.search.test.SearchTestCase;
-import org.hibernate.search.FullTextSession;
-import org.hibernate.search.FullTextQuery;
-import org.hibernate.search.Search;
-import org.hibernate.Transaction;
-import org.hibernate.Session;
+import org.apache.lucene.analysis.KeywordAnalyzer;
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.analysis.KeywordAnalyzer;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.search.FullTextQuery;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.test.SearchTestCase;
 
 /**
  * @author Emmanuel Bernard
  */
-public class
-		QueryLoaderTest extends SearchTestCase {
+public class QueryLoaderTest extends SearchTestCase {
 
-	public void testWithEagerCollectionLoad() throws Exception
-	{
+	public void testWithEagerCollectionLoad() throws Exception {
 		Session sess = openSession();
 		Transaction tx = sess.beginTransaction();
 		Music music = new Music();
-		music.setTitle("Moo Goes The Cow");
+		music.setTitle( "Moo Goes The Cow" );
 		Author author = new Author();
-		author.setName("Moo Cow");
-		music.addAuthor(author);
-		sess.persist(author);
+		author.setName( "Moo Cow" );
+		music.addAuthor( author );
+		sess.persist( author );
 		author = new Author();
-		author.setName("Another Moo Cow");
-		music.addAuthor(author);
-		sess.persist(author);
+		author.setName( "Another Moo Cow" );
+		music.addAuthor( author );
+		sess.persist( author );
 		author = new Author();
-		author.setName("A Third Moo Cow");
-		music.addAuthor(author);
-		sess.persist(author);
+		author.setName( "A Third Moo Cow" );
+		music.addAuthor( author );
+		sess.persist( author );
 		author = new Author();
-		author.setName("Random Moo Cow");
-		music.addAuthor(author);
-		sess.persist(author);
-		sess.save(music);
+		author.setName( "Random Moo Cow" );
+		music.addAuthor( author );
+		sess.persist( author );
+		sess.save( music );
 
 		Music music2 = new Music();
-		music2.setTitle("The Cow Goes Moo");
+		music2.setTitle( "The Cow Goes Moo" );
 		author = new Author();
-		author.setName("Moo Cow The First");
-		music2.addAuthor(author);
-		sess.persist(author);
+		author.setName( "Moo Cow The First" );
+		music2.addAuthor( author );
+		sess.persist( author );
 		author = new Author();
-		author.setName("Moo Cow The Second");
-		music2.addAuthor(author);
-		sess.persist(author);
+		author.setName( "Moo Cow The Second" );
+		music2.addAuthor( author );
+		sess.persist( author );
 		author = new Author();
-		author.setName("Moo Cow The Third");
-		music2.addAuthor(author);
-		sess.persist(author);
+		author.setName( "Moo Cow The Third" );
+		music2.addAuthor( author );
+		sess.persist( author );
 		author = new Author();
-		author.setName("Moo Cow The Fourth");
-		music2.addAuthor(author);
-		sess.persist(author);
-		sess.save(music2);
+		author.setName( "Moo Cow The Fourth" );
+		music2.addAuthor( author );
+		sess.persist( author );
+		sess.save( music2 );
 		tx.commit();
 		sess.clear();
 
-		FullTextSession s = Search.createFullTextSession(sess);
+		FullTextSession s = Search.createFullTextSession( sess );
 		tx = s.beginTransaction();
-		QueryParser parser = new QueryParser("title", new KeywordAnalyzer());
-		Query query = parser.parse("title:moo");
-		FullTextQuery hibQuery = s.createFullTextQuery(query, Music.class);
+		QueryParser parser = new QueryParser( "title", new KeywordAnalyzer() );
+		Query query = parser.parse( "title:moo" );
+		FullTextQuery hibQuery = s.createFullTextQuery( query, Music.class );
 		List result = hibQuery.list();
-		assertEquals("Should have returned 2 Books", 2, result.size());
+		assertEquals( "Should have returned 2 Books", 2, result.size() );
 		music = (Music) result.get( 0 );
-		assertEquals("Book 1 should have four authors", 4, music.getAuthors().size());
+		assertEquals( "Book 1 should have four authors", 4, music.getAuthors().size() );
 		music2 = (Music) result.get( 1 );
-		assertEquals("Book 2 should have four authors", 4, music2.getAuthors().size());
+		assertEquals( "Book 2 should have four authors", 4, music2.getAuthors().size() );
 
 		//cleanup
 		music.getAuthors().clear();
 		music2.getAuthors().clear();
 
-		for (Object o : s.createCriteria( Object.class ).list() ) {
+		for (Object o : s.createCriteria( Object.class ).list()) {
 			s.delete( o );
 		}
 




More information about the hibernate-commits mailing list