Comments (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.
@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.
@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)
- Upgrade Hibernate 6.6 builds to Final release
- Upgrade to Hibernate 6.6
- Upgraded to Hibernate 6.2.7 along with Spring 3 - Inner joins added along with Left join HOT 1
- findLastChangeRevision doesn't return last revision if two revisions with exactly the same timestamp exist HOT 2
- Could you please provide instructions on how to run the test code for the 'Spring-Data-JPA' module? HOT 7
- Release 3.2.10 (2023.1.10)
- Release 3.3.4 (2024.0.4)
- Not expecting UnexpectedRollbackException HOT 1
- Refactor `instanceof` to use pattern variable
- Consider `NULLS` precedence using `Sort` for Criteria Queries
- Explore using JPQL queries instead of Criteria Queries HOT 5
- Expressions.nullExpresion() in querydsl result in NPE in SqmExpressible HOT 3
- Issue with Null Handling in Oracle SQL Function Called via JPA/Hibernate HOT 3
- Support for sorting by subclass properties inside repository methods HOT 3
- Replace with expression lambda in AbstractStringBasedJpaQuery class
- use @Postcontruct jpa first level cache didn't work HOT 1
- Projections stopped working after upgrading to 3.3.3 HOT 2
- `suspending` support within `JpaRepository` HOT 1
- Reorder documentation of class-based projection support for JPA Native Queries HOT 3
- `limit` in `FetchableFluentQueryBySpecification` addes duplicate `order by` clause HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-jpa.