-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
HHH-17706 Optimize FK comparison to eliminate unnecessary left join #7782
Conversation
Fix regression introduced by ef155c2
As I mentioned in more details on the Jira, we opted to avoid optimizing away explicit joins (see discussion on previous pr) to prevent problematic queries. I also fear the checks you're using won't be enough to ensure we can avoid using the target side key to create correct queries, except for very simple cases like the test you're introducing. Closing this PR for now. |
@quaff sure, I'm getting in touch with our Db2 team for a more detailed analysis – will get back to you as soon as I have the details. |
But .... why wouldn't you guys just change the query to use |
@gavinking I'm totally with you if I would define the query manually but the case I originally reported is spring-projects/spring-data-jpa#3349 so Spring Data JPA generates the query. That's why I was asking if an enhancement should be considered in there (spring-projects/spring-data-jpa#3349 (comment)) but obviously workarounds are possible in the client code, like spring-projects/spring-data-jpa#3349 (comment) where |
Look, I'm just saying..... package org.example;
import org.hibernate.StatelessSession;
import org.hibernate.annotations.processing.Find;
import java.util.List;
interface BookRepository {
StatelessSession session();
@Find
List<Book> findAllByPublisher(long publisher$id);
} Generated by annotation processor: package org.example;
import jakarta.annotation.Generated;
import jakarta.annotation.Nonnull;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;
import jakarta.persistence.metamodel.StaticMetamodel;
import java.util.List;
import org.hibernate.StatelessSession;
@Dependent
@StaticMetamodel(BookRepository.class)
@Generated("org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
public class BookRepository_ implements BookRepository {
private final @Nonnull StatelessSession entityManager;
@Inject
public BookRepository_(@Nonnull StatelessSession entityManager) {
this.entityManager = entityManager;
}
public @Nonnull StatelessSession session() {
return entityManager;
}
/**
* Find {@link Book} by {@link Book#publisher.id publisher.id}.
*
* @see org.example.BookRepository#findAllByPublisher(long)
**/
@Override
public List<Book> findAllByPublisher(long publisher$id) {
var builder = entityManager.getFactory().getCriteriaBuilder();
var query = builder.createQuery(Book.class);
var entity = query.from(Book.class);
query.where(
builder.equal(entity.get(Book_.publisher).get(Publisher_.id), publisher$id)
);
return entityManager.createQuery(query).getResultList();
}
} Not only can freely rename the method SQL: select
b1_0.isbn,
b1_0.duration,
b1_0.price,
b1_0.publication_date,
b1_0.publisher_id,
b1_0.quantitySold,
b1_0.text,
b1_0.title,
b1_0.type
from
books b1_0
where
b1_0.publisher_id=? But I can't force you to use this. It's something you have to realize for yourself. |
(You can of course use your own preferred flavor of "session" here. I personally like |
Absolutely, I understand that a Hibernate-only solution would work without issues, and I do appreciate the hint! I'm just coming from a codebase strongly connected to Spring Data JPA and I'd like to understand how the Spring Data team sees the current issue before evaluating a tech change, given that there are already other ways in Spring Data to get the right query generated. (I know, Hibernate is already there and features can be used, but maybe you get what I mean 🙂) |
Of course, I understand. |
Fix regression introduced by ef155c2
https://hibernate.atlassian.net/browse/HHH-17706