java - Issue Related to Specification criteria query Builder in spring boot - Stack Overflow

admin2025-04-18  4

I have total 4 table name is user, user_role, role and user_role_site in user_role_site table i have site_id and USER_ROLE_ID and in user_role table i have USER_ID and ROLE_ID

My requirement is : retrieve user with the HOST role and a matching siteId in user_role_site.

I have an API that retrieves all users using pagination. However, I need to add a parameter site_id. If the frontend sends the site_id, I need to find the users associated with that site, which requires joining four tables by using Specification criteria query Builder class.

In this, the user is joined with the user_role table, meaning one user can have multiple roles. The user_role table is then joined with both the role and user_role_site tables, where user_role has a Many-to-One relationship with the Role table and a One-to-Many relationship with the user_role_site table.

Additionally, I have created a StaticMetamodel for all the above entities and added the following method

private Specification<User> withHostRoleAndSiteId(String siteId) {
        return (root, query, builder) -> {
            // Join User to UserRole (User_.userRoleUserViaUserId is a List<UserRole>, so use ListJoin)
            ListJoin<User, List<UserRole>> userRoleJoin = root.join(User_.userRoleUserViaUserId);

            // Join UserRole to UserRoleSit e (UserRole_.userRoleSiteUserRoleViaUserRoleId is a Set<UserRoleSite>, so use SetJoin)
            SetJoin<UserRole, UserRoleSite> userRoleSiteJoin = userRoleJoin.join(UserRole_.userRoleSiteUserRoleViaUserRoleId);

            Predicate rolePredicate = builder.equal(userRoleJoin.get(UserRole_.roleId.getName()), "HOST");
            Predicate sitePredicate = builder.equal(userRoleSiteJoin.get("siteId_"), siteId);

            return builder.and(rolePredicate);
//            return builder.and(rolePredicate, sitePredicate);
        };
    }

but from

SetJoin<UserRole, UserRoleSite> userRoleSiteJoin = userRoleJoin.join(UserRole_.userRoleSiteUserRoleViaUserRoleId);

line of code i got Cannot resolve method 'join(SetAttribute<UserRole, UserRoleSite>)' error not join table.

Please give me a solution on what I can do and how I can resolve this issue.

转载请注明原文地址:http://www.anycun.com/QandA/1744956136a90000.html