【问题标题】:InvalidDataAccessApiUsageException - Parameter value did not match expected typeInvalidDataAccessApiUsageException - 参数值与预期类型不匹配
【发布时间】:2021-08-05 11:47:53
【问题描述】:

我需要从数据库中获取所有具有特定角色的用户。为此,我需要使用 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)]

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

【问题讨论】:

    标签: kotlin spring-data-jpa


    【解决方案1】:

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

    问题解决了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2018-09-02
      • 2016-04-22
      • 2020-11-29
      • 2015-12-13
      相关资源
      最近更新 更多