Skip to content
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

[BUG] - "join" is not performed for @ManyToOne fields when using Fetcher #893

Open
storympro opened this issue Jan 20, 2025 · 1 comment
Labels
enhancement New feature or request pending Postpone plans because of more important tasks

Comments

@storympro
Copy link

Jimmer Version

0.9.2.4

JDK Version

23

Database

PostgreSQL

OS

Windows

Expected behavior

I expect the orm to use "join" when executing the query if the relationship between the tables is @manytoone, because the row will always remain one for the original entity.

Actual behavior

ORM executes incremental queries instead of merging everything at once. I searched the documentation and examples for this case, but I didn't find a solution. Maybe I'm not using the library correctly.

Description

Function for search entity

  public Website find(WebsiteConditions filterCondition, WebsiteFetcher fetcher) throws WebsiteNotFoundException {
        if (filterCondition == null || filterCondition.getId() == null) {
            throw new WebsiteNotFoundException();
        }

        WebsiteTable table = WebsiteTable.$;

        Website website = sqlClient
                .filters(cfg -> {
                    if (filterCondition.notDeleted()) {
                        cfg.setBehavior(LogicalDeletedBehavior.IGNORED);
                    }
                })
                .createQuery(table)
                .where(table.id().eq(filterCondition.getId()))
                .whereIf(filterCondition.notDeleted(), table.deletedAt().isNull())
                .whereIf(filterCondition.onlyDeleted(), table.deletedAt().isNotNull())
                .select(table.fetch(fetcher))
                .distinct()
                .execute()
                .getFirst();

        if (website == null) {
            throw new WebsiteNotFoundException(filterCondition.getId());
        }

        return website;
    }

Next function that use call find (this for example, maybe this is not the best example)

 public void checkPermission(Long idWebsite, String permission) throws WebsiteForbiddenException {
        try {
            Website website = find(
                    new WebsiteConditions(idWebsite),
                    WebsiteFetcher.$
                            .user(UserFetcher.$
                                    .role(RoleFetcher.$
                                            .identityName()
                                            .permissions())));
           ... next other code
        } catch (WebsiteNotFoundException e) {
            throw new WebsiteForbiddenException();
        }
    }

Reproduction steps

use entities with @manytoone and sequentially combine them through Fetcher. For example:

  WebsiteFetcher.$.user(UserFetcher.$
                              .role(RoleFetcher.$
                                      .identityName()
                                      .permissions())));

The website has an owner (user_id), the owner has a role(role_id)

Generated SQL

2025-01-20T18:51:02.735+02:00 INFO 4600 --- [nio-8080-exec-4] o.b.jimmer.sql.runtime.ExecutorForLog : Execute SQL===>
Purpose: QUERY
SQL: select distinct
tb_1_.ID,
tb_1_.id_user
from websites tb_1_
where
tb_1_.ID = ? /* 6 /
and
tb_1_.deleted_at is null
JDBC response status: success
Time cost: 11ms
<===Execute SQL
2025-01-20T18:51:02.749+02:00 INFO 4600 --- [nio-8080-exec-4] o.b.jimmer.sql.runtime.ExecutorForLog : Execute SQL===>
Purpose: LOAD
SQL: select
tb_1_.ID,
tb_1_.id_role
from users tb_1_
where
tb_1_.ID = ? /
9 /
JDBC response status: success
Time cost: 2ms
<===Execute SQL
2025-01-20T18:51:02.754+02:00 INFO 4600 --- [nio-8080-exec-4] o.b.jimmer.sql.runtime.ExecutorForLog : Execute SQL===>
Purpose: LOAD
SQL: select
tb_1_.ID,
tb_1_.identity_name,
tb_1_.PERMISSIONS
from users_roles tb_1_
where
tb_1_.ID = ? /
6 */
JDBC response status: success
Time cost: 3ms
<===Execute SQL

Relation Model

No response

Screenshots

No response

Logs

No response

@storympro storympro added the bug Something isn't working label Jan 20, 2025
@babyfish-ct babyfish-ct added enhancement New feature or request and removed bug Something isn't working labels Jan 20, 2025
@babyfish-ct
Copy link
Owner

This is a major improvement that is already in the 1.0 queue, and it has a high priority, so just wait patiently.

@babyfish-ct babyfish-ct added the pending Postpone plans because of more important tasks label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pending Postpone plans because of more important tasks
Projects
None yet
Development

No branches or pull requests

2 participants