How can I generate findBy method corresponding to below query in JPARepository?
select * from person where name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL)
How can I generate findBy method corresponding to below query in JPARepository?
select * from person where name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL)
You can technically do it. But its better to avoid it.
Assuming you entity class is like
class Person {
String name;
Integer effDt;
Integer expDt;
// getters and setters
}
your method name will be
findByNameIsAndEffDtLessThanAndExpDtGreaterThanOrNameIsAndEffDtLessThanAndExpDtIsNull(String name, Integer effDt, Integer expDt, String name2, Integer effDt2);
//name and name2 will be same & effDt and effDt2 will be same.
//Boolean Algebra: A(B+C) = A.B + A.C
As you can see this is needlessly complicated and difficult to understand. Use a native query instead.
select * from person where name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL) Assuming the entity class by the names of the variables
@Entity
public class Person{
@Id
@GeneratedValue
private long id;
private String name;
private Date eff_dt;
private Date exp_dt;
}
The query can be
public final static String FIND_ALL_PERSONS="select s from Person p where p.name=:name and p.eff_dt < :eff_dt and (exp_dt > : exp_dt OR exp_dt is null)";
@Query(FIND_ALL_PERSONS)
List<Person> findAllPersons(@Param("name")String name,@Param("eff_dt") eff_dt,@Param("exp_dt") Date axp_dt);