Skip to content

Add support for AOT Repositories #3265

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

Closed
mp911de opened this issue Apr 8, 2025 · 0 comments
Closed

Add support for AOT Repositories #3265

mp911de opened this issue Apr 8, 2025 · 0 comments
Assignees
Labels
theme: aot An issue related to Ahead-Of-Time processing type: enhancement A general enhancement

Comments

@mp911de
Copy link
Member

mp911de commented Apr 8, 2025

It makes sense to leverage AOT processing to pre-process repository definitions and to prepare repository query methods for improved query execution so that we don't need to use runtime query generation and query derivation.

During AOT processing, we have all necessary details to determine the actual query, extract value expressions and to generate code that runs the query.

We could generate something along the lines of:

  public List<User> findByLastnameStartingWith(String lastname) {
    String queryString = "SELECT u FROM org.springframework.data.jpa.domain.sample.User u WHERE u.lastname LIKE ?1 ESCAPE '\\'";
    Query query = this.entityManager.createQuery(queryString);
    query.setParameter(1, "%s%%".formatted(lastname));

    return (List<User>) query.getResultList();
  }

for a repository:

interface UserRepository extends CrudRepository<User, Integer> {

	List<User> findByLastnameStartingWith(String lastname);
}

AOT processing binds assumptions made during build-time processing into code so that repositories are assigned statically to a Spring Data module (i.e., Spring Data repositories are generally implemented by the availability of a store module if no store-specific annotations or repository declarations are being used).

Customizing bean registration code fragments allows mounting the generated AOT fragment to the final repository.

A major benefit is that all query processing and validation happens during build time and we no longer need to parse and derive queries provided through an AOT repository.

In certain arrangements, AOT repository query methods can become overly complex (e.g. JPA stored procedures, dynamic projections) in which it doesn't make sense to generate such a method.

Related, since we have all query information at hand, we could generate a repository JSON metadata file containing query details for later usage.

For the time being, we focus on imperative repository query methods. Reactive ones will be discussed at a later stage.

@mp911de mp911de added type: enhancement A general enhancement theme: aot An issue related to Ahead-Of-Time processing labels Apr 8, 2025
@mp911de mp911de added this to the 4.0 M2 (2025.1.0) milestone Apr 14, 2025
mp911de pushed a commit that referenced this issue Apr 14, 2025
…gments in BeanDefinition AOT code.

We now provide infrastructure to generate AOT repository method code that implements Query method behavior.

No longer use spring.factories but write some custom bean config code so that one of the properties can provide an instance of the generated repository.

Closes #3265
mp911de added a commit that referenced this issue Apr 14, 2025
Introduce RepositoryFragmentsFunction and a collection of functions to provide a well-formed contract.

Use RepositoryMetadata from initialized RepositoryFactoryBean.

See #3265
mp911de added a commit that referenced this issue Apr 14, 2025
mp911de added a commit that referenced this issue Apr 14, 2025
Fix fragment method order lookup. Lazify ProjectionFactory retrieval on DefaultFragmentCreationContext.

See #3265
mp911de added a commit that referenced this issue Apr 14, 2025
Introduce Environment to AotContext.
Move JSON classes to aot/generate package.

Refine exposed API.

See #3265
@mp911de mp911de closed this as completed Apr 14, 2025
mp911de added a commit that referenced this issue Apr 14, 2025
Introduce Environment to AotContext.
Move JSON classes to aot/generate package.

Refine exposed API.

See #3265
mp911de added a commit that referenced this issue Apr 14, 2025
Introduce Environment to AotContext.
Move JSON classes to aot/generate package.

Refine exposed API.

See #3265
schauder pushed a commit that referenced this issue Apr 16, 2025
…gments in BeanDefinition AOT code.

We now provide infrastructure to generate AOT repository method code that implements Query method behavior.

No longer use spring.factories but write some custom bean config code so that one of the properties can provide an instance of the generated repository.

Closes #3265
schauder pushed a commit that referenced this issue Apr 16, 2025
Introduce RepositoryFragmentsFunction and a collection of functions to provide a well-formed contract.

Use RepositoryMetadata from initialized RepositoryFactoryBean.

See #3265
schauder pushed a commit that referenced this issue Apr 16, 2025
schauder pushed a commit that referenced this issue Apr 16, 2025
Fix fragment method order lookup. Lazify ProjectionFactory retrieval on DefaultFragmentCreationContext.

See #3265
schauder pushed a commit that referenced this issue Apr 16, 2025
Introduce Environment to AotContext.
Move JSON classes to aot/generate package.

Refine exposed API.

See #3265
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: aot An issue related to Ahead-Of-Time processing type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants