[英]How to reference entity in Query in CrudRepository in Spring Data JDBC?
I want to fetch Entity from Database with Spring Data CrudRepository with a findWithTitle()
method like this:我想使用像这样的findWithTitle()
方法使用 Spring Data CrudRepository 从数据库中获取实体:
interface TasksCrudRepository extends CrudRepository<Task, Long> {
@Query(value = "SELECT t FROM Task t WHERE t.title IS NOT NULL")
List<Task> findWithTitle();
}
But I get但我明白了
org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "T" not found; SQL statement:
SELECT t FROM Task t [42122-199]
I can't find the cause why is that happening.我找不到发生这种情况的原因。 From what I know this syntax should be working as expected.据我所知,这种语法应该按预期工作。
This is how Task entity looks like:这是 Task 实体的样子:
@Data
@Table("task")
@AllArgsConstructor
public class Task {
@Id
private Long id;
private String title;
private String description;
private Set<Attachment> attachments;
private Set<TagRef> tagRefs;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
And here's the schema for it:这是它的架构:
CREATE TABLE task
(
id IDENTITY,
title VARCHAR(100),
description VARCHAR(1024),
created_at TIMESTAMP,
updated_at TIMESTAMP,
);
Instead of Custom query use predefined Spring JPA method.而不是自定义查询使用预定义的Spring JPA方法。
Please try the below approach.请尝试以下方法。
interface TasksCrudRepository extends CrudRepository<Task, Long> {
List<Task> findByTitleNotNull();
}
Modifications done:所做的修改:
The solution is to use asterisk as @deHaar mentioned:解决方案是使用@deHaar 提到的星号:
@Query("SELECT * FROM Task t ...")
List<Task> findTasks();
}
What is more if you want to perform JOIN query it is also possible to run with asterisk as well:更重要的是,如果您想执行 JOIN 查询,也可以使用星号运行:
@Query("SELECT * FROM Task t JOIN Attachment a ON t.id = a.task")
List<Task> findWithAttachments();
May be this will help you, COLUMN_NOT_FOUND_1 = 42122 The error with code 42122 is thrown when referencing an non-existing column.可能这会对您有所帮助, COLUMN_NOT_FOUND_1 = 42122 引用不存在的列时会引发代码为 42122 的错误。 Example: CREATE TABLE TEST(ID INT);示例:创建表测试(ID INT); SELECT NAME FROM TEST; SELECT 名称来自测试;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.