@SuppressWarnings("unchecked")
public void testMoreLikeThisToEntity() {
Transaction transaction = fullTextSession.beginTransaction();
Query mltQuery;
try {
QueryBuilder qb = getCoffeeQueryBuilder();
Coffee decaffInstance = getDecaffInstance( qb );
// query results to compare toEntity() results against
mltQuery = qb
.moreLikeThis()
.comparingField( "summary" ).boostedTo( 10f )
.andField( "description" )
.toEntityWithId( decaffInstance.getId() )
.createQuery();
List<Object[]> results = (List<Object[]>) fullTextSession
.createFullTextQuery( mltQuery, Coffee.class )
.setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
.list();
// pass entity itself in a managed state
mltQuery = qb
.moreLikeThis()
.comparingField( "summary" ).boostedTo( 10f )
.andField( "description" )
.toEntity( decaffInstance )
.createQuery();
List<Object[]> entityResults = (List<Object[]>) fullTextSession
.createFullTextQuery( mltQuery, Coffee.class )
.setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
.list();
// query from id and from the managed entity should match
assertThat( entityResults ).isNotEmpty();
assertThat( entityResults ).hasSize( results.size() );
for ( int index = 0; index < entityResults.size(); index++ ) {
Object[] real = entityResults.get( index );
Object[] expected = results.get( index );
assertThat( real[1] ).isEqualTo( expected[1] );
assertThat( ( (Coffee) real[0] ).getId() ).isEqualTo( ( (Coffee) expected[0] ).getId() );
}
outputQueryAndResults( decaffInstance, mltQuery, entityResults );
// pass entity itself with a matching id but different values
// the id should take precedene
Coffee nonMatchingOne = (Coffee) results.get( results.size() - 1 )[0];
Coffee copyOfDecaffInstance = new Coffee();
copyOfDecaffInstance.setId( decaffInstance.getId() );
copyOfDecaffInstance.setInternalDescription( nonMatchingOne.getInternalDescription() );
copyOfDecaffInstance.setName( nonMatchingOne.getName() );
copyOfDecaffInstance.setDescription( nonMatchingOne.getDescription() );
copyOfDecaffInstance.setIntensity( nonMatchingOne.getIntensity() );
copyOfDecaffInstance.setSummary( nonMatchingOne.getSummary() );
mltQuery = qb
.moreLikeThis()
.comparingField( "summary" ).boostedTo( 10f )
.andField( "description" )
.toEntity( copyOfDecaffInstance )
.createQuery();
entityResults = (List<Object[]>) fullTextSession
.createFullTextQuery( mltQuery, Coffee.class )
.setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
.list();
// query from id and from the managed entity should match
assertThat( entityResults ).isNotEmpty();
assertThat( entityResults ).hasSize( results.size() );
for ( int index = 0; index < entityResults.size(); index++ ) {
Object[] real = entityResults.get( index );
Object[] expected = results.get( index );
assertThat( real[1] ).isEqualTo( expected[1] );
assertThat( ( (Coffee) real[0] ).getId() ).isEqualTo( ( (Coffee) expected[0] ).getId() );
}
outputQueryAndResults( decaffInstance, mltQuery, entityResults );
// pass entity itself with the right values but no id
copyOfDecaffInstance = new Coffee();
copyOfDecaffInstance.setInternalDescription( decaffInstance.getInternalDescription() );
copyOfDecaffInstance.setName( decaffInstance.getName() );
copyOfDecaffInstance.setDescription( decaffInstance.getDescription() );
copyOfDecaffInstance.setIntensity( decaffInstance.getIntensity() );
copyOfDecaffInstance.setSummary( decaffInstance.getSummary() );
mltQuery = qb
.moreLikeThis()
.comparingField( "summary" ).boostedTo( 10f )
.andField( "description" )
.toEntity( copyOfDecaffInstance )
.createQuery();