0

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
yong shi
  • 55
  • 1
  • 2
  • 10

1 Answers1

1

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
dasunse
  • 2,839
  • 1
  • 14
  • 32
  • thank you its worked fine , but now as i understand i have to create date object to be alighned with my query , can show me how ? – yong shi Oct 23 '19 at 06:03
  • could you please explain more.. Are you asing about this type of scenario. @Query("SELECT b.msisdn from Bulk_repository b where b.status = ?1 ") List getBylkRepositoryByStatus(int status); Are you asking about how status have passed... is it this type of scenario.. – dasunse Oct 23 '19 at 06:16
  • im just trying to represent the above query , i have updated my question with query , but im getting problem about how to set date object – yong shi Oct 23 '19 at 07:22
  • @yongshi it is better to ask a new question. People who view this question again may confusing with question and answers. because you have asked an edited question and the answers may be for your previous question. – dasunse Oct 23 '19 at 08:05
  • Instead of giving this much long method name, you can use QueryDSL, Specification, CriteriaQuery – Ravirajsinh Vaghela Oct 23 '19 at 08:16
  • can you help me with this question https://stackoverflow.com/questions/58582177/jpa-with-https-request-multithreading-spring/58584987#58584987 – yong shi Oct 28 '19 at 08:38