-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ListArrayTypeDescriptor doesn't support Spring JPA Projections #562
- Loading branch information
1 parent
96335e4
commit 7e39ae3
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ernate-55/src/test/java/io/hypersistence/utils/spring/repo/projection/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.hypersistence.utils.spring.repo.projection; | ||
|
||
import io.hypersistence.utils.spring.domain.Post; | ||
import io.hypersistence.utils.spring.repository.BaseJpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@Repository | ||
public interface PostRepository extends BaseJpaRepository<Post, Long> { | ||
|
||
@Query(value = "select p.title as title, array_agg(p.slug) as slugs " + | ||
"from Post p " + | ||
"group by p.title", | ||
nativeQuery = true) | ||
@Nullable | ||
List<TestProjection> findAllSlugGroupedByTitle(); | ||
|
||
|
||
interface TestProjection { | ||
@Nullable | ||
String getTitle(); | ||
@Nullable | ||
List<String> getSlugs(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
.../hypersistence/utils/spring/repo/projection/ProjectionSpringDataJPABaseConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.hypersistence.utils.spring.repo.projection; | ||
|
||
import io.hypersistence.utils.hibernate.type.array.ListArrayType; | ||
import io.hypersistence.utils.spring.config.AbstractSpringDataJPAConfiguration; | ||
import io.hypersistence.utils.spring.domain.Post; | ||
import io.hypersistence.utils.spring.repository.BaseJpaRepositoryImpl; | ||
import org.hibernate.boot.spi.MetadataBuilderContributor; | ||
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
|
||
import java.util.Properties; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@ComponentScan( | ||
basePackages = { | ||
"io.hypersistence.utils.spring.repo.projection", | ||
} | ||
) | ||
@EnableJpaRepositories( | ||
value = "io.hypersistence.utils.spring.repo.projection", | ||
repositoryBaseClass = BaseJpaRepositoryImpl.class | ||
) | ||
public class ProjectionSpringDataJPABaseConfiguration extends AbstractSpringDataJPAConfiguration { | ||
|
||
@Override | ||
protected String packageToScan() { | ||
return Post.class.getPackage().getName(); | ||
} | ||
|
||
@Override | ||
protected void additionalProperties(Properties properties) { | ||
properties.put("hibernate.jdbc.batch_size", "100"); | ||
properties.put("hibernate.order_inserts", "true"); | ||
|
||
properties.put( | ||
EntityManagerFactoryBuilderImpl.METADATA_BUILDER_CONTRIBUTOR, | ||
(MetadataBuilderContributor) metadataBuilder -> metadataBuilder.applyBasicType( | ||
ListArrayType.INSTANCE | ||
) | ||
); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...t/java/io/hypersistence/utils/spring/repo/projection/ProjectionSpringDataJPABaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.hypersistence.utils.spring.repo.projection; | ||
|
||
import io.hypersistence.utils.hibernate.type.util.Configuration; | ||
import io.hypersistence.utils.spring.domain.Post; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.transaction.support.TransactionCallback; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = ProjectionSpringDataJPABaseConfiguration.class) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
public class ProjectionSpringDataJPABaseTest { | ||
|
||
protected final Logger LOGGER = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private TransactionTemplate transactionTemplate; | ||
|
||
@Autowired | ||
private PostRepository postRepository; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Test | ||
public void myTest() { | ||
// given | ||
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> { | ||
postRepository.persist( | ||
new Post() | ||
.setId(1L) | ||
.setTitle("test title") | ||
.setSlug("slug1") | ||
); | ||
|
||
postRepository.persistAndFlush( | ||
new Post() | ||
.setId(2L) | ||
.setTitle("test title") | ||
.setSlug("slug2") | ||
); | ||
|
||
return null; | ||
}); | ||
|
||
// when | ||
List<PostRepository.TestProjection> postsSummary = transactionTemplate.execute(transactionStatus -> | ||
postRepository.findAllSlugGroupedByTitle() | ||
); | ||
|
||
// then | ||
PostRepository.TestProjection result = postsSummary.get(0); | ||
assertEquals("test title", result.getTitle()); | ||
|
||
List<String> expectedSlugs = new ArrayList<>(); | ||
expectedSlugs.add("slug1"); | ||
expectedSlugs.add("slug2"); | ||
assertEquals(expectedSlugs, result.getSlugs()); | ||
} | ||
} | ||
|