JPA passing list to IN clause in named native query

前端 未结 10 1072

I know I can pass a list to named query in JPA, but how about NamedNativeQuery? I have tried many ways but still can\'t just pass the list to a NamedNativeQuery. Anyone know

10条回答
  •  隐瞒了意图╮
    2020-11-30 07:49

    In my case ( EclipseLink , PostGreSQL ) this works :

        ServerSession serverSession = this.entityManager.unwrap(ServerSession.class);
        Accessor accessor = serverSession.getAccessor();
        accessor.reestablishConnection(serverSession);
        BigDecimal result;
        try {
            Array jiraIssues = accessor.getConnection().createArrayOf("numeric", mandayWorkLogQueryModel.getJiraIssues().toArray());
            Query nativeQuery = this.entityManager.createNativeQuery(projectMandayWorkLogQueryProvider.provide(mandayWorkLogQueryModel));
            nativeQuery.setParameter(1,mandayWorkLogQueryModel.getPsymbol());
            nativeQuery.setParameter(2,jiraIssues);
            nativeQuery.setParameter(3,mandayWorkLogQueryModel.getFrom());
            nativeQuery.setParameter(4,mandayWorkLogQueryModel.getTo());
            result = (BigDecimal) nativeQuery.getSingleResult();
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    
        return result;
    

    Also in query cannot use IN(?) because you will get error like :

    Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: numeric = numeric[]

    'IN(?)' must be swapped to '= ANY(?)'

    My solution was based on Erhannis concept.

提交回复
热议问题