我有以下 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
正如您在定义中看到的那样,我使用带有 PANACHE 的 HIBERNATE ORM,它遵循 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
正确更新账户的属性?