HHH-13140 - Criteria API multiselect aliases are not passed through to the JPQL query and they are not available in ResultTransformer

This commit is contained in:
Vlad Mihalcea 2018-12-04 17:55:31 +02:00
parent 7316307d0e
commit 89372191ec
2 changed files with 124 additions and 2 deletions

View File

@ -239,12 +239,22 @@ public abstract class AbstractPathImpl<X>
@Override
public String render(RenderingContext renderingContext) {
PathSource<?> source = getPathSource();
String path;
if ( source != null ) {
source.prepareAlias( renderingContext );
return source.getPathIdentifier() + "." + getAttribute().getName();
path = source.getPathIdentifier() + "." + getAttribute().getName();
}
else {
return getAttribute().getName();
path = getAttribute().getName();
}
String alias = getAlias();
if ( alias != null ) {
path = path + " as " + alias;
}
return path;
}
}

View File

@ -0,0 +1,112 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.criteria.alias;
import java.util.List;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class CriteriaMultiselectAliasTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Book.class
};
}
@Before
public void init() {
doInJPA( this::entityManagerFactory, entityManager -> {
Book book = new Book();
book.id = 1;
book.name = bookName();
entityManager.persist( book );
} );
}
@Test
public void test() {
doInJPA( this::entityManagerFactory, entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Object[]> query = cb.createQuery( Object[].class );
final Root<Book> entity = query.from( Book.class );
query.multiselect(
entity.get( "id" ).alias( "id" ),
entity.get( "name" ).alias( "title" )
);
List<BookDto> dtos = entityManager.createQuery( query )
.unwrap( Query.class )
.setResultTransformer( Transformers.aliasToBean( BookDto.class ) )
.getResultList();
assertEquals( 1, dtos.size() );
BookDto dto = dtos.get( 0 );
assertEquals( 1, (int) dto.getId() );
assertEquals( bookName(), dto.getTitle() );
} );
}
@Entity(name = "Book")
public static class Book {
@Id
private Integer id;
private String name;
}
public static class BookDto {
private Integer id;
private String title;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
protected String bookName() {
return "Vlad's High-Performance Java Persistence";
}
}