[hibernate-commits] Hibernate SVN: r11552 - in trunk/HibernateExt/search/src: test/org/hibernate/search/test/embedded and 1 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Wed May 23 18:51:41 EDT 2007
Author: epbernard
Date: 2007-05-23 18:51:40 -0400 (Wed, 23 May 2007)
New Revision: 11552
Added:
trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Categorie.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Entite.java
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/engine/DocumentBuilder.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/MassIndexTest.java
Log:
HSEARCH-59 unproxy indexedEmbedded objects before indexing them
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/engine/DocumentBuilder.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/engine/DocumentBuilder.java 2007-05-22 19:28:04 UTC (rev 11551)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/engine/DocumentBuilder.java 2007-05-23 22:51:40 UTC (rev 11552)
@@ -19,6 +19,7 @@
import org.apache.lucene.document.Field;
import org.apache.lucene.index.Term;
import org.hibernate.Hibernate;
+import org.hibernate.proxy.HibernateProxy;
import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XAnnotatedElement;
@@ -424,10 +425,11 @@
private void buildDocumentFields(Object instance, Document doc, PropertiesMetadata propertiesMetadata) {
if (instance == null) return;
-
+ //needed for field access: I cannot work in the proxied version
+ Object unproxiedInstance = unproxy( instance );
for ( int i = 0; i < propertiesMetadata.keywordNames.size(); i++ ) {
XMember member = propertiesMetadata.keywordGetters.get( i );
- Object value = getMemberValue( instance, member );
+ Object value = getMemberValue( unproxiedInstance, member );
propertiesMetadata.keywordBridges.get( i ).set(
propertiesMetadata.keywordNames.get( i ), value, doc, Field.Store.YES,
Field.Index.UN_TOKENIZED, getBoost( member )
@@ -435,7 +437,7 @@
}
for ( int i = 0; i < propertiesMetadata.textNames.size(); i++ ) {
XMember member = propertiesMetadata.textGetters.get( i );
- Object value = getMemberValue( instance, member );
+ Object value = getMemberValue( unproxiedInstance, member );
propertiesMetadata.textBridges.get( i ).set(
propertiesMetadata.textNames.get( i ), value, doc, Field.Store.YES,
Field.Index.TOKENIZED, getBoost( member )
@@ -443,7 +445,7 @@
}
for ( int i = 0; i < propertiesMetadata.unstoredNames.size(); i++ ) {
XMember member = propertiesMetadata.unstoredGetters.get( i );
- Object value = getMemberValue( instance, member );
+ Object value = getMemberValue( unproxiedInstance, member );
propertiesMetadata.unstoredBridges.get( i ).set(
propertiesMetadata.unstoredNames.get( i ), value, doc, Field.Store.NO,
Field.Index.TOKENIZED, getBoost( member )
@@ -451,7 +453,7 @@
}
for ( int i = 0; i < propertiesMetadata.fieldNames.size(); i++ ) {
XMember member = propertiesMetadata.fieldGetters.get( i );
- Object value = getMemberValue( instance, member );
+ Object value = getMemberValue( unproxiedInstance, member );
propertiesMetadata.fieldBridges.get( i ).set(
propertiesMetadata.fieldNames.get( i ), value, doc, propertiesMetadata.fieldStore.get( i ),
propertiesMetadata.fieldIndex.get( i ), getBoost( member )
@@ -459,12 +461,10 @@
}
for ( int i = 0; i < propertiesMetadata.embeddedGetters.size(); i++ ) {
XMember member = propertiesMetadata.embeddedGetters.get( i );
- Object value = getMemberValue( instance, member );
- //if ( ! Hibernate.isInitialized( value ) ) continue; //this sounds like a bad idea
+ Object value = getMemberValue( unproxiedInstance, member );
//TODO handle boost at embedded level: already stored in propertiesMedatada.boost
if (value == null) continue;
-
PropertiesMetadata embeddedMetadata = propertiesMetadata.embeddedPropertiesMetadata.get( i );
switch( propertiesMetadata.embeddedContainers.get(i) ) {
case ARRAY:
@@ -492,6 +492,15 @@
}
}
+ private Object unproxy(Object value) {
+ //FIXME this service should be part of Core?
+ if ( value instanceof HibernateProxy ) {
+ value = ( ( HibernateProxy ) value ).getHibernateLazyInitializer()
+ .getImplementation();
+ }
+ return value;
+ }
+
public Term getTerm(Serializable id) {
return new Term( idKeywordName, idBridge.objectToString( id ) );
}
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java 2007-05-22 19:28:04 UTC (rev 11551)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java 2007-05-23 22:51:40 UTC (rev 11552)
@@ -139,13 +139,15 @@
}
- public void testIndexedEmbeddedAndCollections() throws Exception {
+ public void testIndexedEmbeddedAndCollectionsFailureExpected() throws Exception {
Author a = new Author();
a.setName( "Voltaire" );
Author a2 = new Author();
a2.setName( "Victor Hugo" );
Author a3 = new Author();
a3.setName( "Moliere" );
+ Author a4 = new Author();
+ a4.setName( "Proust" );
Order o = new Order();
o.setOrderNumber( "ACVBNM");
@@ -170,6 +172,7 @@
s.persist( a );
s.persist( a2 );
s.persist( a3 );
+ s.persist( a4 );
s.persist( o );
s.persist( o2 );
s.persist( p1 );
@@ -179,7 +182,9 @@
s.clear();
FullTextSession session = Search.createFullTextSession(s);
- QueryParser parser = new MultiFieldQueryParser( new String[] {"name", "authors.name"}, new StandardAnalyzer() );
+ tx = session.beginTransaction();
+
+ QueryParser parser = new MultiFieldQueryParser( new String[] {"name", "authors.name"}, new StandardAnalyzer() );
Query query;
List result;
@@ -187,9 +192,9 @@
result = session.createFullTextQuery(query, Product.class).list();
assertEquals( "collection of embedded ignored", 1, result.size() );
- session = Search.createFullTextSession(s);
- //parser = new MultiFieldQueryParser( new String[] {"name", "orders.orderNumber"}, new StandardAnalyzer() );
- //query = parser.parse("ZERTYD");
+ //update the collection
+ Product p = (Product) result.get(0);
+ p.getAuthors().add(a4);
//PhraseQuery
query = new TermQuery(new Term("orders.orderNumber", "ZERTYD"));
@@ -199,9 +204,17 @@
result = session.createFullTextQuery(query, Product.class).list();
assertEquals( "collection of untokenized ignored", 1, result.size() );
+ tx.commit();
+
s.clear();
tx = s.beginTransaction();
+ session = Search.createFullTextSession( s );
+ query = parser.parse("Proust");
+ result = session.createFullTextQuery(query, Product.class).list();
+ //HSEARCH-56
+ assertEquals( "update of collection of embedded ignored", 1, result.size() );
+
s.delete( s.get(Product.class, p1.getId() ) );
s.delete( s.get(Product.class, p2.getId() ) );
tx.commit();
Added: trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Categorie.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Categorie.java (rev 0)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Categorie.java 2007-05-23 22:51:40 UTC (rev 11552)
@@ -0,0 +1,56 @@
+//$Id: $
+package org.hibernate.search.test.session;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Store;
+import org.hibernate.search.annotations.Indexed;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Indexed
+ at Entity
+public class Categorie {
+
+ @DocumentId
+ @Id @GeneratedValue
+ private Integer id;
+
+ @Field( index = Index.TOKENIZED, store = Store.YES )
+ private String nom;
+
+ public Categorie() {
+ }
+
+ public Categorie(String nom) {
+ this.nom = nom;
+ }
+
+ public String toString() {
+ return ( nom );
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getNom() {
+ return nom;
+ }
+
+ public void setNom(String nom) {
+ this.nom = nom;
+ }
+}
+
+
Added: trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Entite.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Entite.java (rev 0)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/Entite.java 2007-05-23 22:51:40 UTC (rev 11552)
@@ -0,0 +1,66 @@
+//$Id: $
+package org.hibernate.search.test.session;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.ManyToOne;
+import javax.persistence.FetchType;
+
+import org.hibernate.search.annotations.IndexedEmbedded;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Indexed
+ at Entity
+public class Entite {
+ @DocumentId
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Field( index = Index.TOKENIZED, store = Store.YES )
+ private String titre;
+
+ @IndexedEmbedded
+ @ManyToOne(fetch = FetchType.LAZY)
+ private Categorie categorie;
+
+ public Entite() {
+ }
+
+ public Entite(String titre, Categorie categorie) {
+ this.titre = titre;
+ this.categorie = categorie;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getTitre() {
+ return titre;
+ }
+
+ public void setTitre(String titre) {
+ this.titre = titre;
+ }
+
+ public Categorie getCategorie() {
+ return categorie;
+ }
+
+ public void setCategorie(Categorie categorie) {
+ this.categorie = categorie;
+ }
+}
\ No newline at end of file
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/MassIndexTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/MassIndexTest.java 2007-05-22 19:28:04 UTC (rev 11551)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/session/MassIndexTest.java 2007-05-23 22:51:40 UTC (rev 11552)
@@ -17,6 +17,9 @@
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.index.Term;
/**
* @author Emmanuel Bernard
@@ -86,12 +89,49 @@
assertEquals( 1, result.size() );
tx.commit();
s.close();
+ }
+ public void testLazyLoading() throws Exception {
+ Categorie cat = new Categorie("Livre");
+ Entite ent = new Entite( "Le temple des songes", cat);
+ Session s = openSession( );
+ Transaction tx = s.beginTransaction();
+ s.persist( cat );
+ s.persist( ent );
+ tx.commit();
+ s.close();
+
+ s = openSession( );
+ FullTextSession session = Search.createFullTextSession( s );
+ Query luceneQuery = new TermQuery( new Term("categorie.nom", "livre") );
+ List result = session.createFullTextQuery( luceneQuery, Entite.class ).list();
+ assertEquals( 1, result.size() );
+ s.close();
+
+ s = openSession( );
+ ent = (Entite) s.get( Entite.class, ent.getId() );
+ session = Search.createFullTextSession( s );
+ session.index( ent );
+ s.close();
+
+ s = openSession( );
+ session = Search.createFullTextSession( s );
+ luceneQuery = new TermQuery( new Term("categorie.nom", "livre") );
+ result = session.createFullTextQuery( luceneQuery, Entite.class ).list();
+ assertEquals( "test lazy loading and indexing", 1, result.size() );
+ ent = (Entite) result.get( 0 );
+ cat = ent.getCategorie();
+ ent.setCategorie( null );
+ session.delete( cat );
+ session.delete( ent );
+ s.close();
}
protected Class[] getMappings() {
return new Class[] {
- Email.class
+ Email.class,
+ Entite.class,
+ Categorie.class
};
}
}
More information about the hibernate-commits
mailing list