[hibernate-commits] Hibernate SVN: r11703 - in trunk/HibernateExt/search: src/java/org/hibernate/search and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Jun 22 19:40:51 EDT 2007


Author: epbernard
Date: 2007-06-22 19:40:50 -0400 (Fri, 22 Jun 2007)
New Revision: 11703

Added:
   trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/
   trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextEntityManager.java
   trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextQuery.java
   trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/impl/
   trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java
Modified:
   trunk/HibernateExt/search/build.xml
   trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java
Log:
HSEARCH-89 JPA wrappers implementation

Modified: trunk/HibernateExt/search/build.xml
===================================================================
--- trunk/HibernateExt/search/build.xml	2007-06-22 20:58:27 UTC (rev 11702)
+++ trunk/HibernateExt/search/build.xml	2007-06-22 23:40:50 UTC (rev 11703)
@@ -29,8 +29,10 @@
 			  value="${basedir}/../annotations/target/hibernate-annotations/hibernate-annotations.jar"/>
 	<property name="commons-annotations.jar"
 			  value="${basedir}/../commons-annotations/target/hibernate-commons-annotations/hibernate-commons-annotations.jar"/>
+    <property name="entitymanager.jar"
+			  value="${basedir}/../entitymanager/target/hibernate-entitymanager/hibernate-entitymanager.jar"/>
 
-	<import file="${common.dir}/common-build.xml"/>
+    <import file="${common.dir}/common-build.xml"/>
 
     
     <property name="build.testresources.dir" value="${build.dir}/testresources"/>
@@ -76,7 +78,8 @@
         <pathelement location="${src.dir}"/>
 		<pathelement location="${test.dir}"/>
 		<pathelement location="${annotations.jar}"/>
-		<fileset dir="${jdbc.dir}">
+        <pathelement location="${entitymanager.jar}"/>
+        <fileset dir="${jdbc.dir}">
 			<include name="*.jar"/>
 			<include name="*.zip"/>
 		</fileset>

Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java	2007-06-22 20:58:27 UTC (rev 11702)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextQuery.java	2007-06-22 23:40:50 UTC (rev 11703)
@@ -9,6 +9,7 @@
  * The base interface for lucene powered searches.
  *
  * @author Hardy Ferentschik
+ * @author Emmanuel Bernard
  */
 //TODO return FullTextQuery rather than Query in useful chain methods
 public interface FullTextQuery extends Query {

Added: trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextEntityManager.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextEntityManager.java	                        (rev 0)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextEntityManager.java	2007-06-22 23:40:50 UTC (rev 11703)
@@ -0,0 +1,32 @@
+//$Id$
+package org.hibernate.search.jpa;
+
+import javax.persistence.EntityManager;
+
+import org.hibernate.search.SearchFactory;
+
+/**
+ * Extends an EntityManager with Full-Text operations
+ *
+ * @author Emmanuel Bernard
+ */
+public interface FullTextEntityManager extends EntityManager {
+	/**
+	 * Create a Query on top of a native Lucene Query returning the matching objects
+	 * of type <code>entities</code> and their respective subclasses.
+	 * If no entity is provided, no type filtering is done.
+	 */
+	FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities);
+
+	/**
+	 * Force the (re)indexing of a given <b>managed</b> object.
+	 * Indexation is batched per transaction
+	 */
+	void index(Object entity);
+
+	/**
+	 * return the SearchFactory
+	 */
+	SearchFactory getSearchFactory();
+
+}

Added: trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextQuery.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextQuery.java	                        (rev 0)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/FullTextQuery.java	2007-06-22 23:40:50 UTC (rev 11703)
@@ -0,0 +1,65 @@
+//$Id$
+package org.hibernate.search.jpa;
+
+import javax.persistence.Query;
+
+import org.apache.lucene.search.Sort;
+import org.hibernate.Criteria;
+
+/**
+ * The base interface for lucene powered searches.
+ * This extends the JPA Query interface
+ *
+ * @author Hardy Ferentschik
+ * @author Emmanuel Bernard
+ */
+//TODO return FullTextQuery rather than Query in useful chain methods
+public interface FullTextQuery extends Query {
+	//todo Determine what other lucene specific features to expose via this
+	// interface. Maybe we should give access to the underlying lucene hit
+	// objects?
+
+	/**
+	 * Allows to let lucene sort the results. This is useful when you have
+	 * additional sort requirements on top of the default lucene ranking.
+	 * Without lucene sorting you would have to retrieve the full result set and
+	 * order the hibernate objects.
+	 *
+	 * @param sort The lucene sort object.
+	 * @return this for method chaining
+	 */
+	FullTextQuery setSort(Sort sort);
+
+	/**
+	 * Returns the number of hits for this search
+	 *
+	 * Caution:
+	 * The number of results might be slightly different from
+	 * <code>list().size()</code> because list() if the index is
+	 * not in sync with the database at the time of query.
+	 */
+	int getResultSize();
+
+	/**
+	 * Defines the Database Query used to load the Lucene results.
+	 * Useful to load a given object graph by refining the fetch modes
+	 *
+	 * No projection (criteria.setProjection() ) allowed, the root entity must be the only returned type
+	 * No where restriction can be defined either.
+	 *
+	 */
+	FullTextQuery setCriteriaQuery(Criteria criteria);
+
+	/**
+	 * Defines the Lucene field names projected and returned in a query result
+	 * Each field is converted back to it's object representation, an Object[] being returned for each "row"
+	 * (similar to an HQL or a Criteria API projection).
+	 *
+	 * A projectable field must be stored in the Lucene index and use a {@link org.hibernate.search.bridge.TwoWayFieldBridge}
+	 * Unless notified in their JavaDoc, all built-in bridges are two-way. All @DocumentId fields are projectable by design.
+	 *
+	 * If the projected field is not a projectable field, null is returned in the object[]
+	 *
+	 */
+	FullTextQuery setIndexProjection(String... fields);
+}

