Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-682 Include test for search of embedded entity using fluent A…
…PI and field bridge
- Loading branch information
1 parent
97bcab9
commit 87735c2
Showing
3 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...earch-orm/src/test/java/org/hibernate/search/test/query/dsl/embedded/ContainerEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors | ||
* as indicated by the @authors tag. All rights reserved. | ||
* See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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, v. 2.1. | ||
* This program is distributed in the hope that it will be useful, but WITHOUT A | ||
* 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, | ||
* v.2.1 along with this distribution; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
package org.hibernate.search.test.query.dsl.embedded; | ||
|
||
import javax.persistence.Embedded; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.Indexed; | ||
import org.hibernate.search.annotations.IndexedEmbedded; | ||
|
||
/** | ||
* @author neek | ||
*/ | ||
@Entity | ||
@Indexed | ||
public class ContainerEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Field | ||
private String parentStringValue; | ||
|
||
@Embedded | ||
@IndexedEmbedded(depth = 1, prefix = "emb.") | ||
private EmbeddedEntity embeddedEntity; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getParentStringValue() { | ||
return parentStringValue; | ||
} | ||
|
||
public void setParentStringValue(String parentStringValue) { | ||
this.parentStringValue = parentStringValue; | ||
} | ||
|
||
public EmbeddedEntity getEmbeddedEntity() { | ||
return embeddedEntity; | ||
} | ||
|
||
public void setEmbeddedEntity(EmbeddedEntity embeddedEntity) { | ||
this.embeddedEntity = embeddedEntity; | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
...orm/src/test/java/org/hibernate/search/test/query/dsl/embedded/DslEmbeddedSearchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors | ||
* as indicated by the @authors tag. All rights reserved. | ||
* See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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, v. 2.1. | ||
* This program is distributed in the hope that it will be useful, but WITHOUT A | ||
* 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, | ||
* v.2.1 along with this distribution; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
package org.hibernate.search.test.query.dsl.embedded; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.hibernate.Session; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.search.FullTextQuery; | ||
import org.hibernate.search.FullTextSession; | ||
import org.hibernate.search.Search; | ||
import org.hibernate.search.query.dsl.QueryBuilder; | ||
import org.hibernate.search.test.SearchTestCase; | ||
|
||
/** | ||
* @author Davide D'Alto | ||
*/ | ||
public class DslEmbeddedSearchTest extends SearchTestCase { | ||
|
||
private Session s = null; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
EmbeddedEntity ee = new EmbeddedEntity(); | ||
ee.setEmbeddedField( "embedded" ); | ||
ee.setNumber( 7 ); | ||
|
||
ContainerEntity pe = new ContainerEntity(); | ||
pe.setEmbeddedEntity( ee ); | ||
pe.setParentStringValue( "theparentvalue" ); | ||
|
||
s = openSession(); | ||
s.getTransaction().begin(); | ||
s.persist( pe ); | ||
s.getTransaction().commit(); | ||
} | ||
|
||
@Override | ||
public void tearDown() throws Exception { | ||
s.clear(); | ||
deleteAll( s, ContainerEntity.class ); | ||
s.close(); | ||
super.tearDown(); | ||
} | ||
|
||
public void testSearchString() throws Exception { | ||
FullTextSession fullTextSession = Search.getFullTextSession( s ); | ||
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder() | ||
.forEntity( ContainerEntity.class ).get(); | ||
Query q = qb.keyword().onField( "emb.embeddedField" ).matching( "embedded" ).createQuery(); | ||
List<ContainerEntity> results = execute( fullTextSession, q ); | ||
|
||
assertEquals( "DSL didn't find the embedded string field", 1, results.size() ); | ||
assertEquals( "embedded", results.get( 0 ).getEmbeddedEntity().getEmbeddedField() ); | ||
|
||
} | ||
|
||
public void testSearchNumberWithFieldBridge() throws Exception { | ||
FullTextSession fullTextSession = Search.getFullTextSession( s ); | ||
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder() | ||
.forEntity( ContainerEntity.class ).get(); | ||
Query q = qb.keyword().onField( "emb.num" ).matching( 7 ).createQuery(); | ||
List<ContainerEntity> results = execute( fullTextSession, q ); | ||
|
||
assertEquals( "DSL didn't find the embedded numeric field", 1, results.size() ); | ||
assertEquals( Integer.valueOf( 7 ), results.get( 0 ).getEmbeddedEntity().getNumber() ); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private List<ContainerEntity> execute(FullTextSession fullTextSession, Query q) { | ||
FullTextQuery combinedQuery = fullTextSession.createFullTextQuery( q, ContainerEntity.class ); | ||
|
||
return combinedQuery.list(); | ||
} | ||
|
||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class[] { ContainerEntity.class }; | ||
} | ||
|
||
private void deleteAll(Session s, Class<?>... classes) { | ||
Transaction tx = s.beginTransaction(); | ||
for ( Class<?> each : classes ) { | ||
List<?> list = s.createCriteria( each ).list(); | ||
for ( Object object : list ) { | ||
s.delete( object ); | ||
} | ||
} | ||
tx.commit(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...search-orm/src/test/java/org/hibernate/search/test/query/dsl/embedded/EmbeddedEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors | ||
* as indicated by the @authors tag. All rights reserved. | ||
* See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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, v. 2.1. | ||
* This program is distributed in the hope that it will be useful, but WITHOUT A | ||
* 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, | ||
* v.2.1 along with this distribution; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
package org.hibernate.search.test.query.dsl.embedded; | ||
|
||
import javax.persistence.Embeddable; | ||
|
||
import org.hibernate.search.annotations.Analyze; | ||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.FieldBridge; | ||
import org.hibernate.search.annotations.Parameter; | ||
import org.hibernate.search.test.bridge.PaddedIntegerBridge; | ||
|
||
@Embeddable | ||
public class EmbeddedEntity { | ||
|
||
@Field | ||
private String embeddedField; | ||
|
||
@Field(name = "num", analyze = Analyze.NO) | ||
@FieldBridge( | ||
impl = PaddedIntegerBridge.class, | ||
params = { @Parameter(name = "padding", value = "4") } | ||
) | ||
private Integer number; | ||
|
||
public String getEmbeddedField() { | ||
return embeddedField; | ||
} | ||
|
||
public void setEmbeddedField(String embeddedField) { | ||
this.embeddedField = embeddedField; | ||
} | ||
|
||
public Integer getNumber() { | ||
return number; | ||
} | ||
|
||
public void setNumber(Integer number) { | ||
this.number = number; | ||
} | ||
|
||
} |