antacerod
antacerod

Reputation: 1420

Native select...in() clause with Spring Data JPA

I have found that using a very common query like this:

@Query(nativeQuery=true, value="select * from questions where id in (22,45,65,777,444)") results in a list of objects instead of a list of questions object.

[[Ljava.lang.Object;@49fd8d8f, [Ljava.lang.Object;@2558cd3c, [Ljava.lang.Object;@690a314b]

How is this situation going to be fixed?

How can I manage this right now?

Thanks!

Edit:

@RooJpaRepository(domainType = Challenge.class)
public interface ChallengeRepository extends ChallengeRepositoryCustom
{
    @Query(nativeQuery=true, value="select * from questions where id in (22,45,65,777,444)")
    List<Questions> chooseRandomOldQuestions();
}

Upvotes: 1

Views: 2188

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83171

I strongly feel like this has nothing to do with the in clause but the types you use.

Your repository is typed to the Challenge domain class. Question (I guess you mean that rather than Questions) is probably not a sub type of Challenge right? If this is the case we do not hand the domain type to the EntityManager to accommodate scenarios where people execute projections to Long etc. and the query execution would fail if we handed a non-managed type to the query execution.

The workaround is to create a dedicated repository managing Question(s) instances (still not sure this is a typo or a separate class) should do the trick.

Upvotes: 1

Related Questions