首页 > 解决方案 > 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)]

告诉我如何正确组织用户搜索?您能否向网站展示此类请求的示例?

标签: kotlinspring-data-jpa

解决方案


首先,我开始使用 JPA 功能并将“In”粒子添加到存储库中的方法名称中。第二,我删除了@Query 注释第三,我重新访问了我的测试,结果我在保存用户之后添加了角色,因此我的更改没有提交到数据库。我修好了它。

问题解决了


推荐阅读