Skip to content

Commit

Permalink
HSEARCH-1358 containsCollectionRole needs to verify whether the speci…
Browse files Browse the repository at this point in the history
…fied role starts with any of the registered collection roles (not just equality match). This is needed for nested embedded collections.

Also adding test case.
  • Loading branch information
hferentschik authored and Sanne committed Oct 1, 2013
1 parent eeb0603 commit b987fa6
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 1 deletion.
Expand Up @@ -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
Expand Down Expand Up @@ -210,7 +211,12 @@ public Collection<XClass> 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() {
Expand Down Expand Up @@ -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.<something>
}
return false;
}

private boolean determineWhetherDocumentIdPropertyIsTheSameAsJpaIdProperty(XProperty jpaIdProperty) {
if ( idPropertyMetadata == null ) {
return false; // not an indexed type
Expand Down Expand Up @@ -489,6 +505,11 @@ public Class<?> getIndexedType() {
public TypeMetadata build() {
return new TypeMetadata( this );
}

@Override
public String toString() {
return "TypeMetadata.Builder{indexedType=" + indexedType + "}";
}
}
}

Expand Down
@@ -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;
}
}


@@ -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<Long, String> categoriesValue = (Map<Long, String>) value;
for ( String s : categoriesValue.values() ) {
luceneOptions.addFieldToDocument( name, s, document );
}
}
}


@@ -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<Long, String> categories;

@ElementCollection
@IndexedEmbedded
@MapKeyColumn
@Field(bridge = @FieldBridge(impl = CategoriesBridge.class))
public Map<Long, String> getCategories() {
if ( categories == null ) {
categories = new HashMap<Long, String>();
}
return categories;
}

public void setCategories(Map<Long, String> categories) {
this.categories = categories;
}
}


@@ -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<Book> 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<Book>) query.getResultList();
}
}


6 changes: 6 additions & 0 deletions orm/src/test/resources/META-INF/persistence.xml
Expand Up @@ -29,4 +29,10 @@
<class>org.hibernate.search.test.query.ProductArticle</class>
<exclude-unlisted-classes />
</persistence-unit>

<persistence-unit name="EventBasedEmbeddableCollectionUpdateTestPU">
<class>org.hibernate.search.test.event.autoindexembeddable.Book</class>
<class>org.hibernate.search.test.event.autoindexembeddable.EmbeddableCategories</class>
<exclude-unlisted-classes />
</persistence-unit>
</persistence>

0 comments on commit b987fa6

Please sign in to comment.