kotlin - InvalidDataAccessApiUsageException - 参数值与预期类型不匹配
问题描述
我需要从数据库中获取具有特定角色的所有用户。为此,我需要使用 JPA。
所有角色都存储在一个特殊的集合中:
UserAccount.kt
@ManyToMany(cascade = [(CascadeType.MERGE)])
@JoinTable(
name = "user_authorities",
joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "id")],
inverseJoinColumns = [JoinColumn(name = "authority_id", referencedColumnName = "id")]
)
var authoritySet: MutableSet<Authority> = hashSetOf()
我想这样做:
UserAccountService.kt
override fun getAllUsersByAuthorityName(name: String): List<UserAccountDto> {
return userAccountRepository.findUsersByAuthoritySet(mutableSetOf(authorityService.findAuthorityByAuthorityName(name))).map { it.toDto() }
}
UserAccountRepository.kt
@Query("select u from UserAccount u where u.authoritySet = ?1")
fun findUserAccountByAuthoritySet(authoritySet: MutableSet<Authority>): List<UserAccount>
但是在测试中调用这些方法时,会报错:
Parameter value [Authority(id=1, authority='ROLE_ADMIN')] did not match expected type [java.util.Set (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [Authority(id=1, authority='ROLE_ADMIN')] did not match expected type [java.util.Set (n/a)]
告诉我如何正确组织用户搜索?您能否向网站展示此类请求的示例?
解决方案
首先,我开始使用 JPA 功能并将“In”粒子添加到存储库中的方法名称中。第二,我删除了@Query 注释第三,我重新访问了我的测试,结果我在保存用户之后添加了角色,因此我的更改没有提交到数据库。我修好了它。
问题解决了
推荐阅读
- c - 如何确定结构的成员是否已设置?
- c# - 如何将 IConfiguration 绑定到在构造函数中具有参数的类
- azure-devops - 使用 Azure DevOps 恢复 EfCore 迁移
- javascript - 为什么 querySelectorAll 不选择所有(“li”)元素?
- django - 如何只允许管理员(或某些用户)在 Django 中编辑页面?
- node.js - 为什么要在 node/express 中的 websocket 服务器中挂接 httpserver?
- java - 如何在通用列表中读取 JSON
- javascript - 如何获取函数中单元格的先前值
- android - Android Accessibility 服务同时与多个开关交互
- azure - 如何自动将数据从 CSV 文件加载到 Azure SQL