title: Spring MVC CrudRepository findByIn
body: <p>I have a CrudRepository that is supposed to make a query with an array (<code>findByIn</code>). In my repository tests it works, but when I try to use the query in my service, it doesn't work. Could someone explain why it doesn't work? Here is my setup (excluding some code irrelevant to the question)</p>
  
  <p>Database model:</p>
  
  <pre><code>@Entity
  @Table(name="Place")
  public class Place implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "placeId", nullable = false)
      private Long placeId;
  
      @Column(name = "owner", nullable = false)
      private String owner;
  
      public Long getPlaceId() {
      return placeId;
      }
  
      public void setPlaceId(Long placeId) {
          this.placeId = placeId;
      }
  
      public String getOwner() {
          return owner;
      }
  
      public void setOwner(String owner) {
          this.owner = owner;
      }
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>@Repository
  public interface PlaceRepository extends CrudRepository&lt;Place, Long&gt; {
      List&lt;Place&gt; findByPlaceIdIn(Long[] placeId);
  }
  </code></pre>
  
  <p>Service (this is the part not working):</p>
  
  <pre><code>@Service
  public class PlaceService {
  
      @Autowired
      private PlaceRepository placeRepository;
  
      public List&lt;Place&gt; getPlaces(Long[] placeIds) {
          return placeRepository.findByPlaceIdIn(placeIds);
      }
  }
  </code></pre>
  
  <p>The problem is that in my service <code>placeRepository.findByPlaceIdIn(placeIds)</code> returns 0 objects if <code>placeIds</code> contains more than one item. If <code>placeIds</code> contains just one item, the query works fine. I tried replacing <code>return placeRepository.findByPlaceIdIn(placeIds)</code> with this piece of code that does the query for every array item one by one (this actually works, but I'd like to get the query work as it should):</p>
  
  <pre><code>    ArrayList&lt;Place&gt; places = new ArrayList&lt;Place&gt;();
      for (Long placeId : placeIds) {
          Long[] id = {placeId};
          places.addAll(placeRepository.findByPlaceIdIn(id));
      }
      return places;
  </code></pre>
  
  <p>I know that the repository should work, because I have a working test for it:</p>
  
  <pre><code>public class PlaceRepositoryTest {
  
      @Autowired
      private PlaceRepository repository;
  
      private static Place place;
      private static Place place2;
      private static Place otherUsersPlace;
  
      @Test
      public void testPlacesfindByPlaceIdIn() {
          place = new Place();
          place.setOwner(USER_ID);
  
          place2 = new Place();
          place2.setOwner(USER_ID);
  
          place = repository.save(place);
          place2 = repository.save(place2);
          Long[] ids = {place.getPlaceId(), place2.getPlaceId()};
          assertEquals(repository.findByPlaceIdIn(ids).size(), 2);
      }
  }
  </code></pre>
  
  <p>I also have another repository for other model, which also uses <code>findByIn</code> and it works fine. I can't see any relevant difference between the repositories. I thought it might offer some more details to show the working repository, so I included it below:</p>
  
  <p>Database model:</p>
  
  <pre><code>@Entity
  @Table(name="LocalDatabaseRow")
  @JsonIgnoreProperties(ignoreUnknown=false)
  public class LocalDatabaseRow implements Serializable {
      public LocalDatabaseRow() {}
  
      public LocalDatabaseRow(RowType rowType) {
          this.rowType = rowType;
      }
  
      public enum RowType {
          TYPE1,
          TYPE2
      };
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      @JsonProperty("id")
      private Long id;
  
      @JsonProperty("rowType")
      @Column(name = "rowType")
      private RowType rowType;
  
      public Long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public RowType getRowType() {
          return rowType;
      }
      public void setRowType(RowType rowType) {
          this.rowType = rowType;
      }
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>@Repository
  public interface LocalDatabaseRowRepository extends CrudRepository&lt;LocalDatabaseRow, Long&gt; {
      List&lt;LocalDatabaseRow&gt; findByRowTypeAndUserIdIn(RowType type, String[] userId);
  }
  </code></pre>