org.hibernate.transform.BasicTransformerAdapter Java Examples

The following examples show how to use org.hibernate.transform.BasicTransformerAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateSearchWithElasticsearchIT.java    From hibernate-demos with Apache License 2.0 5 votes vote down vote up
@Test
public void projectionWithTransformer() {
	EntityManager em = emf.createEntityManager();

	inTransaction( em, tx -> {
		FullTextEntityManager ftem = Search.getFullTextEntityManager( em );
		QueryBuilder qb = ftem.getSearchFactory()
				.buildQueryBuilder()
				.forEntity( VideoGame.class )
				.get();

		FullTextQuery query = ftem.createFullTextQuery(
				qb.keyword()
				.onField( "tags" )
				.matching( "round-based" )
				.createQuery(),
				VideoGame.class
		)
		.setProjection( "title", "publisher.name", "release" )
		.setResultTransformer( new BasicTransformerAdapter() {
			@Override
			public VideoGameDto transformTuple(Object[] tuple, String[] aliases) {
				return new VideoGameDto( (String) tuple[0], (String) tuple[1], (Date) tuple[2] );
			}
		} );

		VideoGameDto projection = (VideoGameDto) query.getSingleResult();
		assertThat( projection.getTitle() ).isEqualTo( "Tanaka's return" );
		assertThat( projection.getPublisherName() ).isEqualTo( "Samurai Games, Inc." );
		assertThat( projection.getRelease() ).isEqualTo( new GregorianCalendar( 2011, 2, 13 ).getTime() );
	} );

	em.close();
}