STP
STP

Reputation: 51

How do you create a custom Spring JPA query using "IN"?

I have a parameter of integers. I need to write a custom Spring JPA query where the equivalent SQL would be . . . AND x.propertyStatusId IN (1,2) . . .

I see in the Spring Data JPA documentation the keyword is "IN" as in "findByAgeIn(Collection<Age> ages)" that translates to "where x.age in ?1" But I get an error. I am using an arrayList of integers.

ArrayList<Integer> propertyStatusId = new ArrayList<Integer>();
propertyStatusId.add(1);
propertyStatusId.add(2);
findByPropertyOwnerIdAndIn(propertyStatusId)AndListingDateGreaterThanEqual(user.getId(),propertyStatusId, ListingDate, pageable);

Upvotes: 0

Views: 304

Answers (1)

snmaddula
snmaddula

Reputation: 1161

Assuming that you have an entity called Product defined as below:

@Entity
public class Product {

    @Id
    @GeneratedValue
    private Integer id;
    private String title;
    private String description;
    private Date createDate;

// getters & setters 

}

If you would need to query based on title, List of ids and greater than or equal to createDate then you can define the method like below:

List<Product> findByTitleAndIdInAndCreateDateGreaterThanEqual(String title, List<Integer> ids, Date createDate);

Upvotes: 1

Related Questions