如何在 Spring 引导应用程序中使用 JPQL 配置 JPA - "can't resolve symbol.."

How to configure JPA with JPQL in Spring Boot Application - "can't resolve symbol.."

我有依赖性spring-boot-starter-data-jpa

JPA 的配置在 application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/db?serverTimezone=UTC
spring.datasource.username=name
spring.datasource.password=pass
...

当我创建 @Entity:

@Entity
@Table(name="cats")
public class Cat {
    @Column(name="age")
    private int age;
    ....
}

效果很好。当我尝试使用 JPQL:

"select cat from Cat cat"

Intellij IDEA强调说"Can't resolve symbol Cat",我点击更多 "This inspection controls whether the Persistence QL Queries are error-checked".
值得一提的是它工作正常 没有错误 异常

为了解决这个问题,有人在Intellij中推荐:

项目结构-切面-添加-JPA.
之后 Intellij stopJPQL 附近显示警告语法,但 开始 以在 @Column(name="name") 和附近显示警告@Table(name="cats") 在我的 Entity.class.

如何配置 JPA 在 JPQL 和 Entity.class 中都不会收到此警告?

试试这个:

"select cat from " + Cat.class.getName() + " cat"

这个解决方案非常好,因为每当你重命名 class Cat 时,intellij 也会在 jpql 查询中重命名它。