QueryDsl: argument type mismatch exception with bean and oneToMany or manyToMany projection

I have manyToMany association between User and Role objects ( User > --- < Role )

I wanted to fulfill this query:

 createQuery() .from(qUser) .leftJoin(qUser.roles, qRole) .where(qUser.login.eq(login)) .singleResult( Projections.bean(User.class, qUser.id, qUser.login, qUser.password, GroupBy.set(Projections.bean(Role.class, qRole.id, qRole.code )).as(qUser.roles) ) ); 

The generated request looks like this, for me it is perfect:

 SELECT user0_.ID AS col_0_0_, user0_.LOGIN AS col_1_0_, user0_.PASSWORD AS col_2_0_, role2_.ID AS col_4_0_, role2_.CODE AS col_5_0_ FROM public.USER user0_ LEFT OUTER JOIN public.USER_ROLE roles1_ ON user0_.ID=roles1_.USER_ID LEFT OUTER JOIN public.ROLE role2_ ON roles1_.ROLE_ID=role2_.ID WHERE user0_.LOGIN=? LIMIT ? 

But I have java.lang.IllegalArgumentException: argument type mismatch .

I was debugging, and I found out that the data from the database id is loading without problems. This is when QueryDsl / Hibernate did some introspection to create and initialize my objects that throw an exception.

The problem is that the User.setRoles(Set<Role>) method called with the parameter long : the identifier of the first list of Role User entities. Instead of creating Set of Role a, then map these roles to User .

Is there a problem with the request? Or is it not supported by QueryDsl?

I am using QueryDsl 3.6.6 (I tested with 3.7.4: same result)

+6
source share
2 answers

I believe that java.lang.IllegalArgumentException: argument type mismatch is not thrown when comparing JOINs , and you can verify that by checking the ID type of the three tables UTILISATEUR, ROLE and USER_ROLE .

  • If there is a difference between the types

UTILISATEUR.ID = USER_ROLE.USER_ID

or

USER_ROLE..ROLE_ID = ROLE.ID

so the problem.

  1. If the problem does not occur, an exception is raised by checking for equality of entry.

But I suspect that the join table USER_ROLE is not joining with the correct table. You can have two tables USER and UTILISATEUR. If you did not rename the connection table.

0
source

I had the same error as yours, I try different methods, and it took a lot of time. In the end, I founded this path, My activities:

  • Lesson
  • LessonScores to save user points
  • LessonScoresModel to return data and
  • LessonScoresModel.ScoresModel is a static nested class

I need information about user scores in the class. I use the below query to return data. I use exact coupled models to reconfigure the data, but this gives the beautiful "argument type mismatch" error. Therefore, I developed a static class to return data.

 JPAQuery query = new JPAQuery(em); //select from which model you need query.from(QLessonScores.lessonScores); //use your condition query.where(predicate); //use query and returning data Map<Lesson,List<LessonScoresModel.ScoresModel>> map = (Map<Lesson,List<LessonScoresModel.ScoresModel>>) //use tranform for making group by query.transform(GroupBy.groupBy(QLessonScores.lessonScores.lesson).as( //return list of data which column we need GroupBy.list(Projections.fields(LessonScoresModel.ScoresModel.class, QLessonScores.lessonScores.score.as("score"), QLessonScores.lessonScores.scoresType.as("scoresType"), QLessonScores.lessonScores.success.as("success") )) )); 

You will find more information in these links, CODE programcreek

0
source

Source: https://habr.com/ru/post/1011708/


All Articles