44

I was trying to use Spring Data JPA on Spring Boot and I kept getting error, I can't figure out what the problem is:

Unable to locate Attribute with the the given name [firstName] on this ManagedType [com.example.h2demo.domain.Subscriber]

FirstName is declared in my entity class. I have used a service class with DAO before with different project and worked perfectly.

My Entity class (getters and setters are also in the class) :

        @Entity
        public class Subscriber {

        @Id @GeneratedValue
        private long id;
        private String FirstName,LastName,Email;

        public Subscriber(long id, String firstName, String lastName, String email) {
            this.id = id;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Email = email;
          }
        }
...

My Repository Class

@Component
public interface SubscriberRepository extends JpaRepository<Subscriber,Long> {
    Subscriber findByFirstName(String FirstName);
    Subscriber deleteAllByFirstName(String FirstName);
}

My Service Class

@Service
public class SubscriberService {

    @Autowired
    private SubscriberRepository subscriberRepository;

    public Subscriber findByFirstName(String name){
        return  subscriberRepository.findByFirstName(name);

    }

    public Subscriber deleteAllByFirstName(String name){
        return  subscriberRepository.deleteAllByFirstName(name);

    }

    public void addSubscriber(Subscriber student) {
        subscriberRepository.save(student);
    }
}

And My Controller class:

@RestController
@RequestMapping("/subscribers")
public class SubscriberController {

    @Autowired
    private SubscriberService subscriberService;

    @GetMapping(value = "/{name}")
    public Subscriber findByFirstName(@PathVariable("name") String fname){
        return  subscriberService.findByFirstName(fname);
    }

    @PostMapping( value = "/add")
    public String insertStudent(@RequestBody final Subscriber subscriber){
        subscriberService.addSubscriber(subscriber);
        return "Done";
    }

}
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
smned
  • 663
  • 1
  • 6
  • 19

4 Answers4

54

Try changing private String FirstName,LastName,Email; to private String firstName,lastName,email;

It should work.

findByFirstName in SubscriberRepository tries to find a field firstName by convention which is not there.

Further reference on how properties inside the entities are traversed https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

harsh
  • 1,471
  • 13
  • 12
  • 1
    Can i ask one more question? Sending JSON POST to addSubscriber method gives null values to stored to database. Is there any error in the code?? – smned Mar 16 '18 at 11:48
  • try checking the values before calling `addSubscriber` – harsh Mar 16 '18 at 12:48
  • 1
    In my opinion the accepted answer should suggest the method to identify the problem in a general way; this answer is only debugging this specific code: soft of pair-programming – lrkwz Dec 20 '20 at 16:51
14

The same problem was when i had deal with Spring Data Specifications (https://www.baeldung.com/rest-api-search-language-spring-data-specifications)

Initial piece of code was:

private Specification<Project> checkCriteriaByProjectNumberLike(projectNumber: String) {
    (root, query, criteriaBuilder) -> criteriaBuilder.like(root.get("project_number"), "%" + projectNumber)
}

The problem was in root.get("project_number"). Inside the method, I had to put the field name as in the model (projectNumber), but I sent the field name as in the database (project_number).

That is, the final correct decision was:

private Specification<Project> checkCriteriaByProjectNumberLike(projectNumber: String) {
    (root, query, criteriaBuilder) -> criteriaBuilder.like(root.get("projectNumber"), "%" + projectNumber)
}
S.Daineko
  • 1,790
  • 1
  • 20
  • 29
6

As per the specification, the property names should start with lower case.

[...]The resolution algorithm starts with interpreting the entire part (AddressZipCode) as the property and checks the domain class for a property with that name (uncapitalized)[...].

It will try to find a property with uncapitalized name. So use firstName instead of FirstName and etc..

serv-inc
  • 35,772
  • 9
  • 166
  • 188
JPS
  • 2,730
  • 5
  • 32
  • 54
6

After I change my entity class variables from capital letter to small letter for instance Username to username the method Users findByUsername(String username); is working for me now .

Osama Al-Banna
  • 1,465
  • 5
  • 20
  • 33