如何用hibernate panache更新属性?

xwbd5t1u  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(475)

我有以下ddl定义:

@Entity
data class Interest(
    @JsonIgnore
    @ManyToMany(mappedBy = "interests")
    var account: List<Account> = listOf(),
    var description: String = "") : PanacheEntity()

@Entity
data class Account(@field:Id var uuid: UUID? = null,
                   @ManyToOne(fetch = FetchType.LAZY) var gender: Gender? = null,
                   var birthday: Long = 0,
                   @ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST])
                   @JoinTable(
                       name = "account_interests",
                       joinColumns = [JoinColumn(name = "account_id")],
                       inverseJoinColumns = [JoinColumn(name = "interest_id")]
                   )
                   var interests: List<Interest> = listOf()) : PanacheEntityBase

正如您在定义中看到的,我将hibernate orm与panache结合使用,它遵循活动记录模式。此外,我还创造了 AccountRepository :

@ApplicationScoped
class AccountRepository : PanacheRepository<Account> {

  @Transactional
  fun create(newAcc: Account): Account =
      persist(newAcc).let { newAcc }

  fun read(id: UUID): Account? =
      find("uuid", id).firstResult()

  @Transactional
  fun updateInterests(id: UUID, i: Interests): Int {
    println(i)
    return update("interests = ?1 where uuid = ?2", i, id)
  }

}

我的问题是方法 updateInterests . 它引发异常:

RESTEASY002020: Unhandled asynchronous exception, sending back 500: org.jboss.resteasy.spi.ApplicationException: java.lang.IllegalArgumentException: Parameter value [Interest(account=[], description=Luxury)] did not match expected type [java.util.Collection (n/a)]

即将到来的 interests 有效载荷为:

[Interest(account=[], description=Luxury), Interest(account=[], description=Travel)]

如何更新帐户的属性 interests 正确地?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题