|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.repository; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.assertj.core.api.Assumptions.*; |
| 20 | + |
| 21 | +import jakarta.persistence.EntityManager; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.context.annotation.ComponentScan; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.context.annotation.FilterType; |
| 31 | +import org.springframework.context.annotation.ImportResource; |
| 32 | +import org.springframework.data.domain.Page; |
| 33 | +import org.springframework.data.domain.PageRequest; |
| 34 | +import org.springframework.data.domain.Pageable; |
| 35 | +import org.springframework.data.jpa.domain.sample.Role; |
| 36 | +import org.springframework.data.jpa.domain.sample.User; |
| 37 | +import org.springframework.data.jpa.provider.PersistenceProvider; |
| 38 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 39 | +import org.springframework.data.jpa.repository.sample.RoleRepository; |
| 40 | +import org.springframework.data.jpa.repository.sample.UserRepository; |
| 41 | +import org.springframework.data.repository.CrudRepository; |
| 42 | +import org.springframework.test.context.ContextConfiguration; |
| 43 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 44 | +import org.springframework.transaction.annotation.Transactional; |
| 45 | + |
| 46 | +/** |
| 47 | + * Hibernate-specific repository tests. |
| 48 | + * |
| 49 | + * @author Mark Paluch |
| 50 | + */ |
| 51 | +@ExtendWith(SpringExtension.class) |
| 52 | +@ContextConfiguration() |
| 53 | +@Transactional |
| 54 | +class HibernateRepositoryTests { |
| 55 | + |
| 56 | + @Autowired UserRepository userRepository; |
| 57 | + @Autowired RoleRepository roleRepository; |
| 58 | + @Autowired CteUserRepository cteUserRepository; |
| 59 | + @Autowired EntityManager em; |
| 60 | + |
| 61 | + PersistenceProvider provider; |
| 62 | + User dave; |
| 63 | + User carter; |
| 64 | + User oliver; |
| 65 | + Role drummer; |
| 66 | + Role guitarist; |
| 67 | + Role singer; |
| 68 | + |
| 69 | + @BeforeEach |
| 70 | + void setUp() { |
| 71 | + provider = PersistenceProvider.fromEntityManager(em); |
| 72 | + |
| 73 | + assumeThat(provider).isEqualTo(PersistenceProvider.HIBERNATE); |
| 74 | + roleRepository.deleteAll(); |
| 75 | + userRepository.deleteAll(); |
| 76 | + |
| 77 | + drummer = roleRepository.save(new Role("DRUMMER")); |
| 78 | + guitarist = roleRepository.save(new Role("GUITARIST")); |
| 79 | + singer = roleRepository.save(new Role("SINGER")); |
| 80 | + |
| 81 | + dave = userRepository. save( new User( "Dave", "Matthews", "[email protected]", singer)); |
| 82 | + carter = userRepository. save( new User( "Carter", "Beauford", "[email protected]", singer, drummer)); |
| 83 | + oliver = userRepository. save( new User( "Oliver August", "Matthews", "[email protected]")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test // GH-3726 |
| 87 | + void testQueryWithCTE() { |
| 88 | + |
| 89 | + Page<UserExcerptDto> result = cteUserRepository.findWithCTE(PageRequest.of(0, 1)); |
| 90 | + assertThat(result.getTotalElements()).isEqualTo(3); |
| 91 | + } |
| 92 | + |
| 93 | + @ImportResource({ "classpath:infrastructure.xml" }) |
| 94 | + @Configuration |
| 95 | + @EnableJpaRepositories(basePackageClasses = HibernateRepositoryTests.class, considerNestedRepositories = true, |
| 96 | + includeFilters = @ComponentScan.Filter( |
| 97 | + classes = { CteUserRepository.class, UserRepository.class, RoleRepository.class }, |
| 98 | + type = FilterType.ASSIGNABLE_TYPE)) |
| 99 | + static class TestConfig {} |
| 100 | + |
| 101 | + interface CteUserRepository extends CrudRepository<User, Integer> { |
| 102 | + |
| 103 | + /* |
| 104 | + WITH entities AS ( |
| 105 | + SELECT |
| 106 | + e.id as id, |
| 107 | + e.number as number |
| 108 | + FROM TestEntity e |
| 109 | + ) |
| 110 | + SELECT new com.example.demo.Result('X', c.id, c.number) |
| 111 | + FROM entities c |
| 112 | + */ |
| 113 | + |
| 114 | + @Query(""" |
| 115 | + WITH cte_select AS (select u.firstname as firstname, u.lastname as lastname from User u) |
| 116 | + SELECT new org.springframework.data.jpa.repository.UserExcerptDto(c.firstname, c.lastname) |
| 117 | + FROM cte_select c |
| 118 | + """) |
| 119 | + Page<UserExcerptDto> findWithCTE(Pageable page); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments