From b987fa6ddf09cb71e20b61e58efedf5517204d70 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 30 Sep 2013 15:32:01 +0200 Subject: [PATCH] HSEARCH-1358 containsCollectionRole needs to verify whether the specified role starts with any of the registered collection roles (not just equality match). This is needed for nested embedded collections. Also adding test case. --- .../engine/metadata/impl/TypeMetadata.java | 23 ++- .../test/event/autoindexembeddable/Book.java | 81 +++++++++++ .../autoindexembeddable/CategoriesBridge.java | 47 +++++++ .../EmbeddableCategories.java | 61 ++++++++ ...ntBasedEmbeddableCollectionUpdateTest.java | 131 ++++++++++++++++++ .../test/resources/META-INF/persistence.xml | 6 + 6 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/Book.java create mode 100644 orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/CategoriesBridge.java create mode 100644 orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EmbeddableCategories.java create mode 100644 orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EventBasedEmbeddableCollectionUpdateTest.java diff --git a/engine/src/main/java/org/hibernate/search/engine/metadata/impl/TypeMetadata.java b/engine/src/main/java/org/hibernate/search/engine/metadata/impl/TypeMetadata.java index 8ae88c56beb..b25597d6e08 100644 --- a/engine/src/main/java/org/hibernate/search/engine/metadata/impl/TypeMetadata.java +++ b/engine/src/main/java/org/hibernate/search/engine/metadata/impl/TypeMetadata.java @@ -55,6 +55,7 @@ */ public class TypeMetadata { private static final Log log = LoggerFactory.make(); + private static final String COMPONENT_PATH_SEPARATOR = "."; /** * The type for this metadata @@ -210,7 +211,12 @@ public Collection getOptimizationBlackList() { } public boolean containsCollectionRole(String role) { - return collectionRoles.contains( role ); + for ( String knownRolls : collectionRoles ) { + if ( isSubRole( knownRolls, role ) ) { + return true; + } + } + return false; } public boolean areClassBridgesUsed() { @@ -287,6 +293,16 @@ public String toString() { return sb.toString(); } + private boolean isSubRole(String subRole, String role) { + if ( role.equals( subRole ) ) { + return true; // direct match + } + if ( role.startsWith( subRole + COMPONENT_PATH_SEPARATOR ) ) { + return true; // role == subRole. + } + return false; + } + private boolean determineWhetherDocumentIdPropertyIsTheSameAsJpaIdProperty(XProperty jpaIdProperty) { if ( idPropertyMetadata == null ) { return false; // not an indexed type @@ -489,6 +505,11 @@ public Class getIndexedType() { public TypeMetadata build() { return new TypeMetadata( this ); } + + @Override + public String toString() { + return "TypeMetadata.Builder{indexedType=" + indexedType + "}"; + } } } diff --git a/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/Book.java b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/Book.java new file mode 100644 index 00000000000..9e09ba6cc40 --- /dev/null +++ b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/Book.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, 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.event.autoindexembeddable; + +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.Id; + +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; + +/** + * @author Hardy Ferentschik + */ +@Entity +@Indexed +public class Book { + + private Long id; + private String title; + private EmbeddableCategories embeddableCategories; + + @Id + @DocumentId + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Field(store = Store.YES) + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Embedded + @IndexedEmbedded + public EmbeddableCategories getEmbeddableCategories() { + if ( embeddableCategories == null ) { + embeddableCategories = new EmbeddableCategories(); + } + return embeddableCategories; + } + + public void setEmbeddableCategories(EmbeddableCategories categories) { + this.embeddableCategories = categories; + } +} + + diff --git a/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/CategoriesBridge.java b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/CategoriesBridge.java new file mode 100644 index 00000000000..f2f83e7fc04 --- /dev/null +++ b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/CategoriesBridge.java @@ -0,0 +1,47 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, 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.event.autoindexembeddable; + +import java.util.Map; + +import org.apache.lucene.document.Document; +import org.hibernate.search.bridge.FieldBridge; +import org.hibernate.search.bridge.LuceneOptions; + +/** + * @author Hardy Ferentschik + */ +public class CategoriesBridge implements FieldBridge { + @Override + public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { + @SuppressWarnings("unchecked") + Map categoriesValue = (Map) value; + for ( String s : categoriesValue.values() ) { + luceneOptions.addFieldToDocument( name, s, document ); + } + } +} + + diff --git a/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EmbeddableCategories.java b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EmbeddableCategories.java new file mode 100644 index 00000000000..3ad6d52bf05 --- /dev/null +++ b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EmbeddableCategories.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, 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.event.autoindexembeddable; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.ElementCollection; +import javax.persistence.Embeddable; +import javax.persistence.MapKeyColumn; + +import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.FieldBridge; +import org.hibernate.search.annotations.IndexedEmbedded; + +/** + * @author Hardy Ferentschik + */ +@Embeddable +public class EmbeddableCategories { + + private Map categories; + + @ElementCollection + @IndexedEmbedded + @MapKeyColumn + @Field(bridge = @FieldBridge(impl = CategoriesBridge.class)) + public Map getCategories() { + if ( categories == null ) { + categories = new HashMap(); + } + return categories; + } + + public void setCategories(Map categories) { + this.categories = categories; + } +} + + diff --git a/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EventBasedEmbeddableCollectionUpdateTest.java b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EventBasedEmbeddableCollectionUpdateTest.java new file mode 100644 index 00000000000..aeed6d33ef8 --- /dev/null +++ b/orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/EventBasedEmbeddableCollectionUpdateTest.java @@ -0,0 +1,131 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, 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.event.autoindexembeddable; + +import java.util.List; +import javax.persistence.EntityManager; + +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.queryParser.MultiFieldQueryParser; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParser; +import org.hibernate.search.jpa.FullTextEntityManager; +import org.hibernate.search.jpa.FullTextQuery; +import org.hibernate.search.jpa.Search; +import org.hibernate.search.test.TestConstants; +import org.hibernate.search.test.jpa.JPATestCase; +import org.hibernate.search.test.util.TestForIssue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Hardy Ferentschik + */ +@TestForIssue(jiraKey = "HSEARCH-1358") +public class EventBasedEmbeddableCollectionUpdateTest extends JPATestCase { + + private EntityManager entityManager; + + @Before + public void setUp() { + super.setUp(); + entityManager = factory.createEntityManager(); + } + + @After + public void tearDown() { + FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager( entityManager ); + fullTextEntityManager.purgeAll( Book.class ); + fullTextEntityManager.flushToIndexes(); + fullTextEntityManager.close(); + super.tearDown(); + } + + @Test + public void testUpdateOfEmbeddedElementCollectionTriggersIndexUpdate() throws Exception { + indexBookAndEnsureItIsIndexed(); + + // find and update book by adding "Bar" into Embedded ElementCollection + entityManager.getTransaction().begin(); + + Book book = entityManager.find( Book.class, 1234L ); + book.getEmbeddableCategories().getCategories().remove( 12L ); + book.getEmbeddableCategories().getCategories().put( 13L, "Bar" ); + + entityManager.persist( book ); + entityManager.getTransaction().commit(); + + assertEquals( + "Foo should have been removed by indexed update", + 0, + search( "embeddableCategories.categories:Foo" ).size() + ); + assertEquals( + "Bar should have been added by indexed update", + 1, + search( "embeddableCategories.categories:Bar" ).size() + ); + } + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { }; // configured in persistence.xml + } + + private void indexBookAndEnsureItIsIndexed() throws ParseException { + entityManager.getTransaction().begin(); + + Book book = new Book(); + book.setId( 1234L ); + book.getEmbeddableCategories().getCategories().put( 12L, "Foo" ); + + entityManager.persist( book ); + entityManager.getTransaction().commit(); + + assertEquals( + "Foo should have been added during indexing", + 1, + search( "embeddableCategories.categories:Foo" ).size() + ); + assertEquals( "Bar was not yet added", 0, search( "embeddableCategories.categories:Bar" ).size() ); + } + + @SuppressWarnings("unchecked") + private List search(String searchQuery) throws ParseException { + QueryParser parser = new MultiFieldQueryParser( + TestConstants.getTargetLuceneVersion(), + new String[] { }, + new StandardAnalyzer( TestConstants.getTargetLuceneVersion() ) + ); + FullTextQuery query = Search.getFullTextEntityManager( entityManager ) + .createFullTextQuery( parser.parse( searchQuery ), Book.class ); + return (List) query.getResultList(); + } +} + + diff --git a/orm/src/test/resources/META-INF/persistence.xml b/orm/src/test/resources/META-INF/persistence.xml index cb9bd810a82..22f58e180a5 100644 --- a/orm/src/test/resources/META-INF/persistence.xml +++ b/orm/src/test/resources/META-INF/persistence.xml @@ -29,4 +29,10 @@ org.hibernate.search.test.query.ProductArticle + + + org.hibernate.search.test.event.autoindexembeddable.Book + org.hibernate.search.test.event.autoindexembeddable.EmbeddableCategories + +