Skip to content
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

Question: Spring Data 3.1 Jakarta Jpa Repository + EntitySpecification + Paging + Entity Graph #1815

Closed
thejeff77 opened this issue Oct 23, 2023 · 12 comments

Comments

@thejeff77
Copy link

Description

Question about default JpaRepository methods available via spring extensions.

Hey guys, I'm new to Blaze Persistence. I can see that the spring 3.1 support is relatively new, and I'm trying to solve an issue I'm having by integrating with your library. I want to do it optimally, so thus this question.

First, here's the issue I'm trying to solve:

(kotlin)

Here's my code with Vanilla spring data. As you can see, I'm trying to get Pageable working with an Entity Graph and Entity Specification.

@Repository
interface CustomJpaRepository :
    JpaRepository<CustomEntity, UUID>,
    JpaSpecificationExecutor<CustomEntity> {

    @EntityGraph("CustomEntity.oneToManyChild")
    override fun findAll(spec: Specification<CustomEntity>, pageable: Pageable): Page<CustomEntity>
}

Unfortunately, this breaks because it paginates in-memory. I have this setting enabled for efficiency: spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=false

This won't paginate efficiently...

I know I can do this with your Criteria API, however it adds a whole lot of code....

Are there any extension methods for a JpaRepository that allow a findAll with the combo of these 3 with Blaze? I.E. a Jpa Repository interface or something? Or my ability to specify EntityGraph as a parameter instead of an annotation?

I wanted to ask this before re-writing all of this with the CriteriaApi and bloating code more than possibly necessary.

Thanks for any help/clarification!

@thejeff77 thejeff77 changed the title Question: Spring Data Jpa Repository + EntitySpecification + Paging + Entity Graph Question: Spring Data 3.1 Jakarta Jpa Repository + EntitySpecification + Paging + Entity Graph Oct 23, 2023
@beikov
Copy link
Member

beikov commented Oct 23, 2023

You're probably just missing an annotation. You have to replace your @EnableRepositories annotation with @EnableBlazeRepositories or if you want to work with both the Spring Data JPA repositories and Blaze-Persistence repositories, then you have to split your repositories into different packages and apply the basePackage setting respectively. Also see https://persistence.blazebit.com/documentation/1.6/entity-view/manual/en_US/index.html#spring-data-setup

@thejeff77
Copy link
Author

@beikov thanks, I've added the annotation and still don't see much difference. It seems to me that Blaze Persistence doesn't extend a typical JpaRepository, is that correct? I'd have to define another EntityView and EntityViewSpecification for that EntityView, then specify this under basePackages and use it separately?

@thejeff77
Copy link
Author

thejeff77 commented Oct 23, 2023

Trying to build a view in kotlin...

@EntityView(CustomEntity::class)
interface CustomView {
    @get:IdMapping
    val id: UUID
}

seems to have issues:

Caused by: java.lang.IllegalArgumentException: Unknown managed type 'com.company.example.model.view.CustomView'
	at com.blazebit.persistence.impl.EntityMetamodelImpl.getEntry(EntityMetamodelImpl.java:691)
	at com.blazebit.persistence.impl.EntityMetamodelImpl.getManagedType(EntityMetamodelImpl.java:671)
	at com.blazebit.persistence.spring.data.impl.repository.BlazePersistenceRepositoryFactory.getRepositoryBaseClass(BlazePersistenceRepositoryFactory.java:257)
	at com.blazebit.persistence.spring.data.impl.repository.BlazePersistenceRepositoryFactory.lambda$getRepositoryInformation$2(BlazePersistenceRepositoryFactory.java:523)
	at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330)
	at com.blazebit.persistence.spring.data.impl.repository.BlazePersistenceRepositoryFactory.getRepositoryInformation(BlazePersistenceRepositoryFactory.java:521)
	at com.blazebit.persistence.spring.data.impl.repository.BlazePersistenceRepositoryFactory.getRepository(BlazePersistenceRepositoryFactory.java:425)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279)
	at org.springframework.data.util.Lazy.getNullable(Lazy.java:245)
	at org.springframework.data.util.Lazy.get(Lazy.java:114)

Sorry to be a pain, suggestions welcome though. Thanks for any reply.

@beikov
Copy link
Member

beikov commented Oct 24, 2023

You can try playing around with one of the quickstarts to figure out what you might be missing: https://github.com/Blazebit/blaze-persistence#quickstart

The error you just posted looks to me as if you were trying to setup a JpaRepository with entity views. If you want some convenience methods, you can use the EntityViewRepository interface, but note that if you want to implement JpaRepository, you still have to pass the entity type as type variable when implementing the interface.

@thejeff77
Copy link
Author

demo 4.zip

I used the quickstart to generate a project, then used that template to create a kotlin project with my setup to see if I could reproduce the issue.

This example project showcases the issue:

Caused by: java.lang.IllegalArgumentException: Unknown managed type 'com.example.demo.view.CatSimpleView'
	at com.blazebit.persistence.impl.EntityMetamodelImpl.getEntry(EntityMetamodelImpl.java:691) ~[blaze-persistence-core-impl-jakarta-1.6.9.jar:1.6.9]

I don't know if its because of the kotlin code, or because we also have jpa repositories enabled alongside in separate packages or what's going on.

Make sure to run ./build_db_container.sh to start the db (docker required)

@beikov
Copy link
Member

beikov commented Oct 24, 2023

From a quick look at the project, you should remove the bean for EntityViewConfiguration in RepoConfig i.e.

    @Bean
    fun entityViewConfiguration(): EntityViewConfiguration = EntityViews.createDefaultConfiguration()

@thejeff77
Copy link
Author

thejeff77 commented Oct 24, 2023

@beikov this is required for creating the entityViewManager as shown in the instructions you linked.

https://persistence.blazebit.com/documentation/1.6/entity-view/manual/en_US/index.html#spring-data-setup

Is the entity view manager not required to get the EntityView(s) working?

Removing this and the dependent bean entityViewManager (shown in the instructions) yields:

Field evm in com.blazebit.persistence.spring.data.impl.repository.BlazePersistenceRepositoryFactoryBean required a bean of type 'com.blazebit.persistence.view.EntityViewManager' that could not be found.

@beikov
Copy link
Member

beikov commented Oct 24, 2023

The @EnableEntityViews annotation takes care of producing the EntityViewConfiguration bean. See https://persistence.blazebit.com/documentation/1.6/entity-view/manual/en_US/index.html#anchor-environment-spring

@thejeff77
Copy link
Author

thejeff77 commented Oct 24, 2023

@beikov thank you! That fixed it in the demo but somehow not in my main app.. sorry that's a bit confusing, the IDE thinks that one wasn't defined. Curious why autoconfig doesn't setup the other beans by default via @ConditionalOnMissingBean annotation/autoconfig. This would simplify setup, andwould be a non-breaking change.

Also default bean scope is singleton, so I don't believe that is necessary.

Thank you very much for your timely replies :)

@beikov
Copy link
Member

beikov commented Oct 24, 2023

It just never occurred to me that we could use conditional beans. Would be great if you could contribute that 😀

@thejeff77
Copy link
Author

Definitely might.. if I can get it working and used in production I'll have a vested interest in keeping this working and easy to use. Now the demo isn't useful in reproducing my issue so I'll have to bang my head on the wall a while longer. Thanks again for help so far @beikov

@thejeff77
Copy link
Author

I fixed it, a dumb typo as usual. Thanks again for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants