Criteria - Sort by predicate with Join

Asked

Viewed 86 times

0

Hello, I’m trying to make an ordination in Criteria for the following query:

SELECT *
FROM Social social
         LEFT JOIN socialfavorite favorite ON (social.id = favorite.favoritedSocial_id AND favorite.user_id = 2)
ORDER BY favorite.id desc;

I transformed the sort part using the Criteria API:

Join<SocialUser, SocialFavorite> favorite = socialUser.join("socialFavorites");

                    favorite.on(builder.and(
                            builder.equal(socialUser.get("id"), favorite.get("favoritedSocial").get("id")),
                            builder.equal(favorite.get("user").get("alias"), search.getCurrentUserAlias())
                    ));

                    if (orderParam.getType().equals(OrderType.ASC)) {
                        return builder.asc(favorite.get("id"));
                    }
                    return builder.desc(favorite.get("id"));

However, whenever Boer executes the orderBy the following error occurs:

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'socialuser2_.alias' in 'on clause'
  • socialuser2_.alias is being informed where in the query?

  • It is mounted by Criteria, probably when I do Join, but I found it very strange that he "gets lost" in this

  • Yes, which is why I wondered, I took a look at the Criteria documentation last night, even in the examples I found on the net, I didn’t find anything like your... I’m trying to find a solution here, if I can I put it to you

  • Man, thank you so much. I’ve been trying several solutions for days, I even tried with subquery but gave the same error

1 answer

0

Solution:

When this is in the Join clause in the Criteria API, it is only possible to select your entity’s primary key, making the other properties inaccessible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.