FindById查询方法“无法在此ManagedType上找到具有给定名称[id]的属性”]]

问题描述 投票:0回答:1

因此,我正在尝试使用查询方法(如果这就是它们的名称),由于某种原因,我无法启动我的应用程序,因为我命名方法的方式仍然错误。

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

所以我正在尝试查询方法(如果这就是它们的名字),由于某种原因,我无法启动我的应用程序,因为我命名方法的方式仍然错误。 @Repository(“ ...

java mysql methods spring-data-jpa
1个回答
0
投票

您的实体中有houseId而不是Id,您的方法应如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.