-
Notifications
You must be signed in to change notification settings - Fork 362
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 9232995
Showing
15 changed files
with
486 additions
and
8 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
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
33 changes: 33 additions & 0 deletions
33
...ernate-52/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,33 @@ | ||
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
...java/io/hypersistence/utils/spring/repo/projection/SpringDataProjectionConfiguration.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 SpringDataProjectionConfiguration 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 | ||
) | ||
); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...src/test/java/io/hypersistence/utils/spring/repo/projection/SpringDataProjectionTest.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,75 @@ | ||
package io.hypersistence.utils.spring.repo.projection; | ||
|
||
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 static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = SpringDataProjectionConfiguration.class) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
public class SpringDataProjectionTest { | ||
|
||
protected final Logger LOGGER = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private TransactionTemplate transactionTemplate; | ||
|
||
@Autowired | ||
private PostRepository postRepository; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Test | ||
public void myTest() { | ||
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; | ||
}); | ||
|
||
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()); | ||
} | ||
} | ||
|
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
33 changes: 33 additions & 0 deletions
33
...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,33 @@ | ||
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
...java/io/hypersistence/utils/spring/repo/projection/SpringDataProjectionConfiguration.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 SpringDataProjectionConfiguration 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 | ||
) | ||
); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...src/test/java/io/hypersistence/utils/spring/repo/projection/SpringDataProjectionTest.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,75 @@ | ||
package io.hypersistence.utils.spring.repo.projection; | ||
|
||
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 static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = SpringDataProjectionConfiguration.class) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
public class SpringDataProjectionTest { | ||
|
||
protected final Logger LOGGER = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private TransactionTemplate transactionTemplate; | ||
|
||
@Autowired | ||
private PostRepository postRepository; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Test | ||
public void myTest() { | ||
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; | ||
}); | ||
|
||
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()); | ||
} | ||
} | ||
|
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
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
Oops, something went wrong.