Coder Social home page Coder Social logo

Comments (3)

mp911de avatar mp911de commented on September 23, 2024 3

Right now, there's no global hook for query hints. You could however introduce functionality on your side to make it work. SimpleJpaRepository requires a slightly different approach than QuerydslJpaPredicateExecutor.

For the base repository implementation SimpleJpaRepository you need to create a subclass of SimpleJpaRepository and use that one as base repository.

In the second step, you create a wrapper for CrudMethodMetadata that holds query hints and other details that are evaluated for each query. You would augment CrudMethodMetadata.getComment() with details from CrudMethodMetadata.getMethod(). You could also augment CrudMethodMetadata.getQueryHints() with a custom wrapper.

For QuerydslJpaPredicateExecutor, you can subclass JpaRepositoryFactory and override getRepositoryFragments(…) to hook into CrudMethodMetadata.

from spring-data-jpa.

minkukjo avatar minkukjo commented on September 23, 2024

@mp911de
Thank you for your kindness answer.
I'm going to try to add sub class of SimpleJpaRepository, JpaRepositoryFactory.
Have a good day!

from spring-data-jpa.

minkukjo avatar minkukjo commented on September 23, 2024

@mp911de
Hi, I have a one more question about "wrapper for CrudMethodMetadata"
I'm using JPA 2.7 version.

I made a subclass of SimpleJpaRepository so I succeeded to change base repository SimpleJpaRepository to CustomJpaRepository that I made.

And then I override setRepositoryMethodMetadata to apply Custom CrudMethodMetadata.

But CrudMethodMetadata is always null when I debug setRepositoryMethodMetadata when spring is about start.

How Can I make wrapper for CrudMethodMetadata?

@Repository
public class CustomCrudRepository<T, ID> extends SimpleJpaRepository<T, ID> {

  public CustomCrudRepository(
      JpaEntityInformation<T, ?> entityInformation,
      EntityManager entityManager) {
    super(entityInformation, entityManager);
  }

  @Autowired
  public CustomCrudRepository(Class<T> domainClass, EntityManager em) {
    this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em);
  }

  @Override
  public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { // <- parameter is always null
    CrudMethodMetadata repositoryMethodMetadata = super.getRepositoryMethodMetadata();

    repositoryMethodMetadata.getMethod().getName();

    if (repositoryMethodMetadata instanceof CustomCrudMethodMetadata) {
      System.out.println("True!");
    }

    if (crudMethodMetadata instanceof CustomCrudMethodMetadata) {
      System.out.println("True!");
    }
    super.setRepositoryMethodMetadata(crudMethodMetadata);
  }
}

I copied DefaultCrudMethodMetadata to make custom CrudMethodMetadata like below

public class CustomCrudMethodMetadata implements CrudMethodMetadata {

  @Nullable
  private final LockModeType lockModeType;
  private final QueryHints queryHints;
  private final QueryHints queryHintsForCount;
  private final Optional<EntityGraph> entityGraph;
  private final Method method;

  CustomCrudMethodMetadata(Method method) {
    Assert.notNull(method, "Method must not be null!");
    this.lockModeType = findLockModeType(method);
    this.queryHints = findQueryHints(method, (it) -> true);
    this.queryHintsForCount = findQueryHints(
        method, org.springframework.data.jpa.repository.QueryHints::forCounting);
    this.entityGraph = findEntityGraph(method);
    this.method = method;
  }

  private static Optional<EntityGraph> findEntityGraph(Method method) {
    return Optional.ofNullable(
        AnnotatedElementUtils.findMergedAnnotation(method, EntityGraph.class));
  }

  @Nullable
  private static LockModeType findLockModeType(Method method) {
    Lock annotation = AnnotatedElementUtils.findMergedAnnotation(method, Lock.class);
    return annotation == null ? null : (LockModeType) AnnotationUtils.getValue(annotation);
  }

  private static QueryHints findQueryHints(Method method,
      Predicate<org.springframework.data.jpa.repository.QueryHints> annotationFilter) {
    MutableQueryHints queryHints = new MutableQueryHints();
    org.springframework.data.jpa.repository.QueryHints queryHintsAnnotation = (org.springframework.data.jpa.repository.QueryHints) AnnotatedElementUtils.findMergedAnnotation(
        method, org.springframework.data.jpa.repository.QueryHints.class);
    if (queryHintsAnnotation != null && annotationFilter.test(queryHintsAnnotation)) {
      QueryHint[] var4 = queryHintsAnnotation.value();
      int var5 = var4.length;

      for (int var6 = 0; var6 < var5; ++var6) {
        QueryHint hint = var4[var6];
        queryHints.add(hint.name(), hint.value());
      }
    }

    QueryHint queryHintAnnotation = AnnotationUtils.findAnnotation(method, QueryHint.class);
    if (queryHintAnnotation != null) {
      queryHints.add(queryHintAnnotation.name(), queryHintAnnotation.value());
    }

    return queryHints;
  }

  @Nullable
  public LockModeType getLockModeType() {
    return this.lockModeType;
  }

  public QueryHints getQueryHints() {
    return this.queryHints;
  }

  public QueryHints getQueryHintsForCount() {
    return this.queryHintsForCount;
  }

  public Optional<EntityGraph> getEntityGraph() {
    return this.entityGraph;
  }

  public Method getMethod() {
    return this.method;
  }
}

How Can I customize CrudMethodMetadata to apply global query hint in SimpleJpaRepository?

Could you give me more hint to achieve my goal that is made global query comment in simpleJpaRepository

I was so appreciated your answer. Thanks a lot!! 😄

from spring-data-jpa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.