user1595858
user1595858

Reputation: 3890

Spring JPA to query using "IN" in select statement

I'm trying to query the table based on two status for example status1 or status2. How should I achieve in one SQL statement using Spring JPA.

ws = consultSessionRepository.findByShareIdAndStatus(consultId,
                    consultStatus.active);

public interface ConsultSessionRepository extends JpaRepository<consultSession, Long>  {

    @Query("select ws from consultSession ws where consultId = ?")
    consultSession findByconsultid(String consultId);

    @Query("select ws from consultSession ws where shareId = ?")
    List<consultSession> findByShareId(String shareId);

    consultSession findByShareIdAndStatus(String shareId, consultStatus satus);

}

Trying to achieve something like below

select * from consultSession where status in ('ACTIVE', 'ACTIVE1') and shareid = '<share id>

Upvotes: 1

Views: 2045

Answers (1)

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34826

You can construct the query using a method name, which follows the rules mentioned in the documentation below, like this:

consultSession findByShareIdAndStatusIn(String shareId, consultStatus... states);

or using @Query annotation, which is IMHO more readable:

@Query("FROM consultSession cs WHERE shareId = ?1 AND status IN (?2)")
List<Foo> find(String shareId, consultStatus... states);

Btw, I assume this consultId in ('ACTIVE', 'ACTIVE1') is a typo and it should be status instead of the consultId.


References

Upvotes: 2

Related Questions