Added: trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java	                        (rev 0)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java	2007-06-22 23:40:50 UTC (rev 11703)
@@ -0,0 +1,152 @@
+//$Id$
+package org.hibernate.search.jpa.impl;
+
+import javax.persistence.EntityManager;
+import javax.persistence.FlushModeType;
+import javax.persistence.LockModeType;
+import javax.persistence.Query;
+import javax.persistence.EntityTransaction;
+
+import org.hibernate.search.jpa.FullTextEntityManager;
+import org.hibernate.search.jpa.FullTextQuery;
+import org.hibernate.search.SearchFactory;
+import org.hibernate.search.SearchException;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.Session;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class FullTextEntityManagerImpl implements FullTextEntityManager {
+	private EntityManager em;
+	private FullTextSession ftSession;
+
+	public FullTextEntityManagerImpl(EntityManager em) {
+		this.em = em;
+		Object delegate = em.getDelegate();
+		if ( delegate == null ) {
+			throw new SearchException("Trying to use Hibernate Search without an Hibernate EntityManager (no delegate)");
+		}
+		else if ( Session.class.isAssignableFrom( delegate.getClass() ) ) {
+			ftSession = Search.createFullTextSession( (Session) delegate );
+		}
+		else if ( EntityManager.class.isAssignableFrom( delegate.getClass() ) ) {
+			//Some app servers wrap the EM twice
+			delegate = ( (EntityManager) delegate).getDelegate();
+			if ( delegate == null ) {
+				throw new SearchException("Trying to use Hibernate Search without an Hibernate EntityManager (no delegate)");
+			}
+			else if ( Session.class.isAssignableFrom( delegate.getClass() ) ) {
+				ftSession = Search.createFullTextSession( (Session) delegate );
+			}
+			else {
+				throw new SearchException("Trying to use Hibernate Search without an Hibernate EntityManager: " + delegate.getClass() );
+			}
+		}
+		else {
+			throw new SearchException("Trying to use Hibernate Search without an Hibernate EntityManager: " + delegate.getClass() );
+		}
+	}
+
+	public FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities) {
+		return new FullTextQueryImpl( ftSession.createFullTextQuery( luceneQuery, entities ), ftSession );
+	}
+
+	public void index(Object entity) {
+		ftSession.index( entity );
+	}
+
+	public SearchFactory getSearchFactory() {
+		return ftSession.getSearchFactory();
+	}
+
+
+	public void persist(Object entity) {
+		em.persist( entity );
+	}
+
+	public <T> T merge(T entity) {
+		return em.merge( entity );
+	}
+
+	public void remove(Object entity) {
+		em.remove( entity );
+	}
+
+	public <T> T find(Class<T> entityClass, Object primaryKey) {
+		return em.find( entityClass, primaryKey );
+	}
+
+	public <T> T getReference(Class<T> entityClass, Object primaryKey) {
+		return em.getReference( entityClass, primaryKey );
+	}
+
+	public void flush() {
+		em.flush();
+	}
+
+	public void setFlushMode(FlushModeType flushMode) {
+		em.setFlushMode( flushMode );
+	}
+
+	public FlushModeType getFlushMode() {
+		return em.getFlushMode();
+	}
+
+	public void lock(Object entity, LockModeType lockMode) {
+		em.lock( entity, lockMode );
+	}
+
+	public void refresh(Object entity) {
+		em.refresh( entity );
+	}
+
+	public void clear() {
+		em.clear();
+	}
+
+	public boolean contains(Object entity) {
+		return em.contains( entity );
+	}
+
+	public Query createQuery(String ejbqlString) {
+		return em.createQuery( ejbqlString );
+	}
+
+	public Query createNamedQuery(String name) {
+		return em.createNamedQuery( name );
+	}
+
+	public Query createNativeQuery(String sqlString) {
+		return em.createNativeQuery( sqlString );
+	}
+
+	public Query createNativeQuery(String sqlString, Class resultClass) {
+		return em.createNativeQuery( sqlString, resultClass );
+	}
+
+	public Query createNativeQuery(String sqlString, String resultSetMapping) {
+		return em.createNativeQuery( sqlString, resultSetMapping );
+	}
+
+	public void joinTransaction() {
+		em.joinTransaction();
+	}
+
+	public Object getDelegate() {
+		return em.getDelegate();
+	}
+
+	public void close() {
+		em.close();
+	}
+
+	public boolean isOpen() {
+		return em.isOpen();
+	}
+
+	public EntityTransaction getTransaction() {
+		return em.getTransaction();
+	}
+}




More information about the hibernate-commits mailing list