java - Spring JPA Repository - jsonObject 上的运算符 SIMPLE_PROPERTY 需要标量参数
问题描述
我正在使用 JPA 查询更新一些 Spring Boot 应用程序。除了一种特定类型的查询 ( findByJsonNode
) 外,一切正常。这在早期版本中运行良好,但在我升级后无法创建我的 bean。
它在 Spring Boot 和 Spring Cloud 版本上运行良好2.1.4.RELEASE
,Greenwich.SR1
但是当我升级到 Spring Boot2.2.4.RELEASE
和 Spring Cloud 版本Hoxton.RELEASE
时,Spring 无法创建我的存储库 bean。
Caused by: java.lang.IllegalStateException: Operator SIMPLE_PROPERTY on searchDto requires a scalar argument, found class com.fasterxml.jackson.databind.JsonNode in method public abstract se.company.search.Search se.company.search.SearchRepository.findFirstBySearchDto(com.fasterxml.jackson.databind.JsonNode).
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.throwExceptionOnArgumentMismatch(PartTreeJpaQuery.java:171)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTreeJpaQuery.java:147)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:90)
... 73 common frames omitted.
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.throwExceptionOnArgumentMismatch(PartTreeJpaQuery.java:171) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTreeJpaQuery.java:147) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:90) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE]
... 74 common frames omitted
存储库类如下所示
@Repository
public interface SearchRepository extends PagingAndSortingRepository<Search, String> {
Search findFirstBySearchDto(JsonNode searchDto);
}
实体
/**
* Entity for saving as search
*
* Users can save searches and every search will be stored in history for reference
*/
@Slf4j
@Data
@Entity(name = "search")
@Table(name = "tt_searches", indexes = {
@Index(columnList = "searchDto", name = "searchdto_hidx")
})
@TypeDef(
name = "json-node",
typeClass = JsonNodeStringType.class
)
@EqualsAndHashCode(exclude = { "id", "savedSearches", "searchHistory" })
public class Search {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().findAndRegisterModules();
public Search() {
}
public Search(SearchDto searchDto, List<SavedSearch> savedSearches, List<SearchHistory> searchHistory) {
this.searchDto = OBJECT_MAPPER.valueToTree(searchDto);
this.savedSearches = savedSearches;
this.searchHistory = searchHistory;
}
public Search(JsonNode searchDto, List<SavedSearch> savedSearches, List<SearchHistory> searchHistory) {
this.searchDto = searchDto;
this.savedSearches = savedSearches;
this.searchHistory = searchHistory;
}
@Id
@Column(columnDefinition = "CHAR(36)")
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
@Type(type = "json-node")
@Column(columnDefinition = "VARCHAR(2000)", unique = true)
private JsonNode searchDto;
@OneToMany(mappedBy = "search", fetch = FetchType.LAZY)
@OrderBy("name DESC")
@JsonIgnore
private List<SavedSearch> savedSearches;
@OneToMany(mappedBy = "search", fetch = FetchType.LAZY)
@OrderBy("timestamp DESC")
@JsonIgnore
private List<SearchHistory> searchHistory;
public SearchDto getSearchDto() {
try {
return OBJECT_MAPPER.treeToValue(searchDto, SearchDto.class);
} catch (JsonProcessingException e) {
log.error("Could not convert JsonNode to SearchDto when retrieving data from entity: {}", this.id);
return null;
}
}
public void setSearchDto(SearchDto searchDto) {
this.searchDto = OBJECT_MAPPER.valueToTree(searchDto);
}
}
解决方案
我遇到了同样的问题。我通过在方法的末尾添加“In”来更改方法的名称来解决它。
对于您的示例,它将是
findAllByTagsIn(java.util.Set)
请参阅此处的“方法名称中支持的关键字”表:https ://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
推荐阅读
- c++ - 如何在 graphics.h 中进行 z 缓冲
- c# - 将 C# 代码(迷宫生成器)转换为 PHP 的问题
- rename - MAXScript:如何找到重复的纹理贴图名称并通过 maxscript 重命名?
- sharepoint-online - (401) ApplyProvisioningTemplate 1 小时后未授权
- python - yfinance 不工作 - 接收 json.decoder.JSONDecodeError
- django - 使用 Djoser 创建用户不会存储所有字段的数据(
- sql-server - 获取使用 SQL Server 中的 HierarchyId 构建的树中的叶节点
- github - Jupyter Notebook Markdown 中的文本颜色未在 GitHub 中显示
- android - 无法启动 API 29 模拟器 (Android 10)
- javascript - 在 Jquery 中使用 Div 元素选择同级的问题