当前位置: 首页 > 知识库问答 >
问题:

在Spring Boot中从本机SQL转换为JPA以返回列表

逄兴昌
2023-03-14

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

@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请求时,我们会得到以下错误:

组织。冬眠QueryException:JPA风格的位置参数不是整数序号;

我们将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();
    }

共有1个答案

阎安邦
2023-03-14

skills实体中的属性名称是id,而不是skill\u id

选择*从作业j内连接(选择*从job_skillsjsskill_id在哪里?1.skill_id))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);
 类似资料:
  • 我正在编写一个code-gen工具,用于使用Spring-Data-Jpa为Spring-boot应用程序生成后端连接代码,CrudRepository中的方法返回Iterable而不是List,这让我有点恼火,因为Iterable没有提供足够的功能,但是List提供了,所以我正在寻找将Iterable转换为List的最佳方法。 我看到了这篇关于将可迭代转换为集合的文章,我想知道,与其使用像Gua

  • 我目前正在使用平铺地图为pyplay中的一个游戏开发地图编辑器。关卡由以下结构中的块构建而成(尽管要大得多): 其中“1”是一块墙,“0”是一块空空气。 以下代码基本上是处理块类型更改的代码:

  • 问题内容: 我以为我有一种快速的方法来将“唯一标识符”值转换成Big Int,然后再转换回来。但是在我的第二次转换中有一个问题。任何人都可以评论将GUID完全转换为数字并返回的正确方法吗?当我尝试将其从数字表示形式转换回其原始GUID时,我只是得到了GUID的一部分,而不是全部。 我想将一个整数(我认为它将在MSSQL中归类为“ Large BigInt”?)传递给远程系统,仅使用字符0-9,并且

  • 我有一个用hibernate持久化的带有DateTime属性的实体 对于常规的spring-data-jpa生成的查询来说,这一切都很好。 我正在尝试添加自定义本机查询 我得到的错误是当试图调用这个是 这与我在将自定义类型映射添加到实体之前使用常规spring-data Finder时得到的错误相同 如何使spring-data-jpa/hibernate使用参数到本机查询的自定义类型映射?

  • 方法(下面)是一个类型,我用作参数的类是一个。我想知道我选角的方式是否管用: 这就是方法: 此URL指向类: 我把它放进了粘贴箱,因为课程太长了。

  • 问题内容: 考虑下面的示例,其中有一个包含人员记录的 Person 表和一个包含链接到人员的可选属性的 PersonAttribute 表: Table: Person Table PersonAttribute 我将如何编写一个查询,使所有具有属性的人都像列一样返回?我需要的结果集是: 因此,从本质上讲,我需要编写一个查询,该查询将获取所有带有所有唯一属性键的人记录,这些键被转换为具有每个人记录