Skip to content

JPA repository method with native query containing '&&' works with Long[] but not Collection<Long> #3981

@stsypanov

Description

@stsypanov

Originally asked at: https://stackoverflow.com/questions/79740056/jpa-repository-method-with-native-query-containing-works-with-long-but-no

In one of my Spring Data JPA repositories I have this native query (PostgreSQL):

@Query(value = "SELECT * FROM ballots b WHERE b.companies && :companyIds and b.meeting_id in :meetingIds", nativeQuery = true)
List<Ballot> findByIds(Long[] companyIds, Long[] meetingIds);

Here's my entity:

@Table(name="ballots")
public class Ballot {
    @Id
    private long id;

    @Column
    private long meetingId;

    @Type(ListArrayType.class)
    @Column(name = "companies",columnDefinition = "bigint[]")
    private List<Long> companies = new ArrayList<>();
}

Here's how I call the method:

private final BallotRepository ballotRepo;

  public void findBallots(Set<Long> companyIds, Set<Long> companyMeetingIds) {
    List<Ballot> ballots = ballotRepository.findByIds(companyIds, companyMeetingIds);
    // the rest of the code
  }
}

Repository method signature with

  • Long[] companyIds, Long[] meetingIds works fine
  • Long[] companyIds, Collection<Long> meetingIds works fine
  • Collection<Long> companyIds, Collection<Long> meetingIds fails:
org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [SELECT * FROM ballots b WHERE b.companies && (?) and b.meeting_id in (?,?)] [ERROR: operator does not exist: bigint[] && bigint
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
  Position: 42] [n/a]; SQL [n/a]

I wonder why the framework successfully converts Long[] into bigint[] but fails when in comes to Collection<Long>

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions