1

So I'm experimenting with query-methods (if that's what they're called) and for some reason I'm not able to start my application because the way I name my method is still wrong.

@Repository("PersonJPA")
public interface PersonRepo extends JpaRepository<Person, Long> {

    //@Query("SELECT p FROM Person p WHERE p.house.houseId = ?1")
    List<Person> findPeopleByHouse_Id(long house_id);
}

Normally I would use the @Query, but my teacher told me you could just name it differently and it works too, but I'm struggling to see the problem with my method.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'houseController' defined in file [C:\Users\Z.R. Cai\Documents\[S4] Software\Fundamentals\s4-software-FUN-backend\FUN4 backend\target\classes\com\example\demo\api\HouseController.class]: Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'houseService' defined in file [C:\Users\Z.R. Cai\Documents\[S4] Software\Fundamentals\s4-software-FUN-backend\FUN4 backend\target\classes\com\example\demo\service\HouseService.class]: Unsatisfied dependency expressed through constructor parameter 1; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PersonJPA': Invocation of init method failed; 
nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.demo.repo.PersonRepo.findPeopleByHouse_Id(long)! 
Unable to locate Attribute  with the the given name [id] on this ManagedType [com.example.demo.model.House]

Also tried to use this as sort of a reference: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

Entities:

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(unique = true)
    private long personId;

    @Column(nullable = false)
    private String fullName;
    private String phoneNumber;

    @ManyToOne
    private House house;

    public Person() {
    }

    public Person(@JsonProperty("PersonId") long id,
                  @JsonProperty("FullName") String fullName,
                  @JsonProperty("PhoneNumber") String phoneNumber,
                  @JsonProperty("House") House house) {
        this.personId = id;
        this.fullName = fullName;
        this.phoneNumber = phoneNumber;
        this.house = house;
    }
//getters and setters
} 
@Entity
public class House {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(unique = true)
    private long houseId;

    private String address;
    private String houseNumber;
    private String city;

    @OneToMany(mappedBy = "house")
    private List<Person> people;
    //field expenses

    public House() { }

    public House(@JsonProperty("HouseId") long id,
                 @JsonProperty("Address") String address,
                 @JsonProperty("HouseNumber") String houseNumber,
                 @JsonProperty("City") String city,
                 @JsonProperty("People") List<Person> people) {
        this.houseId = id;
        this.address = address;
        this.houseNumber = houseNumber;
        this.city = city;
        this.people = people;
    }

//getters and setters
}
zhrgci
  • 584
  • 1
  • 6
  • 25

0 Answers0