#Springboot- how to check which methods will create derived queries?

1 messages · Page 1 of 1 (latest)

crude zenith
#

Maybe I have a sample class:

public interface ManagerRepository extends EmployeeRepository {
    public List<Manager> findManagerByName(String name);
}```

Or any number of sample queries in this class.
How can I tell for which entries a derived query is being generated?

ChatGPT is telling me to use a `QueryListener`, but I am unable to import `org.springframework.data.repository.core.support.QueryCreationListener`. (from here https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/core/support/QueryCreationListener.html)

The alternative is to use sql logs, but that induces so much spam that I cant see anything being generated.

What to do?
torpid anvilBOT
#

This post has been reserved for your question.

Hey @crude zenith! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

vivid prawn
#

what do you mean with generating derived queries?

dim spire
#

Enabling a few properties in your spring boot settings will dump the sql logs

torpid anvilBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

crude zenith
#
    drop table if exists department cascade 
Hibernate: 
    drop table if exists department cascade 
2023-10-14T12:44:07.109+08:00 DEBUG 9924 --- [           main] org.hibernate.SQL                        : 
    drop table if exists employee cascade 
Hibernate: 
    drop table if exists employee cascade 
2023-10-14T12:44:07.112+08:00 DEBUG 9924 --- [           main] org.hibernate.SQL                        : 
    create table department (
        ...
    )
Hibernate: 
    create table department (
        ...
    )
2023-10-14T12:44:07.130+08:00 DEBUG 9924 --- [           main] org.hibernate.SQL                        : 
    create table employee (
        ...
    )
Hibernate: 
    create table employee (
        ...
    )
2023-10-14T12:44:07.133+08:00 DEBUG 9924 --- [           main] org.hibernate.SQL                        : 
    alter table if exists employee 
       ...
Hibernate: 
    alter table if exists employee 
       ...
    insert 
    into
        department
        ...
    values
        (?,?,?,?,?,?,?,default)
Hibernate: 
    insert 
    into
        department
        ...
    values
        (?,?,?,?,?,?,?,default)
2023-10-14T12:44:08.052+08:00 DEBUG 9924 --- [           main] org.hibernate.SQL                        : 
    insert 
    into
        employee
        ...
    values
        (?,?,?,?,?,?,?,?,?,?,?,?,'Manager',default)
Hibernate: 
    insert 
    into
        employee
        ...
    values
        (?,?,?,?,?,?,?,?,?,?,?,?,'Manager',default)
2023-10-14T12:44:08.114+08:00 DEBUG 9924 --- [           main] org.hibernate.SQL                        : 
    insert 
    into
        employee
        ...
    values
        (?,?,?,?,?,?,?,?,?,?,?,?,'Employee',default)
crude zenith
# dim spire https://www.baeldung.com/sql-logging-spring-boot

even if I do make a call to one of the derived queries while logging is activated, all it does is pump out like 4 waves of unintelligible spam

It doesn't actually show me what the derived SQL is, or which repository method it came from
there's no list of which repository methods are being derived and which are being ignored by spring-boot

    select
        e1_0.id,
        e1_0.user_type,
        ...
        e1_0.username 
    from
        employee e1_0 
    where
        e1_0.username=? 
        and e1_0.deleted=?
Hibernate: 
    select
        e1_0.id,
        ...
        e1_0.user_type,
        e1_0.username 
    from
        employee e1_0 
    where
        e1_0.username=? 
        and e1_0.deleted=?
2023-10-14T12:48:35.193+08:00 DEBUG 21956 --- [           main] org.hibernate.SQL                        : 
    select
        d1_0.id,
        d1_0.created_by,
        ...
        e1_0.username 
    from
        department d1_0 
    left join
        employee e1_0 
            on d1_0.id=e1_0.department_id 
    where
        d1_0.id=?
Hibernate: 
    select
        d1_0.id,
        d1_0.created_by,
        ...
        e1_0.username 
    from
        department d1_0 
    left join
        employee e1_0 
            on d1_0.id=e1_0.department_id 
    where
        d1_0.id=?
dim spire
#

Wait, so you want to know which repository methods spring derived?

crude zenith
# dim spire Wait, so you want to know which repository methods spring derived?

yeah, and which ones it failed to derived
i am trying to use reflection to get it right now

but i am stuck here

JpaRepositoryFactory extends RepositoryFactorySupport

        return AbstractRepositoryMetadata.getMetadata(repositoryInterface);
    }```

Method getRepositoryMetadataMethod = JpaRepositoryFactory.class.getDeclaredMethod("getRepositoryMetadata", Class.class);
getRepositoryMetadataMethod.setAccessible(true);


it keeps throwing .NoSuchMethodException and idgi why
dim spire
#

I don't know if that's even possible

crude zenith
dim spire
#

Derived methods are fine and follow a pattern

#

If you read the docs it'll show you the syntax

#

They just work

#

Unless, and I can't repeat this enough, INTELLIJ HIGHLIGHTS SOMETHING IN YELLOW

crude zenith
#

the repo interfaces feel like a black box honestly like there's no intellisense to tell u if u messed up

dim spire
#

Wait. Are you using community?

crude zenith
#

yea

dim spire
#

Ah... Ultimate does

#

So eeh. Yeah. If you're working on a large spring project in a company ask them for ultimate

#

Otherwise very much double check the method names

#

And don't make them too complicated. If they're too long better to just type jql or sql

crude zenith
#

yea like not knowing how to debug is ever passable
during crunch sometime eventually something will go wrong and shit will hit the fan, this is just coping

dim spire
#

I know. If you write simple JpaTests for your repository methods. You should be fine

honest turret
#

I always get the whole queries (even ones which are like long af)

#

try removing spring.jpa.properties.hibernate.format_sql and seeing if it makes any difference
you won't get which repository executed what. But at least you'll get the full queries (which are usually pretty hard to read)

#

also advice: never let an orm create/overwrite the schema

vivid prawn