Hi I am using Spring Data JPA and want to use feature generate query from method name. I have a field active in DB which have only value 0 and 1. I want to fetch all data with which have active value is 1. This is a constant value so i don't want to pass this value as method arguments.
please suggest what will be the method for the same.
example:
I have a entity EmailRef
public class EmailRef {
/* other vareialbe */
@Column(name="is_active") /* this is the field which value is 0 and 1 in DB*/
private Integer active;
/* setter getter method */
}
This is the repository for where I want to write method which will fetch all data for which active is 1;
public interface EmailRefRepositry extends JpaRepository<EmailRef, Long> {
@Query("select * from email_reference where is_active=1") /* this is the query I want to convert into method*/
List<EmailRef> findByActive(); /*I want to write method like that which will fetch all data form table where active field value is 1*/
}
I am stuck for constant vale please suggest
Thanks Sudhanshu
If you could change that Integer to a boolean, you could be doing something like:
In your entity:
private Boolean active;
In your repo:
List<EmailRef> findByActiveIsTrue();
Try this:
public interface EmailRefRepositry extends JpaRepository<EmailRef, Long> {
@Query("select e from EmailRef e where e.active=1")
List<EmailRef> findOnlyActiveWithQuery();
default List<EmailRef> findOnlyActive() {
findByActive(1);
}
default List<EmailRef> findNotActive() {
findByActive(0);
}
List<EmailRef> findByActive(Integer active);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With