Метод запроса FindById «Невозможно найти Атрибут с заданным именем [id] в этом ManagedType» - PullRequest
1 голос
/ 26 апреля 2020

Итак, я экспериментирую с методами запросов (если так они называются), и по какой-то причине я не могу запустить свое приложение, потому что способ, которым я называю мой метод, по-прежнему неверен.

@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);
}

Обычно я бы использовал @Query, но мой учитель сказал мне, что вы можете просто назвать его по-другому, и это тоже работает, но я изо всех сил пытаюсь увидеть проблему с моим методом.

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]

Также пытался использовать это как ссылку: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories .query-method.query-creation

Объекты:

@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
}
...