yong shi
yong shi

Reputation: 65

jpa findby with list of conditions

I am still a beginner with java, here I'm trying to fetch the data from mysql database using JPA.

but I'm getting a problem with setting the list of conditions with find by function. i can set Object of date even it refused the inputs below is my code for reference.

my query :

select t.id, t.MSISDN, t.Param1, t.param2
  from BULK_REPOSITORY t
where t.Camp_Start_Date between Sysdate - 2 and sysdate
   and t.status = 0
   and t.camp_type = 1;

Application :

@SpringBootApplication
public class AccessingDataJpaApplication {

    private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
    Bulk_repository bulk ;


    public static void main(String[] args) {
        SpringApplication.run(AccessingDataJpaApplication.class);
    }


    @Bean
    public CommandLineRunner demo(Bulk_repositoryRepository repository) {
        return (args) -> {

            // fetch customers by Status
            log.info("Customer found with findByStatus('0'):");
            log.info("--------------------------------------------");
            repository.findAllByStatusAndCampTypeAndCampStart_dateBetween(1,2,Date,Date-2).forEach(on -> {
                log.info(on.toString());
            });
        };
    }

Bulk_repositoryRepository class :

    import org.springframework.data.repository.CrudRepository;

    public interface Bulk_repositoryRepository extends CrudRepository<Bulk_repository, Long> {
          List<Bulk_repository>  findAllByStatusAndCampTypeAndCampStart_dateBetween(int status, int campType,Date campStart_dateStart, Date campStart_dateEnd);
         Bulk_repository findById(long id);
    } 

Bulk_repository:

@Entity
@Table(name = "BULK_REPOSITORY")
public class Bulk_repository {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "id")
   private long id;

   @Column(name = "msisdn")
   private String msisdn;

   @Column(name = "camp_start_date")   
   private Date campStartDate;

   @Column(name = "camp_end_date")
   private Date campEndDate;

   @Column(name = "camp_type")
   private int campType;

   @Column(name = "camp_cd")
   private String camp_cd;

   @Column(name = "status")
   private int status;

   @Column(name = "process_date")
   private Date processDate;

   @Column(name = "entry_date")
   private Date entryDate;

   @Column(name = "entry_user")
   private String entry_user;

   @Column(name = "param1")
   private String param1;

   @Column(name = "param2")
   private String param2;

   @Column(name = "param3")
   private String param3;

   @Column(name = "param4")
   private String param4;

   @Column(name = "param5")
   private String param5;

   @Column(name = "error_desc")
   private String error_desc;

   @Column(name = "fulfilment_status")
   private int fulfilment_status;
## getter and setter and ToString

Upvotes: 0

Views: 2166

Answers (1)

dasunse
dasunse

Reputation: 3099

Use this way,

Escape the _ by using an additional underscore

List<Bulk_repository>  findAllByStatusAndCamp__typeAndCamp__start__dateBetween(int status, int camp_type,Date camp_start_dateStart, Date camp_start_dateEnd);

I recommond to use this way in your model class. If so you have to change your repository according to this.

@Column(name = "camp_start_date")   
private Date campStartDate; // better to use this way

Upvotes: 1

Related Questions