0

im still new with java and spring concept, I have no previous experience with spring boot, here im trying to fetch the data from mysql row by row where status ==o with table named as Offers, but im keep getting the below error :

error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-10-16 16:29:51.946 ERROR 16984 --- [ main] o.s.boot.SpringApplication
: Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo' defined in com.example.accessingdatajpa.AccessingDataJpaApplication: Unsatisfied dependency expressed through method 'demo' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.accessingdatajpa.Offers com.example.accessingdatajpa.OffersRepository.findByMsisdn(java.lang.String)! Unable to locate Attribute with the the given name [msisdn] on this ManagedType [com.example.accessingdatajpa.Offers] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]

Offers:

@Entity
@Table(name = "Offers")
public class Offers {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "Msisdn")
   private String Msisdn;

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

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

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

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

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

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

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

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

   @Column(name = "Process_Date")
   private String Process_Date;
   //Setters and getters

    @Override
    public String toString() {
        return String.format(
                "Offers[Msisdn='%s', Entry_Date='%s', Start_Date='%s', End_Date='%s', Service_Type='%s', Status='%s', Parm_1='%s', Parm_2='%s', Parm_3='%s',Process_Date='%s']",
                Msisdn, Entry_Date, Start_Date, End_Date, Service_Type, Status, Parm_1,Parm_2,Parm_3,Process_Date);
## then getter ..
    }

Offersrepository:

package com.example.accessingdatajpa;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface OffersRepository extends CrudRepository<Offers, String> {

    List<Offers> findByStatus(String Status);

    Offers findByMsisdn(String Msisdn);
}

AccessingDataJpaApplication:

package com.example.accessingdatajpa;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class AccessingDataJpaApplication {

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

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

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

            // fetch customers where flag ==0
            log.info("Offers found with findByStatus('0'):");
            log.info("--------------------------------------------");
            repository.findByStatus("0").forEach(on -> {
                log.info(on.toString());
            });
            // for (Customer bauer : repository.findByLastName("Bauer")) {
            //  log.info(bauer.toString());
            // }
            log.info("");
        };
    }

}

OffersRepositoryTests:

ackage com.example.accessingdatajpa;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJpaTest
public class OffersRepositoryTests {
    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private OffersRepository offer;

    @Test
    public void testFindByStatus() {
        Offers Offer = new Offers();
        entityManager.persist(Offer);

        List<Offers> findByStatus = offer.findByStatus(Offer.getStatus());

        assertThat(findByStatus).extracting(Offers::getStatus).containsOnly(Offer.getStatus());
    }
}
yong shi
  • 55
  • 1
  • 2
  • 10
  • Hi, please try to using `uncapitalize` words when defining variables, parameters, methods, `Java` has conventions for them. https://www.oracle.com/technetwork/java/codeconventions-150003.pdf on **9.- Naming Conventions** – Jonathan JOhx Oct 16 '19 at 21:21

2 Answers2

0

Change the name of the field Msisdn to msisdn.

In fact, I would rename all of your fields to follow standard java naming conventions.

mad_fox
  • 3,030
  • 5
  • 31
  • 43
  • thank you its worked with me , 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:39
0

Try adding the annotation @Repository on your OfferRepository

like that

@Repository
public interface OffersRepository extends CrudRepository<Offers, String> {

    List<Offers> findByStatus(String Status);

    Offers findByMsisdn(String Msisdn);
}

The issue you have at the moment is that spring does not add the OffersRepository in the context. So its not Autowired

Unsatisfied dependency expressed through method 'demo' parameter 0
Manolis Proimakis
  • 1,787
  • 16
  • 22
  • 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:40