gpt4 book ai didi

java - Spring JPA 和 Hibernate 的 JPQL 查询错误

转载 作者:行者123 更新时间:2023-11-29 07:20:29 24 4
gpt4 key购买 nike

我正在尝试获取与特定人员相关的所有技能。

在我的 Spring Boot 应用程序中尝试使用 JPQL 从 MySQL DB 获取数据时出现以下错误:

org.hibernate.QueryException: could not resolve property: person_skill of: com.skilltrack.app.Domain.Skill [SELECT s FROM com.skilltrack.app.Domain.Skill s JOIN s.person_skill ps WHERE ps.fk_person = ?1 ]

这是我的代码:

Person.java

@Data
@EqualsAndHashCode(exclude = "personCourses")
@Entity(name = "person")
@Table(name = "person")
public class Person implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer person_id;

@NotBlank
private String name;

@NotBlank
private String surname;

@NotBlank
private String email;

@NotBlank
private String password;

@NotBlank
private String personType; //admin or user

@JsonIgnore
@ManyToMany // this mapping is referred to in the Course class
@JoinTable(name = "person_course",
joinColumns = @JoinColumn(name = "fk_person", referencedColumnName = "person_id"),
inverseJoinColumns = @JoinColumn(name = "fk_course", referencedColumnName = "course_id"))
private List<Course> personCourses;

@JsonIgnore
@ManyToMany // this mapping is referred to in the Skill class
@JoinTable(name = "person_skill",
joinColumns = @JoinColumn(name = "fk_person", referencedColumnName = "person_id"),
inverseJoinColumns = @JoinColumn(name = "fk_skill", referencedColumnName = "skill_id"))
private List<Skill> personSkills;

类(class).java

@Data
@EqualsAndHashCode(exclude = "skills")
@Entity(name = "course")
@Table(name = "course")
public class Course {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer course_id;

@NotBlank
private String name;

@NotBlank
private String description;

@JsonIgnore
@ManyToMany(mappedBy = "courses") // mapping defined in Skill already
private List<Skill> skills;

@NotBlank
private Boolean completed; // 0 no 1 yes

@JsonIgnore
@ManyToMany(mappedBy = "personCourses")
private List<Person> coursePerson;

技能.java

@Data
@EqualsAndHashCode(exclude = "courses")
@Entity(name = "skill")
@Table(name = "skill")
public class Skill {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer skill_id;

@NotBlank
private String name;

@NotBlank
private String description;

@JsonIgnore
@ManyToMany(cascade = CascadeType.ALL) // this mapping is referred to in the Course class
@JoinTable(name = "course_skills",
joinColumns = @JoinColumn(name = "fk_skill", referencedColumnName = "skill_id"),
inverseJoinColumns = @JoinColumn(name = "fk_course", referencedColumnName = "course_id"))
private List<Course> courses;

@JsonIgnore
@ManyToMany(mappedBy = "personSkills")
private List<Person> skill_person;

PersonRepository.java

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {

@Query(value =
"SELECT s " +
"FROM skill s JOIN s.person_skill ps " +
"WHERE ps.fk_person = ?1 ")
List<Skill> getPersonSkills(Integer personID);
}


The issue is with the JPQL statement:
"SELECT s FROM skill s JOIN s.person_skill ps WHERE ps.fk_person = ?1 "

I have tried the following variations:

"SELECT s FROM skill s INNER JOIN s.person_skill ps WHERE ps.fk_person = ?1"

"SELECT s FROM skill s JOIN FETCH s.person p WHERE ps.fk_person = ?1"

"SELECT s FROM skill s JOIN s.person p JOIN s.person_skill ps WHERE ps.fk_person = ?1"

"SELECT s FROM skill s JOIN person p JOIN person_skill ps WHERE ps.fk_person = ?1"

我应该如何更改我的 JPQL 查询以返回与特定人员相关的技能列表?

最佳答案

将您错误键入的实体名称从 person_skill 更改为 skill_personSkill.java 中定义

关于java - Spring JPA 和 Hibernate 的 JPQL 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56577244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com