-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use tuple queries for projections using QueryDSL and Query by Example #2600
Comments
I have certainly reproduced this issue. To be clear, the query works. It simply doesn't slim things down to the subset of columns expected. I tested against both H2 as well as Postgres with the following test case: @ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = FetchableFluentQueryByPredicateIntegrationTests.Config.class)
@Transactional
public class FetchableFluentQueryByPredicateIntegrationTests {
@Autowired FetchableFluentRepository repository;
@BeforeEach
void setUp() {
repository.saveAndFlush(new User("Bilbo", "Baggins", "[email protected]"));
}
@Test
void projectionsOnDtoClassesShouldHaveAReducedProjectionInTheQuery() {
List<UserDto> users = repository.findBy(QUser.user.firstname.eq("Bilbo"), p -> p //
.project("firstname") //
.as(UserDto.class) //
.all());
assertThat(users).extracting(UserDto::getFirstname).containsExactly("Bilbo");
}
@Test
void projectionsOnEntitiesShouldHaveAReducedProjectionInTheQuery() {
List<User> users = repository.findBy(QUser.user.firstname.eq("Bilbo"), p -> p //
.project("firstname") //
.all());
assertThat(users).extracting(User::getFirstname).containsExactly("Bilbo");
}
public interface FetchableFluentRepository extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User> {
}
public interface UserDto {
String getFirstname();
}
@EnableJpaRepositories(considerNestedRepositories = true, basePackageClasses = FetchableFluentRepository.class, //
includeFilters = @ComponentScan.Filter(value = { FetchableFluentRepository.class },
type = FilterType.ASSIGNABLE_TYPE))
@EnableTransactionManagement
static class Config {
@Bean(initMethod = "start", destroyMethod = "stop")
public PostgreSQLContainer<?> container() {
return new PostgreSQLContainer<>("postgres:9.6.12") //
.withUsername("postgres");
}
@Bean
public DataSource dataSource(PostgreSQLContainer<?> container) {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setUrl(container.getJdbcUrl());
dataSource.setUser(container.getUsername());
dataSource.setPassword(container.getPassword());
return dataSource;
}
@Bean
public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPersistenceUnitRootLocation("simple-persistence");
factoryBean.setPersistenceUnitName("spring-data-jpa");
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "create");
properties.setProperty("hibernate.dialect", PostgreSQL91Dialect.class.getCanonicalName());
factoryBean.setJpaProperties(properties);
return factoryBean;
}
@Bean
PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
} In both scenarios, first on H2 (before adding the Testcontainers chunk now shown) as well as on Postgres (see above), I saw this query:
This while seeing the hint applied (by design) of: All that being said, this is a JPA hint, which means it's not a hard requirement that the persistence provider support this hint. I'm trying to dig in and see if we're doing the hint wrong, or if in reality, these persistence providers simply don't honor it. |
I also flipped to
|
Query by Example uses specifications that select the CriteriaQuery<Tuple> query = criteriaBuilder.createTupleQuery()
query.select(criteriaBuilder.tuple(employee.get(Employee_.name), employee.get(Employee_.salary))); Related: #487 |
I'm implementing a dynamic query with following code.
But the generated query still select all of the columns.
I expect the generated query should only select the specified column.
Here is the reproducible repo.
https://github.com/kekhuay/special-palm-tree
The text was updated successfully, but these errors were encountered: