简体   繁体   English

@Query 与预期类型不匹配 [java.util.Collection (n/a)]

[英]@Query did not match expected type [java.util.Collection (n/a)]

I have a table named Tasks with following columns in it: author, responsible and observers all of them are FKs of user ids which are located in Users table.我有一个名为 Tasks 的表,其中包含以下列:作者、负责人和观察者,它们都是位于用户表中的用户 ID 的 FK。 And I have the table representing many-to-many relation between Users and Tasks which contains only two columns: task_id and user_id.我有一个表,表示用户和任务之间的多对多关系,其中只包含两列:task_id 和 user_id。 I am using @Query from Spring boot to select all the rows from Tasks.我正在使用 Spring Boot 中的 @Query 来选择任务中的所有行。 My issue is that I want to select all the tasks in which currently authorized user is being observer.我的问题是我想选择当前授权用户作为观察者的所有任务。

My task model:我的任务模型:

@Entity
@Table
public class Task {

private String title;

private String description;

@Column(name = "delete_status")
private Boolean deleteStatus;

@OneToOne
@JoinColumn(name = "task_status_id")
private TaskStatus status;

@OneToMany(mappedBy = "task")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Comment> comments;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "due_date")
private Date dueDate;

@ManyToOne
@JoinColumn(name = "author_id", updatable = false)
private User author;

@ManyToOne
@JoinColumn(name = "responsible_id")
private User responsible;

@JsonManagedReference
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "observers_users",
        joinColumns = {@JoinColumn(name = "observers_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "users_id", referencedColumnName = "id")})
@LazyCollection(LazyCollectionOption.FALSE)
private List<User> observers;
}

My query:我的查询:

@Query("FROM Task t WHERE t.observers = ?1 ORDER BY t.id DESC")
List<Task> findWhereUserObserver(
        @Param("observers") List<User> user
);

No matter how I change the query, I keep getting the same error: Parameter value [User (...)] did not match expected type [java.util.Collection (n / a)]无论我如何更改查询,我都会收到相同的错误:参数值 [User (...)] 与预期类型 [java.util.Collection (n / a)] 不匹配

Because t.observers is type of List I can't figure out how to use it within SQL query in WHERE condition.因为 t.observers 是 List 类型,所以我不知道如何在 WHERE 条件下的 SQL 查询中使用它。 I want to find my currently authorized user in this list but don't understand how should I do so in SQL query.我想在这个列表中找到我当前的授权用户,但不明白我应该如何在 SQL 查询中这样做。 Or should I at all?还是我应该这样做?

Well when you say sth.好吧,当你说某事时。 like T.OBSERVERS IN it means that you will have more than one observer in a list such as IN (User1, User2, User3) etc. But you only have 1 User.像 T.OBSERVERS IN 这意味着你将在一个列表中拥有多个观察者,例如 IN (User1, User2, User3) 等。但你只有 1 个用户。 Checkout SQL IN statement https://www.w3schools.com/sql/sql_in.asp结帐 SQL IN 语句https://www.w3schools.com/sql/sql_in.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM