如何使用Hibernate Panache更新属性?

问题描述

我有以下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 WITH PANACHE ,它遵循Active Record模式。 此外,我还创建了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属性

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)