gpt4 book ai didi

java - 列表中包含 "id"的默认 Spring Data 查询(JpaRepository)

转载 作者:行者123 更新时间:2023-12-01 19:48:35 26 4
gpt4 key购买 nike

我有如下简单实体:

建议.java:

@Entity
public class Suggestion {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToOne
private Employee author;

@OneToMany
@JoinColumn(name = "recipients_id")
private List<Employee> recipients;
}

和 Employee.java:

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String name;
}

我想在 Controller 中返回所有 suggestions其中包含仅一个员工的 idrecipients列表。

是否可以避免自定义查询(原生查询)?

我尝试过:

findByRecipientsContains(id) 或

findByRecipientsContaining(id)

但运气不佳...

<小时/>

编辑:

当我在存储库中使用时:

Optional<List<Suggestion>> findByRecipientsIn(Long id);

也没有 Optional并在 Controller 中:

    @GetMapping("/employees/{id}/suggestions")
@ResponseStatus(HttpStatus.OK)
public List<Suggestion> getSuggestionsByRecipient(@PathVariable("id") Long id) {
return suggestionRepository.findByRecipientsIn(id).get();
}

我得到的异常如下:

Parameter value element [1] did not match expected type [com.herokuapp.erpmesbackend.erpmesbackend.employees.Employee (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value element [1] did not match expected type [com.herokuapp.erpmesbackend.erpmesbackend.employees.Employee (n/a)]] with root cause

最佳答案

关于java - 列表中包含 "id"的默认 Spring Data 查询(JpaRepository),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52284549/

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