Author: epbernard
Date: 2010-03-29 11:06:45 -0400 (Mon, 29 Mar 2010)
New Revision: 19126
Added:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Event.java
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/ParentOfBirthEvent.java
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Person.java
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/RecursiveGraphTest.java
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/AddLuceneWork.java
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/DeleteLuceneWork.java
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/WorkQueue.java
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java
Log:
HSEARCH-476 entities found while processing containedIn / IndexedDmbedded and without id
will trigger an even down the road => ignore
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/AddLuceneWork.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/AddLuceneWork.java 2010-03-28
22:48:08 UTC (rev 19125)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/AddLuceneWork.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -66,7 +66,7 @@
@Override
public String toString() {
- return "AddLuceneWork: " + this.getEntityClass().getName() + "#" +
this.getIdInString();
+ return "Add LuceneWork: " + this.getEntityClass().getSimpleName() +
"#" + this.getIdInString();
}
}
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/DeleteLuceneWork.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/DeleteLuceneWork.java 2010-03-28
22:48:08 UTC (rev 19125)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/DeleteLuceneWork.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -44,7 +44,6 @@
@Override
public String toString() {
- return "DeleteLuceneWork: " + this.getEntityClass().getName() + "#"
+ this.getIdInString();
+ return "Delete LuceneWork: " + this.getEntityClass().getSimpleName() +
"#" + this.getIdInString();
}
-
}
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/WorkQueue.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/WorkQueue.java 2010-03-28
22:48:08 UTC (rev 19125)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/backend/WorkQueue.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -29,11 +29,16 @@
import java.util.Collections;
import org.hibernate.annotations.common.AssertionFailure;
+import org.hibernate.search.util.LoggerFactory;
+import org.slf4j.Logger;
/**
* @author Emmanuel Bernard
*/
public class WorkQueue {
+
+ private static final Logger log = LoggerFactory.make();
+
private List<Work> queue;
private List<LuceneWork> sealedQueue;
@@ -66,9 +71,16 @@
return subQueue;
}
-
public List<LuceneWork> getSealedQueue() {
if (sealedQueue == null) throw new AssertionFailure("Access a Sealed WorkQueue
which has not been sealed");
+ if (log.isTraceEnabled()) {
+ StringBuilder sb = new StringBuilder();
+ for (LuceneWork lw : sealedQueue) {
+ sb.append( lw.toString() );
+ sb.append( "\n" );
+ }
+ log.trace( "sending sealedQueue to backend: \n" + sb.toString() );
+ }
return sealedQueue;
}
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java 2010-03-28
22:48:08 UTC (rev 19125)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -721,7 +721,14 @@
private <T> void addWorkForEmbeddedValue(T value, List<LuceneWork> queue,
Class<T> valueClass,
DocumentBuilderIndexedEntity<T> builderIndexedEntity,
SearchFactoryImplementor searchFactoryImplementor) {
Serializable id = ( Serializable ) ReflectionHelper.getMemberValue( value,
builderIndexedEntity.idGetter );
- builderIndexedEntity.addWorkToQueue( valueClass, value, id, WorkType.UPDATE, queue,
searchFactoryImplementor );
+ if ( id != null) {
+ builderIndexedEntity.addWorkToQueue( valueClass, value, id, WorkType.UPDATE, queue,
searchFactoryImplementor );
+ }
+ else {
+ //this is an indexed entity that is not yet persisted but should be reached by
cascade
+ // and thus raise an Hibernate Core event leading to its indexing by Hibernate Search
+ // => no need to do anything here
+ }
}
public Analyzer getAnalyzer() {
Added:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Event.java
===================================================================
---
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Event.java
(rev 0)
+++
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Event.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -0,0 +1,76 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.scratch;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+@Entity
+public class Event implements Serializable {
+
+ private Long id;
+ private Set<ParentOfBirthEvent> parentsOf = new
HashSet<ParentOfBirthEvent>();
+ private Set<Person> children = new HashSet<Person>();
+
+ @Id
+ @GeneratedValue
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @IndexedEmbedded
+ @OneToMany(mappedBy = "event")
+ public Set<ParentOfBirthEvent> getParentsOf() {
+ return parentsOf;
+ }
+
+ public void setParentsOf(Set<ParentOfBirthEvent> parentsOf) {
+ this.parentsOf = parentsOf;
+ }
+
+ @ContainedIn
+ @OneToMany(mappedBy = "birthEvent")
+ public Set<Person> getChildren() {
+ return children;
+ }
+
+ public void setChildren(Set<Person> children) {
+ this.children = children;
+ }
+
+}
Added:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/ParentOfBirthEvent.java
===================================================================
---
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/ParentOfBirthEvent.java
(rev 0)
+++
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/ParentOfBirthEvent.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -0,0 +1,82 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.scratch;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+@Entity
+@Indexed
+public class ParentOfBirthEvent {
+
+ private Long id;
+ private Person parent;
+ private Event event;
+
+ public ParentOfBirthEvent(Person parent, Event event) {
+ this.parent = parent;
+ this.event = event;
+ }
+
+ @Id
+ @DocumentId
+ @GeneratedValue
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @IndexedEmbedded
+ @ManyToOne
+ public Person getParent() {
+ return parent;
+ }
+
+ public void setParent(Person parent) {
+ this.parent = parent;
+ }
+
+ @ContainedIn
+ @ManyToOne(cascade=CascadeType.ALL)
+ public Event getEvent() {
+ return event;
+ }
+
+ public void setEvent(Event event) {
+ this.event = event;
+ }
+
+}
Added:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Person.java
===================================================================
---
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Person.java
(rev 0)
+++
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/Person.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -0,0 +1,100 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.scratch;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.OneToMany;
+
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+import org.hibernate.search.annotations.Store;
+
+@Entity
+@Indexed
+public class Person implements Serializable {
+
+ private Long id;
+ private Set<ParentOfBirthEvent> parentOfBirthEvents;
+ private Event birthEvent;
+ private String name;
+
+ public Person() {
+ birthEvent = new Event();
+ birthEvent.getChildren().add( this );
+ parentOfBirthEvents = new HashSet<ParentOfBirthEvent>();
+ }
+
+ @DocumentId
+ @Id
+ @GeneratedValue
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @ContainedIn
+ @OneToMany(cascade = { CascadeType.ALL })
+ public Set<ParentOfBirthEvent> getParentOfBirthEvents() {
+ return parentOfBirthEvents;
+ }
+
+ public void setParentOfBirthEvents(Set<ParentOfBirthEvent> parentOfBirthEvents) {
+ this.parentOfBirthEvents = parentOfBirthEvents;
+ }
+
+ @IndexedEmbedded(depth = 4)
+ @ManyToOne(cascade = { CascadeType.ALL }, optional = false)
+ public Event getBirthEvent() {
+ return birthEvent;
+ }
+
+ public void setBirthEvent(Event birthEvent) {
+ this.birthEvent = birthEvent;
+ }
+
+ @Field(store=Store.YES)
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
Added:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/RecursiveGraphTest.java
===================================================================
---
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/RecursiveGraphTest.java
(rev 0)
+++
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/scratch/RecursiveGraphTest.java 2010-03-29
15:06:45 UTC (rev 19126)
@@ -0,0 +1,115 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.scratch;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Fieldable;
+import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.TopDocs;
+import org.hibernate.Session;
+import org.hibernate.search.test.SearchTestCase;
+
+public class RecursiveGraphTest extends SearchTestCase {
+
+ public void testCreateParentAndChild() throws Exception {
+ Person[] people = new Person[2];
+ Person parent = new Person();
+ parent.setName( "parent" );
+ Person child = new Person();
+ child.setName( "child" );
+ connectChildToParent(child, parent);
+ people[0] = parent;
+ people[1] = child;
+ savePeople( people );
+ showIndexContentsForType( Person.class );
+ assertEquals( 2, getDocumentNbr( Person.class ) );
+ }
+
+ private void connectChildToParent(Person child, Person parent) {
+ Event birthEvent = child.getBirthEvent();
+ child.setBirthEvent(birthEvent);
+ ParentOfBirthEvent parentOfBirthEvent = new ParentOfBirthEvent(parent,
child.getBirthEvent());
+ parent.getParentOfBirthEvents().add(parentOfBirthEvent);
+ }
+
+ public void savePeople(Person... people) {
+ for (Person person : people) {
+ if (person==null) continue;
+ Session s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.save( person );
+ s.getTransaction().commit();
+ s.close();
+ }
+ }
+
+ @Override
+ protected Class<?>[] getMappings() {
+ return new Class[]{
+ Event.class,
+ Person.class,
+ ParentOfBirthEvent.class
+ };
+ }
+
+ private int getDocumentNbr(Class type) throws Exception {
+ IndexReader reader = IndexReader.open( getDirectory( type ), false );
+ try {
+ return reader.numDocs();
+ }
+ finally {
+ reader.close();
+ }
+ }
+
+ private void showIndexContentsForType(Class type) throws CorruptIndexException,
IOException {
+ IndexSearcher searcher = new IndexSearcher( getDirectory( type ), false );
+ try {
+ Query q = new MatchAllDocsQuery();
+ TopDocs docs = searcher.search( q, null, 100 );
+ ScoreDoc[] scoreDocs = docs.scoreDocs;
+ for (ScoreDoc sd : scoreDocs) {
+ Document doc = searcher.doc( sd.doc );
+ StringBuilder sb = new StringBuilder();
+ for (Fieldable field : (List<Fieldable>)doc.getFields()) {
+ sb.append( field.name() ).append( ":" ).append( field.stringValue()
).append( " " );
+ }
+ System.out.println( sb.toString() );
+ }
+ }
+ finally {
+ searcher.close();
+ }
+ }
+
+}