java - 在 Spring Boot 中从 Native SQL 转换为 JPA 以返回 List

标签 java hibernate spring-boot jpa hql

您好,我们正在尝试返回包含技能列表的职位列表。 这是我们的代码:

@Query(nativeQuery =true, value="select * from jobs j inner join( select * from job_skills js  where skill_id IN (?1.skill_id)) on j.job_id = js.job_id")
    List<Job> findBySkills(List<Skill> skills);

但是,当我们使用 Postman 进行测试并发送 POST 请求时,我们得到的错误是:

org.hibernate.QueryException: JPA-style positional param was not an integral ordinal;

我们正在将 Spring Boot 与 PostgreSQL 结合使用。我们假设问题出在这里 (?1.skill_id) 我们如何解决这个问题,以从具有查询 ID 的工作表中返回所有技能的列表?

这是我们的作业模型与 JPA 映射

@Entity
@Table(name = "jobs")
public class Job {

    @Id
    @Column(name = "job_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    /** An integer that uniquely identifies this job. */
    private int id;

    @Column(name = "job_title")
    /** The name of this job. */
    private String title;

    @Column(name = "job_description", length=10_000)
    /** A description of this job, with a maximum length of 10,000 characters. */
    private String description;

    @Column(name = "job_location")
    /** A string identifying where the job takes place. */
    private String location;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "job_skills", joinColumns = { @JoinColumn(name = "job_id") }, inverseJoinColumns = {
            @JoinColumn(name = "skill_id") })
    /** A set of skills that candidates applying to this job are expected to have. */
    private Set<Skill> skills = new HashSet<>();

    @Column(name = "job_isFilled")
    /** Returns true if the job opening is currently filled, and false otherwise. */
    private boolean isFilled;

    @OneToMany(mappedBy="job")
    private Set<Interview> interviews = new HashSet<>();

    @OneToOne
    @JoinColumn(name = "filled_by_profile_id")
    /** The employee that currently holds this job. Returns null if it is not held by any employee. */
    private Profile profile;

    /** Creates a new job with all properties set to their default values. */
    public Job() {
        super();
    }

这是我们的技能模型(仅显示相关映射)

@Entity
@Table(name = "skills")
public class Skill {

    @Id
    @Column(name = "skill_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    /** An integer that uniquely identifies this skill. */
    private int id;

    @Column(name = "skill_title")
    /** The name of this skill. */
    private String title;

    @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE }, mappedBy = "skills")
    @Column(name = "profiles")
    /** A set of candidates who claim proficiency in this skill. */

    @JsonIgnore
    private Set<Profile> profiles = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE }, mappedBy = "skills")
    @Column(name = "jobs")
    /** A set of jobs that this skill is necessary for. */
    @JsonIgnore
    private Set<Job> jobs = new HashSet<>();

    /** Creates a new skill with all properties set to their default values. */
    public Skill() {
        super();
    }

最佳答案

技能实体中的属性名称是id,而不是skill_id

select * from jobs j inner join( select * from job_skills js where skill_id IN (?1.skill_id)) on j.job_id = js.job_id

@Query(nativeQuery =true, value="select * from jobs j inner join( select * from job_skills js  where skill_id IN (?1.id)) on j.job_id = js.job_id")
    List<Job> findBySkills(List<Skill> skills);

关于java - 在 Spring Boot 中从 Native SQL 转换为 JPA 以返回 List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60065399/

相关文章:

java - 如何解决java9中的模块读取包错误

java - 操作 ArrayAdapter 时出现 ArrayIndexOutOfBoundsException

java - 强制 TableView 在后续添加时调整列的大小

java - Hibernate:为多个连接表上的属性创建映射

java - 在 Hibernate 中将集合中的映射异常另存为成员变量

spring-boot - 启用@KafkaListener以从application.yml文件中获取可变主题名称

java - 结果rawResult.getContents android更改url

java - 通过用户输入保存音频文件android

Spring Data JPA,如何获取当前事务上下文使用的Connection

spring - Spring Cloud 应用程序与 AWS Parameter Store 的集成测试