spring-boot - Spring JPA Criteria API 无法访问子类中的属性
问题描述
我有以下课程:(仅显示相关字段)
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
class Content {
}
@Entity
class Demo : Content {
var key: String
}
@Entity(name = "contentlink")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
class ContentLink {
@ManyToOne
@JoinColumn(name = "content_id")
internal var content: Content
}
@Entity
class DemoLink : ContentLink {
}
我正在尝试选择匹配对象与某些条件匹配的DemoLink
实体列表。Demo
为此,我想使用规格:
fun hasType(type: String): Specification<DemoLink> {
return Specification { root, _, cb ->
cb.equal(root.get<Demo>("content").get<String>("type"), type)
}
}
但是,当我尝试使用该规范执行查询时,出现以下错误:
无法在此 ManagedType [内容] 上找到具有给定名称 [类型] 的属性
我尝试向 CriteriaBuilder 说明这一点:
fun hasType(type: String): Specification<DemoLink> {
return Specification { root, _, cb ->
cb.equal(cb.treat(root, DemoLink::class.java).get<Demo>("content").get<String>("type"), type)
}
}
不幸的是,这仍然会导致相同的错误。所以,我决定过火,也投了内容关联:
fun hasType(type: String): Specification<DemoLink> {
return Specification { root, _, cb ->
val link = cb.treat(root, DemoLink::class.java)
val demo = cb.treat(link.get("content"), Demo::class.java)
cb.equal(demo.get<String>("type"), type)
}
}
即使这会导致相同的错误。
我究竟做错了什么?
谢谢
解决方案
推荐阅读
- typescript - 有人可以告诉我返回泛型类型 T 和未定义是否可以接受?
- python - 如何在列表中查找彼此相距 1000 以内的值(python)
- sql - 两个日期之间的 SQL 检查不返回任何结果
- javascript - 将 dotenv 与 react 和 webpack 一起使用
- ssh - 从 SSH 会话中分离命令
- sql-server - 如何通过 Plesk 将我的 script.sql 文件和我的数据库文件上传到 GoDaddy?
- python - 我需要将抓取的 url 以 URI 格式保存到 csv 文件中。文件不会写入 csv
- ios - 如何在 Azure Pipeline 中为 iOS 版本使用自动预配
- python - 没有前缀的 URL
- javascript - Selenium 是否支持使用不同配置文件运行的多个浏览器窗口?